tx

wangcx

栖迟於一丘,则天下不易其乐
21,873
MyBatis Plus Mapper.xml 映射文件常用标签 MyBatis Plus Mapper.xml 映射文件常用标签

一、判断 Integer、Long 等常数类参数

<if test="Integer != null">
    and tstc.Integer = #{Integer}
</if>
注意:判断常数类参数,只能判断 != null,不能判断 != '' 否则判断不会生效

二、判断 String 字符串类参数

<if test="str != null and str != ''">
    and t.field = #{str}
</if>
注意:判断字符串类参数可以判断 != ''

三、判断参数值与指定的值,是否相等或不相等

//判断常数类型
<if test="Long != null and Long == 0">
    and t.Long = #{Long}
</if>
<if test="Long != null and Long != 1">
    and t.Long = #{Long}
</if>

//判断字符串类型
<if test="str != null and str == 'aa'.toString()">
    and t.str = #{str}
</if>
<if test="str != null and str != 'aa'.toString()">
    and t.str = #{str}
</if>
注意:
1、判断 Integer、Long 等常数类型等于某个值,值不需要加引号
2、判断 String 字符串类型等于某个值,值需要加单引号,并且用 .toString() 转成字符串类型条件才会生效

四、判断 List、Set 集合类参数

<if test="lists != null and lists.size() > 0">
    and t.id in
    <foreach collection="lists" open="(" item="item" separator="," close=")">
        #{item}
    </foreach>
</if>
注意:
1、判断集合类参数,判断 != null 的同时一定要判断 lists.size() > 0
2、否则 lists 集合不为 null 但集合中没有元素,也会进入循环是有问题的

五、大于、小于等特殊字符转义写法

<if test="date != null">
    and date_format(t.date, '%Y-%m-%d') &lt;= date_format(#{date}, '%Y-%m-%d')
</if>
注意:
1、入参属性类型为date类型,只需要判断 != null 即可
2、>= 为大于等于、<= 为小于等于,还有其他转义写法自行查阅

六、#{}、${} 获取参数的区别及使用场景

//一般常规入参使用 #{} 占位符即可
<if test="str != null and str != ''">
    and t.str = #{str}
</if>

//特殊情况可以使用 ${} 拼接
<if test="lists != null and lists.size() > 0">
    order by t.sort_num,
    <foreach collection="lists" item="sortStr" separator=",">
        t.${sortStr}
    </foreach>
    ${sortType}
</if>
注意:
1、#{} 是占位符,${} 是拼接参数
2、常规获取参数使用 #{} 占位符即可,特殊情况也可以使用 ${} 拼接(例如根据多字段排序,需要使用 ${} 直接拼接,使用 #{} 不生效会报错)
3、当使用 #{} 占位符不生效或报错的情况下,直接使用 ${} 拼接即可
14 条评论
user 编辑评论信息
插入图片

隐私评论
  1. @
    nzulyThZOMc 3小时前
    Windows 10 · Chrome 120 · 孟加拉
    此条为悄悄话,仅发布者可见
  2. @
    nzulyThZOMc 3小时前
    Windows 10 · Chrome 120 · 孟加拉
    此条为悄悄话,仅发布者可见
  3. @
    cVaQqfpAYmh 6天前
    Windows 10 · Chrome 120 · 孟加拉
    此条为悄悄话,仅发布者可见
  4. @
    cVaQqfpAYmh 6天前
    Windows 10 · Chrome 120 · 孟加拉
    此条为悄悄话,仅发布者可见
  5. @
    cVaQqfpAYmh 6天前
    Windows 10 · Chrome 120 · 孟加拉
    此条为悄悄话,仅发布者可见
  6. @
    cVaQqfpAYmh 6天前
    Windows 10 · Chrome 120 · 孟加拉
    此条为悄悄话,仅发布者可见
  7. @
    vWlsZxyEM 4月17日
    Windows 10 · Chrome 120 · 美国
    此条为悄悄话,仅发布者可见
  8. @
    vWlsZxyEM 4月17日
    Windows 10 · Chrome 120 · 美国
    此条为悄悄话,仅发布者可见
  9. @
    vWlsZxyEM 4月17日
    Windows 10 · Chrome 120 · 美国
    此条为悄悄话,仅发布者可见
  10. @
    vWlsZxyEM 4月17日
    Windows 10 · Chrome 120 · 美国
    此条为悄悄话,仅发布者可见
  11. @
    rXGRhwuqHkA 4月3日
    Windows 10 · Chrome 120 · 美国 华盛顿Cogent通信公司
    此条为悄悄话,仅发布者可见
  12. @
    rXGRhwuqHkA 4月3日
    Windows 10 · Chrome 120 · 美国 华盛顿Cogent通信公司
    此条为悄悄话,仅发布者可见
  13. @
    rXGRhwuqHkA 4月3日
    Windows 10 · Chrome 120 · 美国 华盛顿Cogent通信公司
    此条为悄悄话,仅发布者可见
  14. @
    rXGRhwuqHkA 4月3日
    Windows 10 · Chrome 120 · 美国 华盛顿Cogent通信公司
    此条为悄悄话,仅发布者可见