搜索
您的当前位置:首页正文

mybatis - datetime 日期时间范围查询具体模板演示

来源:步旅网

日期时间范围查询

其中 xxx_time 需要自行更改为自己的数据库字段名

startTime ≤  date_range  ≤ endTime

具体模板演示:

<select id="getdata" resultType="com.test.entity.TestEntity">

	SELECT * FROM test_table
<where>
<if test="startTime!=null and startTime.trim() neq ''">
     AND date_format(xxx_time,'%Y-%m-%d %H:%i:%s') >= date_format(#{startTime},'%Y-%m-%d %H:%i:%s')
</if>
<if test="endTime!=null and endTime.trim() neq ''">
     AND date_format(xxx_time,'%Y-%m-%d %H:%i:%s') &lt;= date_format(#{endTime},'%Y-%m-%d %H:%i:%s')
</if>
 </where>
</select>

下面的例子等同于上面的代码 可以使用 <![CDATA[]]> 解释 小于等于号

<select id="getdata" resultType="com.test.entity.TestEntity">

	SELECT * FROM test_table
<where>
<if test="startTime!=null and startTime.trim() neq ''">
     AND date_format(xxx_time,'%Y-%m-%d %H:%i:%s') <![CDATA[>=]]> date_format(#{startTime},'%Y-%m-%d %H:%i:%s')
</if>
<if test="endTime!=null and endTime.trim() neq ''">
     AND date_format(xxx_time,'%Y-%m-%d %H:%i:%s') <![CDATA[<=]]> date_format(#{endTime},'%Y-%m-%d %H:%i:%s')
</if>
 </where>
</select>

?CDATA說明:


参考链接

因篇幅问题不能全部显示,请点此查看更多更全内容

Top