动态sql

在 mapper 的动态 SQL 中若出现大于号(>)、小于号(<)、大于等于号(>=),小于等于号(<=)等
符号,最好将其转换为实体符号。否则, XML 可能会出现解析出错问题。

特别是对于小于号(<),在 XML 中是绝不能出现的。否则解析 mapper 文件会出错

实体符号表:

<小于&lt;
>大于&gt;
>=大于等于&gt;=
<=小于等于&lt;=

if标签

对于该标签的执行,当 test 的值为 true 时,会将其包含的 SQL 片断拼接到其所在的 SQL 语句中。

语法:<if test="条件"> sql 语句的部分 </if>

List<Account> selectAccountIf(Account account);
<select id="selectAccountIf" resultType="com.maj.domain.Account">
        select id,name,money from account
        WHERE 1=1
        <if test="name != null and name !=''">
            and name = #{name}
        </if>
        <if test="money > 10">
            and money &gt; #{money}
        </if>
</select>

where标签

使用<where>标签, 在有查询条件时, 可以自动添加上 where 子句;没有查询条件时,不会添加where 子句

语法:<where> 其他动态 sql </where>

List<Account> selectAccountWhere(Account account);
<select id="selectAccountWhere" resultType="com.maj.domain.Account">
    select id,name,money from account
    <where>
        <if test="name != null and name !=''">
            name = #{name}
        </if>
        <if test="money > 10">
            and money &gt; #{money}
        </if>
    </where>
</select>

foreach标签

<foreach/>标签用于实现对于数组与集合的遍历。

对其使用,需要注意:

  • collection 表示要遍历的集合类型, list , array 等。
  • open、 close、 separator 为对遍历内容的 SQL 拼接

语法:

<foreach collection="集合类型" open="开始的字符" close="结束的字符"
                    item="集合中的成员" separator="集合成员之间的分隔符">
     #{item 的值}
</foreach>  

遍历 List<简单类型>

表达式中的 List 使用 list 表示,其大小使用 list.size 表示

List<Account> selectAccountForList1(List<Integer> idList);
<select id="selectAccountForList1" resultType="com.maj.domain.Account">
        select id,name,money from account
        <if test="list != null and list.size >0">
            where id in 
            <foreach collection="list" open="(" close=")" item="accId" separator=",">
                #{accId}
            </foreach>
        </if>
</select>

遍历List<对象类型>

List<Account> selectAccountForList2(List<Account> accountsList);
<select id="selectAccountForList2" resultType="com.maj.domain.Account">
        select id,name,money from account
        <if test="list != null and list.size >0">
            where id in
            <foreach collection="list" open="(" close=")" item="accObject" separator=",">
                #{accObject.id}
            </foreach>
        </if>
</select>

代码片段

<sql/>标签用于定义 SQL 片断,以便其它 SQL 标签复用。

其它标签使用该 SQL 片断,需要使用<include/>子标签。该sql/>标签可以定义 SQL 语句中的任何部分,所以<include/>子标签可以放在动态 SQL的任何位置。

List<Account> selectAccountForList2(List<Account> accountsList);
<!--    定义代码片段-->
    <sql id="selectAccountAll">
        select id,name,money from account
    </sql>

    <select id="selectAccountForList2" resultType="com.maj.domain.Account">
        <!--  引入定义的sql语句-->
        <include refid="selectAccountAll"></include>
        <if test="list != null and list.size >0">
            where id in
            <foreach collection="list" open="(" close=")" item="accObject" separator=",">
                #{accObject.id}
            </foreach>
        </if>
    </select>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值