如果实体类中的时间为Date类型,mapper中按时间查询时不指定javaType,Mybatis会视参数为Timestamp类型,时间参数被格式化为yyyy-MM-dd HH:mm:ss,如下案例
表结构:online_date类型是date
实体:时间段参数均是Date类型,而非String类型
public class UserOnline implements Serializable {
private Long userId;
// 在线时间,格式为yyyy-MM-dd HH:mm:ss
private Date onlineTime;
// 在线日期,格式为 yyyy-MM-dd
private Date onlineDate;
// 起始日期查询条件,格式为 yyyy-MM-dd
private Date onlineDateStart;
// 终止日期查询条件,格式为 yyyy-MM-dd
private Date onlineDateEnd;
}
定义用户在线实体,所有时间类型都定义为Date类型
mapper查询条件
如果不指定onlineDateStart的jdbcType=DATE,那么Mybatis会默认jdbcType=TIMESTAMP类型,查询的结果不会是预期的结果。
注意:如果使用between的话,指定了jdbcType=DATE也无效,mybatis会把数据当成Timestamp类型
<where>
<if test="userId != null">
and user_id = #{userId}
</if>
<if test="onlineDateStart != null">
and online_date <![CDATA[ >= ]]> #{onlineDateStart,jdbcType=DATE}
</if>
<if test="onlineDateEnd != null">
and online_date <![CDATA[ <= ]]> #{onlineDateEnd,jdbcType=DATE}
</if>
</where>