> 文章列表 > 【Mybatis】4—动态SQL

【Mybatis】4—动态SQL

【Mybatis】4—动态SQL

⭐⭐⭐⭐⭐⭐
Github主页👉https://github.com/A-BigTree
笔记链接👉https://github.com/A-BigTree/Code_Learning
⭐⭐⭐⭐⭐⭐

如果可以,麻烦各位看官顺手点个star~😊

如果文章对你有所帮助,可以点赞👍收藏⭐支持一下博主~😆


文章目录

  • 5 动态SQL
    • 5.1 简介
    • 5.2 `if`和`where`标签
    • 5.3 `set`标签
      • 5.3.1 相关业务举例
      • 5.3.2 实际配置方式
    • 5.4 `trim`标签
    • 5.5 `choose/when/otherwise`标签
    • 5.6 `foreach`标签
      • 5.6.1 批量插入
      • 5.6.2 批量更新
      • 5.6.3 collection属性
    • 5.7 `sql`标签
      • 5.7.1 抽取重复SQL片段
      • 5.7.2 引用已抽取的SQL片段

5 动态SQL

5.1 简介

Mybatis框架的动态SQL技术是一种 根据特定条件动态拼装SQL语句 的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。

MyBatis的一个强大的特性之一通常是它的动态SQL能力。如果你有使用JDBC或其他相似框架的经验,你就明白条件地串联SQL字符串在一起是多么的痛苦,确保不能忘了空格或在列表的最后省略逗号。动态SQL可以彻底处理这种痛苦。

One of the most powerful features of MyBatis has always been its Dynamic SQL capabilities. If you have any experience with JDBC or any similar framework, you understand how painful it is to conditionally concatenate strings of SQL together, making sure not to forget spaces or to omit a comma at the end of a list of columns. Dynamic SQL can be downright painful to deal with.

5.2 ifwhere标签

<!-- List<Employee> selectEmployeeByCondition(Employee employee); -->
<select id="selectEmployeeByCondition" resultType="com.atguigu.mybatis.entity.Employee">select emp_id,emp_name,emp_salary from t_emp<!-- where标签会自动去掉“标签体内前面多余的and/or” --><where><!-- 使用if标签,让我们可以有选择的加入SQL语句的片段。这个SQL语句片段是否要加入整个SQL语句,就看if标签判断的结果是否为true --><!-- 在if标签的test属性中,可以访问实体类的属性,不可以访问数据库表的字段 --><if test="empName != null"><!-- 在if标签内部,需要访问接口的参数时还是正常写#{} -->or emp_name=#{empName}</if><if test="empSalary &gt; 2000">or emp_salary>#{empSalary}</if><!--第一种情况:所有条件都满足 WHERE emp_name=? or emp_salary>?第二种情况:部分条件满足 WHERE emp_salary>?第三种情况:所有条件都不满足 没有where子句--></where>
</select>

5.3 set标签

5.3.1 相关业务举例

实际开发时,对一个实体类对象进行更新。往往不是更新所有字段,而是更新一部分字段。此时页面上的表单往往不会给不修改的字段提供表单项。

<form action="" method=""><input type="hidden" name="userId" value="5232" />年  龄:<input type="text" name="userAge" /><br/>性  别:<input type="text" name="userGender" /><br/>坐  标:<input type="text" name="userPosition" /><br/><!-- 用户名:<input type="text" name="userName" /><br/>   --><!-- 余  额:<input type="text" name="userBalance" /><br/>--><!-- 等  级:<input type="text" name="userGrade" /><br/>  --><button type="submit">修改</button></form>

例如上面的表单,如果服务器端接收表单时,使用的是User这个实体类,那么userNameuserBalanceuserGrade接收到的数据就是null

如果不加判断,直接用User对象去更新数据库,在Mapper配置文件中又是每一个字段都更新,那就会把userNameuserBalanceuserGrade设置为null值,从而造成数据库表中对应数据被破坏。

此时需要我们在Mapper配置文件中,对update语句的set子句进行定制,此时就可以使用动态SQL的set标签。

5.3.2 实际配置方式

<!-- void updateEmployeeDynamic(Employee employee) -->
<update id="updateEmployeeDynamic">update t_emp<!-- set emp_name=#{empName},emp_salary=#{empSalary} --><!-- 使用set标签动态管理set子句,并且动态去掉两端多余的逗号 --><set><if test="empName != null">emp_name=#{empName},</if><if test="empSalary &lt; 3000">emp_salary=#{empSalary},</if></set>where emp_id=#{empId}<!--第一种情况:所有条件都满足 SET emp_name=?, emp_salary=?第二种情况:部分条件满足 SET emp_salary=?第三种情况:所有条件都不满足 update t_emp where emp_id=?没有set子句的update语句会导致SQL语法错误-->
</update>

5.4 trim标签

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

  • prefix属性:指定要动态添加的前缀;
  • suffix属性:指定要动态添加的后缀;
  • prefixOverrides属性:指定要动态去掉的前缀,使用|分隔有可能的多个值;
  • suffixOverrides属性:指定要动态去掉的后缀,使用|分隔有可能的多个值;
<!-- List<Employee> selectEmployeeByConditionByTrim(Employee employee) -->
<select id="selectEmployeeByConditionByTrim" resultType="com.atguigu.mybatis.entity.Employee">select emp_id,emp_name,emp_age,emp_salary,emp_genderfrom t_emp<!-- prefix属性指定要动态添加的前缀 --><!-- suffix属性指定要动态添加的后缀 --><!-- prefixOverrides属性指定要动态去掉的前缀,使用“|”分隔有可能的多个值 --><!-- suffixOverrides属性指定要动态去掉的后缀,使用“|”分隔有可能的多个值 --><!-- 当前例子用where标签实现更简洁,但是trim标签更灵活,可以用在任何有需要的地方 --><trim prefix="where" suffixOverrides="and|or"><if test="empName != null">emp_name=#{empName} and</if><if test="empSalary &gt; 3000">emp_salary>#{empSalary} and</if><if test="empAge &lt;= 20">emp_age=#{empAge} or</if><if test="empGender=='male'">emp_gender=#{empGender}</if></trim>
</select>

5.5 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_empwhere<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>

5.6 foreach标签

5.6.1 批量插入

<!--collection属性:要遍历的集合item属性:遍历集合的过程中能得到每一个具体对象,在item属性中设置一个名字,将来通过这个名字引用遍历出来的对象separator属性:指定当foreach标签的标签体重复拼接字符串时,各个标签体字符串之间的分隔符open属性:指定整个循环把字符串拼好后,字符串整体的前面要添加的字符串close属性:指定整个循环把字符串拼好后,字符串整体的后面要添加的字符串index属性:这里起一个名字,便于后面引用遍历List集合,这里能够得到List集合的索引值遍历Map集合,这里能够得到Map集合的key-->
<foreach collection="empList" item="emp" separator="," open="values" index="myIndex"><!-- 在foreach标签内部如果需要引用遍历得到的具体的一个对象,需要使用item属性声明的名称 -->(#{emp.empName},#{myIndex},#{emp.empSalary},#{emp.empGender})
</foreach>

5.6.2 批量更新

上面批量插入的例子本质上是一条SQL语句,而实现批量更新则需要多条SQL语句拼起来,用分号分开。也就是一次性发送多条SQL语句让数据库执行。此时需要在数据库连接信息的URL地址中设置:

atguigu.dev.url=jdbc:mysql://192.168.198.100:3306/mybatis-example?allowMultiQueries=true

对应的foreach标签如下:

<!-- int updateEmployeeBatch(@Param("empList") List<Employee> empList) -->
<update id="updateEmployeeBatch"><foreach collection="empList" item="emp" separator=";">update t_emp set emp_name=#{emp.empName} where emp_id=#{emp.empId}</foreach>
</update>

5.6.3 collection属性

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

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

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

5.7 sql标签

5.7.1 抽取重复SQL片段

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

5.7.2 引用已抽取的SQL片段

<!-- 使用include标签引用声明的SQL片段 -->
<include refid="mySelectSql"/>