工作中Mybatis.xml 常用写法
- [x] 大于、小于、等于的正确写法
符号 | 原符号 | 替换符号 |
---|---|---|
小于 | < | < |
小于等于 | <= | <= 或者 a <![CDATA[ <= ]]> b |
大于 | > | > |
大于等于 | >= | >= 或者 a <![CDATA[ >= ]]> b |
和 | & | & |
单引号 | ' | ' |
双引号 | " | " |
不等于 | != | a <![CDATA[ != ]]> b |
- [x] List数据 条件查询(separator 属性为逗号)
最后渲染的结果为:and field in ('xxx','xxx','xxx')
<if test="paramList != null and paramList.size()>0">
and field in
<foreach collection="paramList" item="item" index="index"open="(" close=")" separator=",">
#{item,jdbcType = VARCHAR}
</foreach>
</if>
- [x] List数据条件查询(separator 属性为or)
最后渲染的结果为:where (post_name LIKE '%财务%' OR post_name LIKE '%经理%' OR post_name LIKE '%技术%')
<if test="postNameList!=null and postNameList.size()>0">
and
<foreach collection="postNameList" open="(" close=")" separator=" or " item="item">
post_name like '%${item}%'
</foreach>
</if>
- [x] 字段一般查询
<if test="param != null and param != ''">
and field = #{param,jdbcType=VARCHAR}
</if>
- [x] 字段进行模糊查询
<if test="paramLike != null and paramLike != ''">
and field like CONCAT(CONCAT('%',#{paramLike,jdbcType=VARCHAR}),'%')
</if>
- [x] BigDecimal范围查询
<if test="paramMax != null ">
and field <![CDATA[ <= ]]> #{paramMax,jdbcType=DECIMAL}
</if>
<if test="paramMin != null ">
and field <![CDATA[ >= ]]> #{paramMin,jdbcType=DECIMAL}
</if>
- [x] 判断参数等于具体值
<if test='param =="0"'>
field = #{param,jdbcType=VARCHAR}
</if>
<if test="param =='0'.toString()">
field = #{param}
</if>
<if test="paramList.size() == 1 and paramList.contains('0'.toString())">
......
</if>