mybatis - 动态sql

本文深入探讨MyBatis框架中的动态SQL特性,包括if、where、set、foreach、choose和bind标签的使用方法,以及它们如何提升SQL语句的灵活性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态sql

MyBatis 的强大特性之一便是它的动态 SQL,可以动态拼接sql,传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误,其主要元素有:

  • if
  • where trim
  • set
  • foreach
  • choose(when,otherwise)
  • bind

(1)if标签

<select id="findUserById" resultType="user">
           select * from user where 
           <if test="id != null">
               id=#{id}
           </if>
          and name = #{name}
</select>

相当于JAVA中的if语句 test是必填元素,用作判断条件,增加灵活性
但是以上这个句子如果判断为空的话 拼接就成了select * from user where and name = #{name}
就不符合sql语句的格式了,where标签就是来解决这个问题的

(2)where trim标签

<select id="findUserById" resultType="user">
           select * from user 
    		<where>
	           <if test="id != null">
	               id=#{id}
	           </if>
            and name = #{name}
           </where>
</select>

或者

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ... 
</trim>

mybatis是对它做了处理,当它遇到AND或者OR这些,它知道怎么处理

(3)set标签

<update id="updateUser" parameterType="user">
           update user set 
           <if test="name != null">
               name = #{name},
           </if> 
           <if test="sex!= null">
               password = #{password},
           </if> 
           <if test="age != null">
               age = #{age},
           </if> 
           <where>
               <if test="id != null">
                   id = #{id}
               </if>
               
           </where>
</update>

set 元素会动态前置 SET 关键字,同时也会删掉无关的逗号,因为用了条件语句之后很可能就会在生成的 SQL 语句的后面留下这些逗号。

(4)foreach标签

<select id="SelectU" resultType="user">
  SELECT *
  FROM User u
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select>

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。

  • item表示集合中每一个元素进行迭代时的别名;
  • index指定一个名字,用于表示在迭代过程中,每次迭代到的位置;
  • open表示该语句以什么开始;
  • separator表示在每次进行迭代之间以什么符号作为分隔符;
  • close表示以什么结束;

foreach允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及在迭代结果之间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象传递给 foreach 作为集合参数。当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。

(5) choose(when,otherwise)

<select id="findActiveUserLike"
     resultType="user">
  SELECT * FROM BLOG WHERE state = 'ACTIVE'
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and name != null">
      AND name like #{name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

相当于java中的switch语句

(5)bind标签

<select id="selectUserLike" resultType="Blog">
  <bind name="name" value="'%' + User.getName() + '%'" />
  SELECT * FROM User
  WHERE title LIKE #{name}
</select>

bind元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文(模糊查询)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值