mybatis xml时间范围查询三种方式

推荐

推荐方式一和方式和,因为方式三使用了函数DATE_FORMAT,这样导致时间p.create_time索引失效;

其中between ..and ...相当于  [  >=   <= ]的使用范围

方式一

 <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
      and p.create_time &gt;=#{startTime} AND  p.create_time &lt;=#{endTime}
 </if>

方式二

<if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
     and p.create_time between #{startTime} and #{endTime}
</if>

 方式三

<if test="startTime != null and startTime != '' and endTime != null and endTime != ''">

                <![CDATA[  and DATE_FORMAT(p.create_time, '%Y-%m-%d %H:%T:%s') >
                        DATE_FORMAT(#{startTime} , '%Y-%m-%d %H:%T:%s')    ]]>

                <![CDATA[  and DATE_FORMAT(p.create_time, '%Y-%m-%d %H:%T:%s') <=
                        DATE_FORMAT(#{endTime} , '%Y-%m-%d %H:%T:%s')    ]]>
</if>


版权声明:本文为liuming690452074原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。