MyBatis 学习(六)之动态 SQL

news/2025/2/12 23:11:31/

目录

1 动态 SQL 介绍

2 if 标签

3 where 标签

4 set 标签

5 trim 标签

6 choose、when、otherwise 标签

7 foreach 标签

8 bind 标签


1 动态 SQL 介绍

        动态 SQL 是 MyBatis 强大特性之一,极大的简化我们拼装 SQL 的操作。MyBatis 的动态 SQL 是基于 OGNL 的( Object Graph Navigation Language ,对象图导航语言),它是一种强大的表达式语言,通过它可以非常方便的来操作对象属性, 类似于 EL,SpEL 等的表达式。动态 SQL 主要有以下几类:

  • if 标签用于条件判断,根据条件是否满足来决定是否包含某个元素。
  • where 标签用于生成 WHERE 子句,并会自动去掉(忽略)第一个条件的 and 或 or 关键字,避免语法错误
  • set 标签:用于生成 SET 子句,进行更新操作,可以自动处理最后一个条件结尾的逗号
  • trim 标签用于去除或添加前缀和后缀
  • choose、when、otherwise 标签类似于 Java 中的 switch、case、default 语句,根据不同的条件选择执行不同的 SQL 片段
  • foreach 标签用于遍历集合,常用于 IN 语句中的参数列表生成
  • bind 标签用于在 OGNL 表达式之外创建一个变量,并将其绑定到当前的上下文中,以便在后续的表达式中使用

2 if 标签

if 标签,根据条件是否满足来决定是否包含某个元素,判断条件写在 if 标签的 test 属性中

<resultMap id="employeeMap" type="com.dao.Employee"><id property="empId" column="employee_id"/><result property="empName" column="employee_name"/><result property="empAge" column="employee_age"/><result property="empSex" column="employee_sex"/><result property="empEmail" column="employee_email"/><result property="empAddress" column="employee_address"/>
</resultMap><select id="getEmpIf" parameterType="com.dao.Employee" resultMap="employeeMap">select * from employee where<if test="empName != null and empName != ''">employee_name=#{empName}</if>
</select>

上述代码如果 if 标签的条件不符合,则查询语句为 select * from employee where,会报错

3 where 标签

where 标签,用于生成 WHERE 子句,并会自动去掉(忽略)第一个条件的 and 或 or 关键字,避免语法错误。

<select id="getEmpWhereIf" parameterType="com.dao.Employee" resultMap="employeeMap">select * from employee<where><if test="empName != null and empName !=''">and employee_name=#{empName}</if></where>
</select>

通过 where 标签查询,不会出现 SQL 语句多余 where 的情况

set 标签

set 标签,用于生成 SET 子句,进行更新操作,可以自动处理最后一个条件结尾的逗号

<update id="UpdateEmployee" parameterType="com.dao.Employee">update employee<set><if test="empName != null and empName !=''">employee_name=#{empName},</if></set>where employee_id=#{empId}
</update>

测试代码需要提交事务,sqlSession.commit(); 否则不会更新数据库

trim 标签

trim 标签,用于去除或添加前缀和后缀,以下是 trim 标签的属性:

  • prefix:表示在 trim 标签内的 sql 语句加上前缀
  • suffix:表示在 trim 标签内的 sql 语句加上后缀
  • prefixOverrides:表示去除第一个前缀
  • suffixOverrides:表示去除最后一个后缀
<select id="getEmpWhereIf" parameterType="com.dao.Employee" resultMap="employeeMap">select * from employee<!-- 添加前缀 where,移除 and 或 or --><trim prefix="where" prefixOverrides="and | or"><if test="empName != null">and employee_name=#{empName}</if></trim>
</select>

6 choose、when、otherwise 标签

choose、when、otherwise 标签,类似于 Java 中的 switch 语句,根据不同的条件选择执行不同的 SQL 片段

<select id="selectEmployeeByChoose" resultType="com.dao.Employee" parameterMap="employeeMap">select * from employee<where><choose><when test="empName!= null and empName!=''">employee_name=#{empName}</when><when test="empAddress!= null and empAddress!=''">and employee_address=#{empAddress}</when><otherwise>and employee_age=#{empAge}</otherwise></choose></where>
</select>

7 foreach 标签

foreach 标签,用于遍历集合,常用于 IN 语句中的参数列表生成。foreach 标签有以下几个属性:

  • collection:表示要遍历的集合元素,注意不要写 #{}。
  • item:表示每次遍历时生成的对象名(当传入 Map 对象或 Map.Entry 对象的集合时,index  是键,item 是值)
  • index:表示在迭代过程中,每次迭代到的位置
  • open:表示开始遍历时要拼接的字符串
  • close:表示结束遍历时要拼接的字符串
  • sperator:表示在每次遍历时,两个对象之间的连接字符串
在 Mapper.xml SQL 映射文件
<select id="selectEmployeeByListId" resultMap="employeeMap">select * from employee<where><!-- where employee_id in (?,?,?,?)-->
<!--        <foreach collection="ids" item="id" open="employee_id in (" close=")" separator=",">-->
<!--            #{id}-->
<!--        </foreach>--><!-- where employee_id=? or employee_id=?... --><foreach collection="ids" item="id" separator="or">employee_id=#{id}</foreach></where>
</select>在 Mapper.java 接口
List<Employee> selectEmployeeByListId(@Param("ids") List<Integer> ids);在 Test 测试类
@Test
public void testSelectByListid() {List<Integer> ids = new ArrayList<>();ids.add(1);ids.add(2);ids.add(4);ids.add(5);List<Employee> employees = mapper.selectEmployeeByListId(ids);for (Employee employee : employees) {System.out.println(employee);}
}

8 bind 标签

bind 标签,用于在 OGNL 表达式之外创建一个变量,并将其绑定到当前的上下文中,以便在后续的表达式中使用。

<!-- 查询姓马的员工 -->
<select id="selectEmployeeByName" parameterType="com.dao.Employee" resultMap="employeeMap"><bind name="pattern" value="'%' + empName + '%'"/>select * from employee where employee_name like #{pattern}
</select>


http://www.ppmy.cn/news/1368977.html

相关文章

利用FFMPEG 将RTSP流的音频G711 转码为AAC 并 推流到RTMP

之前我们的视频转码项目中 是没有加入音频的 现在 需要加入音频 &#xff0c;由于RTMP只支持AAC的 音频流 而有的RTSP流的音频编码并不是AAC 大多数都是G711编码 还分为G711A 和G711U 之前用ffmpeg命令行可以直接 完成转码 并推送到RTMP 但是考虑到无法获取更详细的状…

2314576

☞ 通用计算机启动过程 1️⃣一个基础固件&#xff1a;BIOS 一个基础固件&#xff1a;BIOS→基本IO系统&#xff0c;它提供以下功能&#xff1a; 上电后自检功能 Power-On Self-Test&#xff0c;即POST&#xff1a;上电后&#xff0c;识别硬件配置并对其进行自检&#xff0c…

APS面试审核准备的常规问题

之前根据其他人的经验贴&#xff0c;准备了一些可能APS 面试审核可能会遇到的常规问题&#xff0c;现在简单分享一下。 一般会考虑到留学资金来源&#xff0c;在德国能不能顺利毕业&#xff1b;学的是什么专业内容之类的&#xff0c;判断去德国会不会好好学习&#xff1b;对德国…

Vue开发实例(七)Axios的安装与使用

说明&#xff1a; 如果只是在前端&#xff0c;axios常常需要结合mockjs使用&#xff0c;如果是前后端分离&#xff0c;就需要调用对应的接口&#xff0c;获取参数&#xff0c;传递参数&#xff1b;由于此文章只涉及前端&#xff0c;所以我们需要结合mockjs使用&#xff1b;由于…

repo介绍和安装

介绍 https://blog.devwiki.net/2023/11/27/Windows-repo.html 安装&#xff1a; https://blog.csdn.net/ysy950803/article/details/104188793

kafka文件存储机制和消费者

1.broker文件存储机制 去查看真正的存储文件&#xff1a; 在/opt/module/kafka/datas/ 路径下 kafka-run-class.sh kafka.tools.DumpLogSegments --files ./00000000000000000000.index 如果是6415那么这个会存储在563的log文件之中&#xff0c;因为介于6410和10090之间。 2.…

Docker技术概论(2):Docker环境的搭建

Docker技术概论&#xff08;2&#xff09; Docker环境的搭建 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this article:https://blo…