mybatis的时间转化问题
1create_time在数据库中是datetime类型
1.1传字符串
1.1.1方式一(转成同一类型)
Integer countTargetRewardSettleAccounts(
@Param(“startTime”) String startTime, @Param(“endTime”) String endTime);
SELECT
COUNT(*)
FROM target_reward_settle_accounts_daily
WHERE
<![CDATA[
create_time >= str_to_date( #{
startTime},%Y-%m-%d %H:%i:%s)
AND create_time < str_to_date( #{
endTime},%Y-%m-%d %H:%i:%s)
]]>
1.1.2方式二(转成同一类型)
<if test="beginTime!=null and beginTime!=">
<![CDATA[ and DATE_FORMAT(tr.add_time, %Y-%m-%d)>= DATE_FORMAT(#{
beginTime}, %Y-%m-%d) ]]>
</if>
<if test="endTime!=null and endTime!=">
<![CDATA[ and DATE_FORMAT(tr.add_time, %Y-%m-%d) <= DATE_FORMAT(#{
endTime}, %Y-%m-%d) ]]>
</if>
1.1.3方式三:
SELECT
COUNT(*)
FROM target_reward_settle_accounts_daily
WHERE
<![CDATA[
create_time >= #{
startTime}
AND create_time < #{
endTime}
]]>
1.2 传date
本身是同一种格式,但注意取值范围
Integer countTargetRewardSettleAccountsByDate(
@Param(“startTime”) Date startTime, @Param(“endTime”) Date endTime);
SELECT
COUNT(*)
FROM target_reward_settle_accounts_daily
WHERE
<![CDATA[
create_time >= #{
startTime,jdbcType=TIMESTAMP}
AND create_time < #{
endTime,jdbcType=TIMESTAMP}
]]>
当你想在实体类中使用java.util.Date类型,而且还想在数据库中保存时分秒时,你可以在xml中修改为:
#{xxdate,jdbcType=TIMESTAMP}
就是将#{}中的jdbcType属性设置成TIMESTAMP,这样在保存的时候就会将时分秒也包含进去。
如果你xml中使用了,为了防止意外,最好将相应的字段也修改:
2 数据库是字符串类型
2.1 传参是字符串
<if test="beginTime!=null and beginTime!=">
AND tm.add_time>=#{
beginTime}
</if>
<if test="endTime!=null and endTime!=">
AND tm.add_time <=#{
endTime}
</if>
3DATE_FORMAT(date, format) 函数用法
宗旨:转换成同一类型进行比较 不关是传参是字符串还是时间,接受是字符串还是时间,都可以用DATE_FORMAT()
注意:进行时间段的查询时,在mapper文件中直接使用">","<“等关系运算符是无法解析的‘ 解决方法有两种, 一种是使用">","<"来表示大于和小于关系,这样,在解析时,这些特殊字符会被转义成所匹配的运算符 另一种是使用”<![CDATA[ ]]>"来嵌套不需要转义的内容
正确的写法:
<if test="beginTime!=null and beginTime!=">
<![CDATA[ and DATE_FORMAT(tr.add_time, %Y-%m-%d)>= DATE_FORMAT(#{
beginTime}, %Y-%m-%d) ]]>
</if>
<if test="endTime!=null and endTime!=">
<![CDATA[ and DATE_FORMAT(tr.add_time, %Y-%m-%d) <= DATE_FORMAT(#{
endTime}, %Y-%m-%d) ]]>
</if>
<if test="beginTime!=null and beginTime!=">
and DATE_FORMAT(tr.add_time, %Y-%m-%d)>=DATE_FORMAT(#{
beginTime}, %Y-%m-%d)
</if>
<if test="endTime!=null and endTime!=">
and DATE_FORMAT(tr.add_time, %Y-%m-%d)<= DATE_FORMAT(#{
endTime}, %Y-%m-%d)
</if>
4 另外参考
5datetime和timestamp的区别
时间日期数据类型总概况 MySQL中有多种表示时间日期的数据类型,主要有YEAR、TIME、DATE、DATETIME、TIMESTAMP等。每一种数据类型都有存储的时间日期格式、以及取值范围,因此在使用时间日期数据类型的时候需要选取最佳的数据类型。
5时间处理(项目)
上一篇:
Python 安装包管理工具 pip
下一篇:
npm安装包报错问题及解决
