# 前言
前面mysql都是通过静态sql进行查询的,但是如果业务复杂的时候,我们会遇到引号问题,或者多一个空格,这就使得sql代码编写错误了,所以为了解决这个问题,我们有了动态sql。

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

# 动态sql
1. 先看一下模块目录结构
在类路径的resources下的mapper包下创建sql.xml文件(共性抽取)

![1.jpg](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e1b131949c1e4b68b12dd20130f43d8d~tplv-k3u1fbpfcp-watermark.image?)

2. 物理建模和逻辑建模
这里省略物理建模步骤,要求数据库的表与pojo类要对应。

```java
package pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private Integer empId;
private String empName;
private Double empSalary;

}
```
3. 引入依赖
把之前的log4j复制到类路径resouces下,另外我们引入依赖后的pom.xml如下:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>day03-mybatis02-dynamic</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>

<!-- Mybatis核心 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>

<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.3</version>
<scope>runtime</scope>
</dependency>

<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

</project>
```
4. 全局配置文件
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--驼峰映射-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--类型别名映射-->
<typeAliases>
<package name="pojo"/>
</typeAliases>
<!--环境配置-->
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="username" value="root"/>
<property name="password" value="888888"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis-example"/>
<property name="driver" value="com.mysql.jdbc.Driver"/>
</dataSource>

</environment>
</environments>
<!--路径映射-->
<mappers>
<mapper resource="mapper/sql.xml"/>
<package name="mapper"/>
</mappers>
</configuration>
```
**注意:** 这里有驼峰映射,别名映射,路径映射和路径映射。和以前的不同的是,我们这里做了sql语句的**共性抽取**,所以得加一个sql的路径映射 ` <mapper resource="mapper/sql.xml"/>`。

5. sql共性抽取文件
在类路径resources下的包mapper下创建一个sql.xml(因为我们sql是要写在映射文件中,自己本身也是映射文件,所以需要写在mapper下)。到要用的时候,在映射路径文件中需要用到这个sql语句的地方加入`
<include refid="mapper.sql.mySelectSql"></include>`。
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.sql">

<sql id="mySelectSql">
select emp_id,emp_name,emp_salary from t_emp
</sql>

</mapper>
```

> 共性抽取文件也可以不配置,这时候直接在映射文件中把要执行的语句重新编写就行了。

6. mapper接口
一共有七个方法,
```java
package mapper;

import org.apache.ibatis.annotations.Param;
import pojo.Employee;

import java.util.List;

public interface EmployeeMapper {

//根据员工的empId查询大于该empId的所有员工,如果empId为null,则查询全体员工
List<Employee> selectEmployeeListByEmpId(Integer empId);

/**
* 查询大于传入的empId并且工资大于传入的empSalary的员工集合,如果传入的empId为null,则不考虑empId条件
* 传入的empSalary为null则不考虑empSalary的条件
*/
List<Employee> selectEmployeeListByEmpIdAndEmpSalary(@Param("empId") Integer empId, @Param("empSalary") Double empSalary);

/**
* 根据empId更新员工信息,如果某个值为null,则不更新这个字段
*/
void updateEmployee(Employee employee);

/**
* 根据emp_id查询员工信息,如果0<emp_id<6,那么就查询所有大于该emp_id的员工,如果emp_id是大于6,那么就查询所有小于该emp_id的员工
* 如果是其它情况,则查询所有员工信息

*/
List<Employee> selectEmployeeList(Integer empId);

/**
* 添加员工信息
*/
void insertEmployee(Employee employee);

/**
* 批量添加员工集合
*/
void insertEmployeeList(@Param("employeeList") List<Employee> employeeList);

/**
* 根据员工的id集合查询员工集
*/
List<Employee> selectEmployeeListByEmpIdList(List<Integer> idList);
}
```
## if

目标:根据员工的empId查询大于该empId的所有员工,如果empId为null,则查询全体员工。

> Dao接口的方法为:
> ` List<Employee> selectEmployeeListByEmpId(Integer empId);`

**静态sql**:
```xml
<select id="selectEmployeeListByEmpId" resultType="Employee">

<include refid="mapper.sql.mySelectSql"></include> where emp_id>#{empId}

</select>
```
**动态sql**:
```xml
<select id="selectEmployeeListByEmpId" resultType="Employee">
<include refid="mapper.sql.mySelectSql"></include>
<if test="empId != null">
where emp_id>#{empId}
</if>
</select>
```
` <include refid="mapper.sql.mySelectSql"></include>`表示引用抽取出的sql片段,也可以直接写sql语句。如果是静态sql,当id为null时,查询出来的是空,动态sql则可以查出全部。**if标签里面有test属性名,作为判断语句。**

## where
目标:
- 查询大于传入的empId并且工资大于传入的empSalary的员工集合
- 如果传入的empId为null,则不考虑empId条件
* 传入的empSalary为null则不考虑empSalary的条件

> Dao接口方法:
>
> ` List<Employee> selectEmployeeListByEmpIdAndEmpSalary(@Param("empId") Integer empId, @Param("empSalary") Double empSalary);`
**用if标签的动态sql**:
```xml
<select id="selectEmployeeListByEmpIdAndEmpSalary" resultType="Employee">
<include refid="mapper.sql.mySelectSql"></include> where
<if test="empId != null">
emp_id>#{empId}
</if>
<if test="empSalary != null">
and emp_salary>#{empSalary}
</if>

```
这里可以看到,如果empSalary为空,那么sql语句为select * from t_emp where emp_id >#{empId},但是如果empId为空,那么sql语句为select * from t_emp where and emp_salary>#{empSalary},很明显这个是错的,if标签在这里就不适用了。所以我们用where标签,或者trim标签。

**where和if的动态sql**:
```xml
<select id="selectEmployeeListByEmpIdAndEmpSalary" resultType="Employee">
<include refid="mapper.sql.mySelectSql"></include>

<where>
<if test="empId != null">
emp_id>#{empId}
</if>
<if test="empSalary != null">
and emp_salary>#{empSalary}
</if>
</where>
</select>
```
> **where标签的作用:**
> 1. 在第一个条件之前自动添加WHERE关键字
> 2. 自动去掉第一个条件前的连接符(AND、OR等等)

## trim
trim是修建的意思,其实就是去头去尾,这里还是根据上面那个方法
**trim的动态sql**
```xml
<select id="selectEmployeeListByEmpIdAndEmpSalary" resultType="Employee">
<include refid="mapper.sql.mySelectSql"></include>
<trim prefix="WHERE" prefixOverrides="AND|OR">
<if test="empId != null">
emp_id>#{empId}
</if>

<if test="empSalary != null">
AND emp_salary>#{empSalary}
</if>
</trim>
</select>
```
> **trim标签:**
> - prefix:指定要动态添加的前缀
> - suffix属性:指定要动态添加的后缀
> - prefixOverrides:指定要动态去掉的前缀,使用“|”分隔有可能的多个值
> - suffixOverrides属性:指定要动态去掉的后缀,使用“|”分隔有可能的多个值

## set

目标:根据empId更新员工信息,如果某个值为null,则不更新这个字段

> Dao接口方法:
>` void updateEmployee(Employee employee);`
我们先用上面的trim标签来解决一下这个问题,
**trim的动态sql**:
```xml
<update id="updateEmployee" >
<trim prefix="set" prefixOverrides=",">
<if test="empName!=null">
emp_name=#{empName}
</if>
<if test="empSalary!=null">
, emp_salary=#{empSalary}
</if>
</trim>
where emp_id=#{empId}
</update>
```

**set的动态sql**

```xml
<update id="updateEmployee" >
update t_emp
<set >
<if test="empName!=null">
emp_name=#{empName}
</if>
<if test="empSalary!=null">
, emp_salary=#{empSalary}
</if>
</set>
```
可以看出
> **set标签的作用:**
> 1. 自动在要修改的第一个字段之前添加SET关键字
> 2. 去掉要修改的第一个字段前的连接符(,)

## choose、when、otherwise
目标:
- 根据emp_id查询员工信息,如果0<emp_id<6,那么就查询所有大于该emp_id的员工
- 如果emp_id是大于6,那么就查询所有小于该emp_id的员工
- 如果是其它情况,则查询所有员工信息

> Dao接口方法:
> ` List<Employee> selectEmployeeList(Integer empId);`

**动态sql**
```xml
<select id="selectEmployeeList" resultType="employee">

<include refid="mapper.sql.mySelectSql"></include> where
<choose>
<!--&lt;是<号的转义字符-->
<when test="empId>0 and empId&lt;6">
emp_id>#{empId}
</when>
<when test="empId>6">
emp_id&lt;#{empId}
</when>
<otherwise>
1==1
</otherwise>
</choose>

</select>
```
choose、when、otherwise
相当于if ... else if... else if ... else
- 如果某一个when的条件成立,则不会继续判断后续的when
- 如果所有的when都不成立,则会拼接otherwise标签中的内容

## foreach
目标1:批量添加员工信息

> Dao接口方法:
>
> void insertEmployeeList(@Param("employeeList") List<Employee> employeeList);


**动态sql**
```xml
<insert id="insertEmployeeList">
insert into t_emp(emp_name,emp_salary)values
<!--collection标签可以写list,collection,
或者自己自己定义参数名@Param("employeeList") List<Employee> employeeList-->
<foreach collection="employeeList" separator="," item="emp">
(#{emp.empName},#{emp.empSalary})
</foreach>
</insert>
```

目标2:根据多个id查询多个员工信息
> Dao接口
>
> List<Employee> selectEmployeeListByEmpIdList(List<Integer> idList);
**动态sql**
```xml
<select id="selectEmployeeListByEmpIdList" resultType="employee">
<include refid="mapper.sql.mySelectSql"></include>
<foreach collection="collection" item="id" separator="," open="where emp_id in (" close=")">
#{id}
</foreach>
</select>
```

**批量查询:foreach标签**

1. collection属性: 表示要遍历的对象,如果要遍历的参数使用**@Param**注解取名了就使用该名字,如果没有取名**List,或者collection。**
2. item属性: 表示遍历出来的元素,我们到时候要拼接SQL语句就得使用这个元素: 如果遍历出来的元素是POJO对象, 那么我们就通过 **#{遍历出来的元素.POJO的属性}** 获取数据;如果遍历出来的元素是简单类型的数据,那么我们就使用 **#{遍历出来的元素}** 获取这个简单类型数据
3. separator属性: 遍历出来的元素之间的分隔符
4. open属性: 在遍历出来的第一个元素之前添加前缀
5. close属性: 在遍历出来的最后一个元素之后添加后缀

四、mybatis动态sql的更多相关文章

  1. 【转载】 mybatis入门系列四之动态SQL

    mybatis 详解(五)------动态SQL 目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when, ...

  2. Mybatis动态SQL简单了解 Mybatis简介(四)

    动态SQL概况 MyBatis 的强大特性之一便是它的动态 SQL 在Java开发中经常遇到条件判断,比如: if(x>0){ //执行一些逻辑........ }   Mybatis应用中,S ...

  3. MyBatis 动态SQL(十二)

    动态条件查询 以下是我们数据库表 tb_user 的记录: 假设现在有一个需求,就是根据输入的用户年龄和性别,查询用户的记录信息.你可能会说,这太简单了,脑袋里立马蹦出如下的 SQL 语句: SELE ...

  4. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...

  5. 9.mybatis动态SQL标签的用法

    mybatis动态SQL标签的用法   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...

  6. 自己动手实现mybatis动态sql

    发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...

  7. Mybatis动态SQL单一基础类型参数用if标签

    Mybatis动态SQL单一基础类型参数用if标签时,test中应该用 _parameter,如: 1 2 3 4 5 6 <select id="selectByName" ...

  8. 超全MyBatis动态SQL详解!( 看完SQL爽多了)

    MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...

  9. mybatis原理分析学习记录,mybatis动态sql学习记录

    以下个人学习笔记,仅供参考,欢迎指正. MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转 ...

  10. mybatis 动态sql和参数

    mybatis 动态sql 名词解析 OGNL表达式 OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性, ...

随机推荐

  1. C#设计模式---观察者模式(Observer Pattern)

    一.目的 提供一种一对多的关系,当主题发生变化时候,可以通知所有关联的对象. 二.定义 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个主题对象在状态发生变化时,会通 ...

  2. 【java虚拟机】类加载机制

    作者:平凡希 原文地址:https://www.cnblogs.com/xiaoxi/p/6959615.html 一.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中, ...

  3. SpringCloud 商品架构例子(一)

    架构演进和分布式系统基础知识 1.传统架构演进到分布式架构 简介:讲解单机应用和分布式应用架构演进基础知识 高可用 LVS+keepalive 单体应用: 集群: 微服务架构: 1.单体应用: 开发速 ...

  4. unity优化 — UGUI纹理格式的选择

    首次界面打开加载的资源(如 贴图)会被缓存在内存中,再次打开界面由于内存中已有了资源 所以会更快.如何让首次打开界面会更快呢? 图片是否进行了有效的压缩.Android 平台下不带透明通道 优先使用E ...

  5. request请求《一》

    1. request对象通常用来接收客户端提交到服务端的数据,如:在servlet或者action中可以用request.getParameter()的方法获取获取参数内容: 2. requestSc ...

  6. GoLang设计模式02 - 工厂模式

    工厂模式是一种创建型模式,也是最常用的设计模式之一.调用方通过工厂产出并获取对象,可以不必关注对象创建的细节和构建逻辑. 在工厂模式下,调用方只和工厂进行交互,并告诉工厂具体获取哪种类型的对象.工厂负 ...

  7. Qt中的Q_PROPERTY宏浅析

    1. Q_PROPERTY Qt提供了一个绝妙的属性系统,Q_PROPERTY()是一个宏,用来在一个类中声明一个属性property,由于该宏是qt特有的,需要用moc进行编译,故必须继承于QObj ...

  8. 利用 Spring Boot 中的 @ConfigurationProperties,优雅绑定配置参数

    使用 @Value("${property}") 注释注入配置属性有时会很麻烦,尤其是当你使用多个属性或你的数据是分层的时候. Spring Boot 引入了一个可替换的方案 -- ...

  9. Python - 面向对象编程 - 魔术方法(双下划线方法)

    什么是魔术方法 在Python中,所有以 __ 双下划线包起来的方法,都统称为 Magic Method 魔术方法,也叫双下划线方法 有哪些重要的魔术方法? __new__ https://www.c ...

  10. Intel® QAT加速卡之加密、哈希操作流程和示例

    Intel QAT 加密API介绍 文章主要讲述了Intel QAT 加密API接口的说明,以及多种应用场景下的使用方法. 文章目录 Intel QAT 加密API介绍 1. 概述 1.1 会话(se ...