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>:相关设置,键值对 ...
随机推荐
- fastJson 之 JSONPath使用
1. JSONPath介绍 官网地址: https://github.com/alibaba/fastjson/wiki/JSONPath fastjson 1.2.0之后的版本支持JSONPath. ...
- 给大家推荐一个C#下文件监听器和资源管理器的示例Demo-含源码
C#下文件监听器和资源管理器的示例Demo:源码下载地址
- python之type函数
python 的type 函数 的介绍的 下面就是此函数的参数 三个参数的意义 '''type(class_name, base_class_tuple, attribute_dict)cla ...
- Java读写文件,字符输入流FileReader 和 字符输出流FileWriter
一个流被定义为一个数据序列.输入流用于从源文件读取数据,输出流用于向目标写数据. 字符输入流FileReader三种读文件方式 package com.shuzf.fileio; import jav ...
- Ubuntu中libprotobuf版本冲突的解决方案
先说解决方法: 因为我出现这个比较奇特,我再下面环境中的第一个项目有这个问题,但是不知道怎么瞎折腾就搞定了,不报这个异常了 不论是Qt Creator直接运行Debug或者Release都没问题 但是 ...
- Grafana+Prometheus打造全方位立体监控系统
前言 本文主要介绍如何使用Grafana和Prometheus以及node_exporter对Linux服务器性能进行监控.下面两张图分别是两台服务器监控信息: 服务器A 服务器B 概述 Promet ...
- Mysql字段名与保留字冲突导致的异常解决
一:引言 用hibernate建表时经常遇到的一个异常:Error executing DDL via JDBC Statement 方法: 查看报错sql语句.问题就在这里. 我是表名(字段名)与保 ...
- Node.js这么下去...
Node.js是基于javascript的.event驱动的单进程服务器(也能实现cluster模式,只要一个fork()语句,类似于C语言的进程创建). 所以大胆估计:Node.js会把很多大网站吞 ...
- IIS配置Url重写实现http自动跳转https的重定向方法(100%解决)
引言 本文推荐阅读地址:https://www.52abp.com/BlogDetails/10008 这种文章网上可以说一搜一大把,但是我为什么还要写呢,因为一搜一把没把我气死,都是东抄西挪的东西, ...
- nodejs源码编译-mipse64el架构
下载nodejs,node-v6.1.0.tar.gz 链接: https://pan.baidu.com/s/1eCtNBWD5yaKiQIHp3pRKew 提取码: faun 注意对应版本的gcc ...