Mybatis 比较时间日期

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>
		<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>

  

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