MyBatis基础:MyBatis动态SQL(3)
1. 概述
MyBatis中动态SQL包括元素:
| 元素 | 作用 | 备注 |
|---|---|---|
| if | 判断语句 | 单条件分支判断 |
| choose(when、otherwise) | 相当于Java中的case when语句 | 多条件分支判断 |
| trim(where、set) | 辅助元素 | 用于处理SQL拼接问题 |
| foreach | 循环语句 | 用于in语句等列举条件 |
2. if元素
if元素是最常用的判断语句,常与test属性联合使用。
2.1 if
<resultMap id="baseResultMap" type="com.libing.helloworld.model.Role">
<id property="id" column="id" />
<result property="roleName" column="role_name" />
</resultMap>
<select id="findBySearchText" resultMap="baseResultMap">
SELECT
id,
role_name
FROM
role
WHERE 1 = 1
<if test="searchText != null and searchText != ''">
AND role_name LIKE CONCAT('%', #{searchText,jdbcType=VARCHAR}, '%')
</if>
ORDER BY id ASC
</select>
2.2 if + where
<select id="findBySearchText" resultMap="baseResultMap">
SELECT
id,
role_name
FROM
role
<where>
<if test="id > 0">
id >= #{id}
</if>
<if test="searchText != null and searchText != ''">
AND role_name LIKE CONCAT(CONCAT('%',#{searchText,jdbcType=VARCHAR}),'%')
</if>
</where>
ORDER BY id ASC
</select>
MyBatis中where标签会判断如果所包含的标签中有返回值,则插入一个‘where’。此外,如果标签返回的内容是以AND或OR开头,则自动删除开头的AND或OR。
2.3 if + set
<update id="update" parameterType="com.libing.helloworld.model.Role">
UPDATE role
<set>
<if test="roleName != null and roleName != ''">
role_name = #{roleName},
</if>
<if test="remark != null and remark != ''">
remark LIKE CONCAT('%', #{remark, jdbcType=VARCHAR}, '%')
</if>
</set>
WHERE id = #{id}
</update>
上面形式,当ramark=null时,动态SQL语句会由于多出一个“,”而错误。
3. choose(when,otherwise)元素
<select id="findByCondition" resultMap="baseResultMap">
SELECT
id,
role_name
FROM
role
<where>
<choose>
<when test="id > 0">
id >= #{id}
</when>
<otherwise>
AND role_name LIKE CONCAT('%', #{roleName, jdbcType=VARCHAR}, '%')
</otherwise>
</choose>
</where>
ORDER BY id ASC
</select>
4.trim元素
4.1 trim:if + where
<select id="findByCondition" resultMap="baseResultMap">
SELECT
id,
role_name
FROM
role
<trim prefix="where" prefixOverrides="AND | OR">
<if test="id > 0">
id >= #{id}
</if>
<if test="roleName != null and roleName != ''">
AND role_name = LIKE CONCAT('%', #{roleName, jdbcType=VARCHAR}, '%')
</if>
</trim>
ORDER BY id ASC
</select>
4.2 trim:if + set
<update id="update" parameterType="com.libing.helloworld.model.Role">
UPDATE role
<trim prefix="set" suffixOverrides=",">
<if test="roleName != null and roleName != ''">
role_name = #{roleName},
</if>
<if test="remark != null and remark != ''">
remark LIKE CONCAT('%', #{remark, jdbcType=VARCHAR}, '%')
</if>
</trim>
WHERE id = #{id}
</update>
5. foreach元素
foreach元素是一个循环语句,作用是遍历集合,支持遍历数组、List、Set接口的集合。
import org.apache.ibatis.annotations.Param;
List<Role> findByIds(@Param("ids") List<Integer> ids);
<select id="findByIds" resultMap="baseResultMap">
SELECT
id,
role_name
FROM
role
WHERE id IN
<foreach collection="ids" index="index" item="id" open="(" separator="," close=")">
#{id}
</foreach>
ORDER BY id ASC
</select>
其中,
collection:传入的参数名称,可以是一个数组、List、Set等集合
item:循环中当前的元素
index:当前元素在集合的位置下标
open和close:包裹集合元素的符号
separator:各个元素的间隔符
int insertBatch(List<Role> list);
<insert id="insertBatch" parameterType="java.util.List">
INSERT role
(
role_name
)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.roleName}
)
</foreach>
</insert>
6. bind元素
bind元素的作用是通过OGNL表达式去自定义一个上下文变量。
List<Role> findBySearchText(@Param("searchText") String searchText);
<select id="findBySearchText" resultMap="baseResultMap">
<bind name="pattern_searchText" value="'%' + searchText + '%'"/>
SELECT
id,
role_name
FROM
role
WHERE
role_name LIKE #{pattern_searchText}
</select>
其中,
searchText:传入的参数名称
MyBatis基础:MyBatis动态SQL(3)的更多相关文章
- mybatis 基础(二) 动态sql 关于where if / where choose when otherwise
个人理解: where if就相当于正常的java中的if 语句,如果有多个条件组合判断的话用 and, or连接 而where choose when otherwise choose就好像是swi ...
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- Mybatis入门之动态sql
Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...
- mybatis 详解------动态SQL
mybatis 详解------动态SQL 目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...
- mybatis中的动态SQL
在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...
- Mybatis映射文件动态SQL语句-01
因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...
- mybatis入门基础(五)----动态SQL
一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...
- 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)
动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...
- 11、MyBatis教程之动态SQL
12.动态SQL 1.介绍 什么是动态SQL:动态SQL指的是根据不同的查询条件 , 生成不同的Sql语句. 官网描述: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或 ...
- 【长文】Spring学习笔记(七):Mybatis映射器+动态SQL
1 概述 本文主要讲述了如何使用MyBatis中的映射器以及动态SQL的配置. 2 MyBatis配置文件概览 MyBatis配置文件主要属性如下: <settings>:相关设置,键值对 ...
随机推荐
- 水题:P2799 国王的魔镜
思路:简单模拟即可.判断一下是不是回文,是回文看长度是不是偶数.是偶数的话,说明又可能是回文.依次这样处理.但是只要长度为奇数则一定是原来的长度直接输出即可. #include<iostream ...
- ssm框架的整合搭建(一)
一个转行菜鸟半年多工作的开始学习历程............ 我是自学,也是我的记录,我学习的见证,如果你有幸看见,不要吐槽,不足之处请指点,相互学习,谢谢!! 请一起共勉!!!!!!!! 使用技术: ...
- IndentationError 这个错误是缩进的问题
1.自定义库导入时,提示这个错误,是缩进的问题
- Java NIO6:选择器1——理论篇
一.选择器 选择器提供选择执行已经就绪的任务的能力,这使得多元I/O成为了可能,就绪执行和多元选择使得单线程能够有效地同时管理多个I/O通道. 某种程度上来说,理解选择器比理解缓冲区和通道类更困难一些 ...
- JS 格式化时间(获取两个日期之间的每一天、每一月、每半小时、每一秒)
时间戳转换为时间 // 时间戳转换为时间 function timestampToTime(timestamp, isMs = true) { const date = new Date(timest ...
- Feign性能优化注意事项
一.FeignClient注解 FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上 @FeignClient(name ...
- Jmeter(三十五)_分布式
jmeter分布式简单步骤说明: 1:添加远程服务器IP到配置文件 在JMETER_HOME / bin / jmeter.properties中,找到名为“ remote_hosts ” 的属性,并 ...
- Bean笔记
为什么需要Bean , 因为 Aop 需要. 顺序 InstantiationAwareBeanPostProcessor , BeanPostProcessor 每个Bean都会执行这两个组件的相关 ...
- Echatrs 中PIE饼图中间位置怎么显示总数值?
title: { text: '总资产', subtext: '2000000.00', x: 'center', y: 'center' }图例:
- Springboot+Redis 配置和使用
pom.xml 引入redis 开启缓存 <!-- cache --> <dependency> <groupId>org.springframework.boot ...