Mybatis之动态sql

Mybatis之动态sql

1、where -if标签

mapper接口:

  List<BookEntity> selectBookList(BookEntity book);

xml配置文件:

    <select id="selectBookList" resultType="com.iflytek.pojo.BookEntity">
        select * from books
        <!-- where标签会自动去掉“标签体内前面多余的and/or” -->
        <where>
            <!-- 在if标签的test属性中,可以访问实体类的属性,不可以访问数据库表的字段 -->
        <if test="name!=null">
            name=#{name}
        </if>

        <if test="description!=null">
          and description=#{description}
        </if>

        </where>
    </select>

当传参book中的name!=null时,sql会加上where name=#{name}

desctiption!=-null时,sql会加上description=#{description}

如果name和description都为null,sql变为select * from books

2、set-if标签

    <update id="updateBookById" parameterType="BookEntity">
        update books
 <!-- 使用set标签动态管理set子句,并且动态去掉两端多余的逗号 -->
        <set>
   <!-- 注意加上-->

            <if test="name!=null">
                name=#{name},
            </if>
        <if test="description!=null">
            description=#{description},
        </if>
            <!-- 价格小于等于100-->
            <if test="price&lt;=100">
                price=#{price},
            </if>
        </set>
        <!--  where 一定要写在set标签的外面,否则逗号去不掉-->
        where id=#{id}
    </update>

更新id为为#{id}的书的name(如果传参中name不为空),description(如果传参中description不为空),price(如果传参price<=100)

3、trim标签

使用trim标签控制条件部分两端是否包含某些字符

  • prefix属性:指定要动态添加的前缀
  • suffix属性:指定要动态添加的后缀
  • prefixOverrides属性:指定要动态去掉的前缀,使用“|”分隔有可能的多个值
  • suffixOverrides属性:指定要动态去掉的后缀,使用“|”分隔有可能的多个值
    <select id="selectBookList" resultType="com.iflytek.pojo.BookEntity">
        select * from books

      <!-- 添加前缀where ,去掉后缀or或者是and-->
       <trim prefix="where" suffixOverrides="or|and">
           <if test="name!=null">
               name=#{name} and
           </if>

           <if test="description!=null">
           description=#{description} and
           </if>
       </trim>


    </select>

4、choose/when/otherwise标签

在多个分支条件中,仅执行一个。

  • 从上到下依次执行条件判断
  • 遇到的第一个满足条件的分支会被采纳
  • 被采纳分支后面的分支都将不被考虑
  • 如果所有的when分支都不满足,那么就执行otherwise分支
<!-- List<Employee> selectEmployeeByConditionByChoose(Employee employee) -->
<select id="selectEmployeeByConditionByChoose" resultType="com.atguigu.mybatis.entity.Employee">
    select emp_id,emp_name,emp_salary from t_emp
    where
    <choose>
        <when test="empName != null">emp_name=#{empName}</when>
        <when test="empSalary &lt; 3000">emp_salary &lt; 3000</when>
        <otherwise>1=1</otherwise>
    </choose>
    
    <!--
     第一种情况:第一个when满足条件 where emp_name=?
     第二种情况:第二个when满足条件 where emp_salary < 3000
     第三种情况:两个when都不满足 where 1=1 执行了otherwise
     -->
</select>

最好补一个where 1=1,否则如果都不成立,sql语句就变成了 select emp_id,emp_name,emp_salary from t_emp where

5、foreach标签

  • collection属性:要遍历的集合

  • item属性:遍历集合的过程中能得到每一个具体对象,在item属性中设置一个名字,将来通过这个名字引用遍历出来的对象

  • separator属性:指定当foreach标签的标签体重复拼接字符串时,各个标签体字符串之间的分隔符

  • open属性:指定整个循环把字符串拼好后,字符串整体的前面要添加的字符串

  • close属性:指定整个循环把字符串拼好后,字符串整体的后面要添加的字符串

  • index属性:这里起一个名字,便于后面引用

    ​ 遍历List集合,这里能够得到List集合的索引值

    ​ 遍历Map集合,这里能够得到Map集合的key

示例sql:select * from books where id in (1,2,3);

mapper接口:

List<BookEntity> selectBookList(int [] ids);

xml配置文件:

    <select id="selectBookList" resultType="com.iflytek.pojo.BookEntity">
   select * from books where id in
                       <foreach collection="ids" open="(" close=")" separator="," item="id">
                           #{id}
                       </foreach>


    </select>

注:

关于foreach标签的collection属性

如果没有给接口中List类型的参数使用@Param注解指定一个具体的名字,那么在collection属性中默认可以使用collection或list来引用这个list集合。这一点可以通过异常信息看出来:

Parameter 'empList' not found. Available parameters are [collection, list]

在实际开发中,为了避免隐晦的表达造成一定的误会,建议使用@Param注解明确声明变量的名称,然后在foreach标签的collection属性中按照@Param注解指定的名称来引用传入的参数。

批量更新:

实现批量更新则需要多条SQL语句拼起来,用分号分开。也就是一次性发送多条SQL语句让数据库执行

<!-- int updateEmployeeBatch(@Param("empList") List<Employee> empList) -->
<update id="updateEmployeeBatch">
  <!-- 在foreach标签内部如果需要引用遍历得到的具体的一个对象,需要使用item属性声明的名称 -->
    <foreach collection="empList" item="emp" separator=";">
        update t_emp set emp_name=#{emp.empName} where emp_id=#{emp.empId}
    </foreach>
</update>

如果批量更新不成功,则需要再连接数据库信息后面添加 &allowMultiQueries=true

6、sql标签

1、抽取重复的SQL片段

    <!-- 使用sql标签抽取重复出现的SQL片段 -->
    <sql id="mySelectSql">
        select emp_id,emp_name,emp_age,emp_salary,emp_gender from t_emp
    </sql>

2、引用已抽取的SQL片段

        <!-- 使用include标签引用声明的SQL片段 -->
        <include refid="mySelectSql"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值