MyBatis:在 MYSQL 数据库上使用 DATE 类型查询 java.util.Date

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11880378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:45:55  来源:igfitidea点击:

MyBatis : query java.util.Date with DATE type on MYSQL database

javamysqldateibatismybatis

提问by Ashika Umanga Umagiliya

In my Mapping XML I have :

在我的映射 XML 中,我有:

<select id="getPatientsByBirthday"  parameterType="date" resultType="com.axiohelix.nozoki.entity.Patient">
            SELECT id AS id,
               pharumo_id AS pharumoId,
               code AS code,
               family_name AS familyName,
               first_name AS firstName,
               family_name_kana AS familyNameKana,
               first_name_kana AS firstNameKana,
               gender AS gender,               
               address AS address,
               tel AS tel
        FROM tbl_patient
        WHERE birthday = #{id}
    </select>

where birthday is a DATEtype in MYSQL database.

其中生日是MYSQL 数据库中的DATE类型。

My Mapper interface is like this :

我的 Mapper 界面是这样的:

public interface PatientMapper {

    void insertPatient(Patient patient);
    Patient getPatientById(Integer id);
    Integer getPatientCount();
    List<Patient> getPatientsByBirthday(Date  bday);
}

I tried to query patients with particular birthday as:

我尝试将具有特定生日的患者查询为:

Calendar cal1 = new GregorianCalendar(1980, 8, 15);
Date bday=cal1.getTime();

List<Patient> plist=mapper.getPatientsByBirthday(bday);

Even though there are records with the given date,this return empty list.

即使有给定日期的记录,这也返回空列表。

Anytips ?

有小费吗 ?

回答by Sai Ye Yan Naing Aye

Change your mapping xml likes this;

像这样改变你的映射 xml;

 <select id="getPatientsByBirthday"  parameterType="java.util.Date" resultType="com.axiohelix.nozoki.entity.Patient">
        SELECT id AS id,
           pharumo_id AS pharumoId,
           code AS code,
           family_name AS familyName,
           first_name AS firstName,
           family_name_kana AS familyNameKana,
           first_name_kana AS firstNameKana,
           gender AS gender,               
           address AS address,
           tel AS tel
        FROM tbl_patient
        WHERE birthday = #{date}
</select>