6 Star 72 Fork 28

JustryDeng / notebook

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
[09]常用xml写法.md 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
JustryDeng 提交于 2023-12-15 13:43 . 常用xml写法

常用xml写法

防转义

  • 防转义

    -- 其中xxx可以包含特殊字符,不会被xml转义
    <![CDATA[xxx]]>
  • 如果不想用防转义处理的话,你可以这样写

    字符 xml中的写法
    < &lt;

判null

<if test="paramId != null">
    AND u.id = #{paramId}
</if>

判空

判断不为empty

<if test="paramName != null and paramName != ''">
    AND u.name = #{paramName}
</if>

判断不为blank

<if test="paramName != null and paramName.trim() != ''">
    AND u.name = #{paramName}
</if>

集合

<if test="stateSet != null and stateSet.size() > 0">
    and u.state in
    <foreach collection="stateSet" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</if>

xml中的if-else

-- 按顺序, 只会进第一个满足条件的when, 所有when都不满足的话, 进otherwise
<choose>
    <when test="sum == '1'.toString()">
        u.rank='第一名'
    </when>
    <when test="sum == '2'.toString()">
        u.rank='第二名'
    </when>
    <when test="sum == '3'.toString()">
        u.rank='第三名'
    </when>
    <otherwise>
        u.rank='路人'
    </otherwise>
</choose>

concat

u.`name` like CONCAT(#{paramName}, '%')

枚举

<if test="gender != null gender.name() == 'MALE'">
    AND u.gender = 1
</if>
1
https://gitee.com/JustryDeng/notebook.git
git@gitee.com:JustryDeng/notebook.git
JustryDeng
notebook
notebook
master

搜索帮助