如果实体类中的时间为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>