mybatis 查詢含Date類型參數的SQL時,沒有返回值
在使用mybatis查詢數據的時候,當參數類型爲JAVA的Date類時,查詢始終沒有返回值,將SQL放到數據庫中直接運行卻能獲取返回值。
select sif.id as id,sif.file_name as fileName,concat(sic.root_path,sif.path) as path,sif.create_date as createDate
from sc_image_file as sif
left join sc_image_config as sic on sic.id = sif.config_id
where del_flag = '0'
<if test="scId !=null and scId != ''">
and sic.sc_id = #{scId}
</if>
<if test="devType">
and sic.dev_type_id = #{devType}
</if>
<if test="startTime!=null and endTime!=null">
and sif.create_date >= #{startTime}
and sif.create_date <= #{endTime}
</if>
執行後命令如下:
==> Preparing: select sif.id as id,sif.file_name as fileName,concat(sic.root_path,sif.path) as path,sif.create_date as createDate from sc_image_file as sif left join sc_image_config as sic on sic.id = sif.config_id where del_flag = '0' and sic.sc_id = ? and sic.dev_type_id = ? and sif.create_date >= ? and sif.create_date <= ?
==> Parameters: 00000000002(String), 1(Integer), 2020-06-13 16:08:07.0(Timestamp), 2020-06-13 16:10:07.0(Timestamp)
<== Total: 0
其中startTime跟endTime爲JAVA Date類型。mybatis運行查詢結果爲0。
最後四處百度,最終定位到當mybatis按datetime條件查詢,參數爲時間戳時,如果時區沒設置對,將獲取不到正確的數據。
jdbc:mysql://localhost:3306/table_name?useTimezone=true&serverTimezone=GMT%2B8
在jdbc properties的連接地址上,增加時區爲東八區。
GMT%2B8表示東八區
修改後重新測試,結果如下:
==> Preparing: select sif.id as id,sif.file_name as fileName,concat(sic.root_path,sif.path) as path,sif.create_date as createDate from sc_image_file as sif left join sc_image_config as sic on sic.id = sif.config_id where del_flag = '0' and sic.sc_id = ? and sic.dev_type_id = ? and sif.create_date >= ? and sif.create_date <= ?
==> Parameters: 00000000002(String), 1(Integer), 2020-06-13 16:08:07.0(Timestamp), 2020-06-13 16:10:07.0(Timestamp)
<== Columns: id, fileName, path, createDate
<== Row: 1, test.jpg, BJ_1/2020/0620, 2020-06-13 16:09:01
<== Total: 1
成功獲取結果。