首页 > 其他 > 详细

Mybatis 比较时间日期

时间:2021-07-19 14:19:51      阅读:100      评论:0      收藏:0      [点我收藏+]

1、实体类中定义为 String 类型

String beginTime = "2021-07-10 15:51:01";
String endTime = "2021-07-11 15:51:01";

2、数据库中 update_date 定义为 timestamp 类型

3、Mybatis 动态 SQL 写法

// 方式一、使用 DATE_FORMAT 函数
<select id="queryAllAgents" resultType="com.tzb.cbd.modular.walletManage.vo.AgentIncomeVo">
	select
	*
	from `lssq`.sys_user su
	<where>
		<if test="beginTime != null and beginTime !=‘‘">
			and su.update_date >= DATE_FORMAT(#{beginTime},‘%Y-%m-%d %H:%i:%S‘)
		</if>
		// < 号在 xml 中是特殊字符,所以需要使用 < 来进行转义
		<if test="endTime != null and endTime != ‘‘">
			and su.update_date <= DATE_FORMAT(#{endTime},‘%Y-%m-%d %H:%i:%S‘)
		</if>
	</where>
</select>

// 方式二、使用 str_to_date 函数
<select id="queryAllAgents" resultType="com.tzb.cbd.modular.walletManage.vo.AgentIncomeVo">
	select
	*
	from `lssq`.sys_user su
	<where>
		<if test="beginTime != null and beginTime !=‘‘">
			and str_to_date(su.update_date,‘%Y-%m-%d %H:%i:%S‘) >= #{beginTime}
		</if>
		<if test="endTime != null and endTime != ‘‘">
			and str_to_date(su.update_date,‘%Y-%m-%d %H:%i:%S‘) <= #{endTime}
		</if>
	</where>
</select>

  

 

Mybatis 比较时间日期

原文:https://www.cnblogs.com/xiaomaomao/p/15029488.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!