模糊查询(CONCAT只能连接两个字符串,||可以连接多个字符串)
- SELECT * FROM table_name WHERE column LIKE CONCAT('%',#{params},'%')
- SELECT * FROM table_name WHERE UPPER column LIKE '%' || #{params} || '%'
foreach循环
(Coll为入参map的key,对应的value是个集合,item处为别名,#{valueitem}为别名的值)
- <foreach collection="Coll" item="valueitem"
- open="(" separator="," close=")">#{valueitem} </foreach>
下面引入简化的例子之一:
<select id="getReportStats" parameterType="java.util.Map" resultMap="VoResultMap"> SELECT code,name FROM table_name <where> <if test="codeLists!=null and codeLists.size>0"> AND code IN <foreach collection="codeLists" item="codevalue" open="(" separator="," close=")">#{codevalue} </foreach> </if> <if test="nameLists!=null and nameLists.size>0 ">
<foreach collection="nameLists" item="name" open="(" separator="," close=")">#{name} </foreach> </if> </where> </select>
以上是我在项目中传入了两个list的迭代,需要注意下它的判断条件写法,
以及判断关键字不能大写(踩过的坑,不要问为什么),
同时引入一篇关于map嵌套foreach循环的例子
<foreach collection="relationMap" index="key" item="ent" separator="union">
SELECT *
FROM BPM_DEF_USER
where RIGHT_TYPE=#{key}
and OWNER_ID in
<foreach collection="ent" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</foreach>
</select>
index 作为map 的key。item为map的值,这里使用了嵌套循环,嵌套循环使用ent。
时间日期的范围性查找
第一种方法:
使用转义字符
<if test="startDate != null"> AND flt_date >= #{startDate,jdbcType=TIMESTAMP} </if>
<if test="endDate != null"> AND flt_date <= #{endDate,jdbcType=TIMESTAMP} </if>
第二种方法:
因为这个是xml格式的,所以不允许出现类似“>”这样的字符,
但是都可以使用<![CDATA[ ]]>符号进行说明,将此类符号不进行解析
<if test="startDate!=null"> AND pih.oper_time<![CDATA[>=]]>#{startDate,jdbcType=TIMESTAMP} </if>
<if test="endDate!=null"> AND pih.oper_time<![CDATA[<=]]>#{endDate,jdbcType=TIMESTAMP} </if>
MyBatis中关于resultType和resultMap的区别
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是 直接表示返回类型的(对应着我们的model对象中的实体),而resultMap则是对外部ResultMap的引用(提前 定义了db和model之间的隐射key-->value关系),但是resultType跟resultMap不能同时存在。
<where>条件中不必加1=1作为拼接条件,否则影响查询的效率
选择型sql
<choose> <when test="carrierStatus=='STASUS_ENTER'"> AND tt.audit_status='STASUS_ENTER' //注意此处的赋值 <if test="timeStart!=null"> AND cc.enter_time<![CDATA[>=]]>#{timeStart,jdbcType=TIMESTAMP} </if> <if test="timeEnd!=null"> AND cc.enter_time<![CDATA[<=]]>#{timeEnd,jdbcType=TIMESTAMP} </if> </when> <when test="carrierStatus=='STASUS_UNLOADING'"> AND tt.audit_status='STASUS_UNLOADING' <if test="timeStart!=null"> AND cc.unloading_time<![CDATA[>=]]>#{timeStart,jdbcType=TIMESTAMP} </if> <if test="timeEnd!=null"> AND cc.unloading_time<![CDATA[<=]]>#{timeEnd,jdbcType=TIMESTAMP} </if> </when> <when test="carrierStatus=='STASUS_EXIT'">//双等号比较 AND tt.audit_status='STASUS_EXIT' <if test="timeStart!=null"> AND cc.exit_time<![CDATA[>=]]>#{timeStart,jdbcType=TIMESTAMP} </if> <if test="timeEnd!=null"> AND cc.exit_time<![CDATA[<=]]>#{timeEnd,jdbcType=TIMESTAMP} </if> </when> <when test="carrierStatus==null or carrierStatus==''">//双等号比较 <if test="timeStart!=null"> AND cc.enter_time<![CDATA[>=]]>#{timeStart,jdbcType=TIMESTAMP} </if> <if test="timeEnd!=null"> AND cc.enter_time<![CDATA[<=]]>#{timeEnd,jdbcType=TIMESTAMP} </if> </when> </choose>