場景
前端傳遞兩個時間參數,開始時間和結束時間,然后從數據庫中篩選出某個時間屬性在此范圍的數據。
Mybatis的動態sql的寫法。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
<if test="ksrq != null"> AND date_format(d.ddsj,'%y%m%d') >= date_format(#{ksrq},'%y%m%d') </if> <if test="jsrq != null"> AND date_format(#{jsrq},'%y%m%d') >= date_format(d.ddsj,'%y%m%d') </if>
注:
其中ksrq是前端傳遞過來的開始日期參數,jsrq是前端傳遞過來的結束日期參數。
d是數據庫的表,d.ddsj是數據庫中做篩選的時間字段。
完整xml參考
<select id="getListBySx" resultMap="KqDdjlResult"> SELECT d.id, d.gh, d.sfcl, j.xm, d.ddlb, d.ddsj, s.dept_name AS mbdw, ss.dept_name AS ydw, d.ddxs FROM dp_ddjl d LEFT JOIN dp_jbxx j ON j.gh = d.gh LEFT JOIN sys_dept s ON d.mbdw = s.dept_id LEFT JOIN sys_dept ss ON d.ydw = ss.dept_id <where> <if test="array != null"> and j.bmid in <foreach collection="array" item="item" open="(" separator="," close=")"> ${item} </foreach> </if> <if test="xm != null and xm != ''"> and j.xm like concat('%', #{xm}, '%') </if> <if test="ddlx != null and ddlx!= ''"> and d.ddlb = #{ddlx}</if> <if test="sfclwc != null and sfclwc!= ''"> and d.sfcl = #{sfclwc}</if> <if test="ksrq != null"> AND date_format(d.ddsj,'%y%m%d') >= date_format(#{ksrq},'%y%m%d') </if> <if test="jsrq != null"> AND date_format(#{jsrq},'%y%m%d') >= date_format(d.ddsj,'%y%m%d') </if> </where> </select>
