MyBatis 動(dòng)態(tài)SQL全面詳解
前面mysql都是通過靜態(tài)sql進(jìn)行查詢的,但是如果業(yè)務(wù)復(fù)雜的時(shí)候,我們會(huì)遇到引號(hào)問題,或者多一個(gè)空格,這就使得sql代碼編寫錯(cuò)誤了,所以為了解決這個(gè)問題,我們有了動(dòng)態(tài)sql。
Mybatis框架的動(dòng)態(tài)SQL技術(shù)是一種根據(jù)特定條件動(dòng)態(tài)拼裝SQL語(yǔ)句的功能,它存在的意義是為了解決拼接SQL語(yǔ)句字符串時(shí)的痛點(diǎn)問題。具體是通過標(biāo)簽來(lái)實(shí)現(xiàn)的。
動(dòng)態(tài)sql
1.先看一下模塊目錄結(jié)構(gòu)
在類路徑的resources下的mapper包下創(chuàng)建sql.xml文件(共性抽?。?/p>

2.物理建模和邏輯建模
這里省略物理建模步驟,要求數(shù)據(jù)庫(kù)的表與pojo類要對(duì)應(yīng)。
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復(fù)制到類路徑resouces下,另外我們引入依賴后的pom.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測(cè)試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL驅(qū)動(dòng) -->
<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 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>
<!--環(huán)境配置-->
<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語(yǔ)句的共性抽取,所以得加一個(gè)sql的路徑映射 <mapper resource="mapper/sql.xml"/>。
5.sql共性抽取文件
在類路徑resources下的包mapper下創(chuàng)建一個(gè)sql.xml(因?yàn)槲覀僺ql是要寫在映射文件中,自己本身也是映射文件,所以需要寫在mapper下)。到要用的時(shí)候,在映射路徑文件中需要用到這個(gè)sql語(yǔ)句的地方加入 <include refid="mapper.sql.mySelectSql"></include>。
<?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>
共性抽取文件也可以不配置,這時(shí)候直接在映射文件中把要執(zhí)行的語(yǔ)句重新編寫就行了。
6.mapper接口
一共有七個(gè)方法
package mapper;
import org.apache.ibatis.annotations.Param;
import pojo.Employee;
import java.util.List;
public interface EmployeeMapper {
//根據(jù)員工的empId查詢大于該empId的所有員工,如果empId為null,則查詢?nèi)w員工
List<Employee> selectEmployeeListByEmpId(Integer empId);
/**
* 查詢大于傳入的empId并且工資大于傳入的empSalary的員工集合,如果傳入的empId為null,則不考慮empId條件
* 傳入的empSalary為null則不考慮empSalary的條件
*/
List<Employee> selectEmployeeListByEmpIdAndEmpSalary(@Param("empId") Integer empId, @Param("empSalary") Double empSalary);
/**
* 根據(jù)empId更新員工信息,如果某個(gè)值為null,則不更新這個(gè)字段
*/
void updateEmployee(Employee employee);
/**
* 根據(jù)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);
/**
* 根據(jù)員工的id集合查詢員工集
*/
List<Employee> selectEmployeeListByEmpIdList(List<Integer> idList);
}
if
目標(biāo):根據(jù)員工的empId查詢大于該empId的所有員工,如果empId為null,則查詢?nèi)w員工。
Dao接口的方法為:List<Employee> selectEmployeeListByEmpId(Integer empId);
靜態(tài)sql:
<select id="selectEmployeeListByEmpId" resultType="Employee">
<include refid="mapper.sql.mySelectSql"></include> where emp_id>#{empId}
</select>
動(dòng)態(tài)sql:
<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語(yǔ)句。如果是靜態(tài)sql,當(dāng)id為null時(shí),查詢出來(lái)的是空,動(dòng)態(tài)sql則可以查出全部。if標(biāo)簽里面有test屬性名,作為判斷語(yǔ)句。
where
目標(biāo):
- 查詢大于傳入的empId并且工資大于傳入的empSalary的員工集合
- 如果傳入的empId為null,則不考慮empId條件
- 傳入的empSalary為null則不考慮empSalary的條件
Dao接口方法:
List<Employee> selectEmployeeListByEmpIdAndEmpSalary(@Param("empId") Integer empId, @Param("empSalary") Double empSalary);
用if標(biāo)簽的動(dòng)態(tài)sql:
<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語(yǔ)句為select * from t_emp where emp_id >#{empId},但是如果empId為空,那么sql語(yǔ)句為select * from t_emp where and emp_salary>#{empSalary},很明顯這個(gè)是錯(cuò)的,if標(biāo)簽在這里就不適用了。所以我們用where標(biāo)簽,或者trim標(biāo)簽。
where和if的動(dòng)態(tài)sql:
<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標(biāo)簽的作用:
- 在第一個(gè)條件之前自動(dòng)添加WHERE關(guān)鍵字
- 自動(dòng)去掉第一個(gè)條件前的連接符(AND、OR等等)
trim
trim是修建的意思,其實(shí)就是去頭去尾,這里還是根據(jù)上面那個(gè)方法
trim的動(dòng)態(tài)sql
<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標(biāo)簽:
- prefix:指定要?jiǎng)討B(tài)添加的前綴
- suffix屬性:指定要?jiǎng)討B(tài)添加的后綴
- prefixOverrides:指定要?jiǎng)討B(tài)去掉的前綴,使用“|”分隔有可能的多個(gè)值
- suffixOverrides屬性:指定要?jiǎng)討B(tài)去掉的后綴,使用“|”分隔有可能的多個(gè)值
set
目標(biāo):根據(jù)empId更新員工信息,如果某個(gè)值為null,則不更新這個(gè)字段
Dao接口方法:void updateEmployee(Employee employee);
我們先用上面的trim標(biāo)簽來(lái)解決一下這個(gè)問題,
trim的動(dòng)態(tài)sql:
<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的動(dòng)態(tài)sql
<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標(biāo)簽的作用:
- 自動(dòng)在要修改的第一個(gè)字段之前添加SET關(guān)鍵字
- 去掉要修改的第一個(gè)字段前的連接符(,)
choose、when、otherwise
目標(biāo):
- 根據(jù)emp_id查詢員工信息,如果0<emp_id<6,那么就查詢所有大于該emp_id的員工
- 如果emp_id是大于6,那么就查詢所有小于該emp_id的員工
- 如果是其它情況,則查詢所有員工信息
Dao接口方法:List<Employee> selectEmployeeList(Integer empId);
動(dòng)態(tài)sql
<select id="selectEmployeeList" resultType="employee">
<include refid="mapper.sql.mySelectSql"></include> where
<choose>
<!--<是<號(hào)的轉(zhuǎn)義字符-->
<when test="empId>0 and empId<6">
emp_id>#{empId}
</when>
<when test="empId>6">
emp_id<#{empId}
</when>
<otherwise>
1==1
</otherwise>
</choose>
</select>
choose、when、otherwise
相當(dāng)于if ... else if... else if ... else
- 如果某一個(gè)when的條件成立,則不會(huì)繼續(xù)判斷后續(xù)的when
- 如果所有的when都不成立,則會(huì)拼接otherwise標(biāo)簽中的內(nèi)容
foreach
目標(biāo)1:批量添加員工信息
Dao接口方法:
void insertEmployeeList(@Param("employeeList") List employeeList);
1.動(dòng)態(tài)sql
<insert id="insertEmployeeList">
insert into t_emp(emp_name,emp_salary)values
<!--collection標(biāo)簽可以寫list,collection,
或者自己自己定義參數(shù)名@Param("employeeList") List<Employee> employeeList-->
<foreach collection="employeeList" separator="," item="emp">
(#{emp.empName},#{emp.empSalary})
</foreach>
</insert>
目標(biāo)2:根據(jù)多個(gè)id查詢多個(gè)員工信息
Dao接口
List selectEmployeeListByEmpIdList(List idList);
2.動(dòng)態(tài)sql
<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標(biāo)簽
- collection屬性: 表示要遍歷的對(duì)象,如果要遍歷的參數(shù)使用@Param注解取名了就使用該名字,如果沒有取名List,或者collection。
- item屬性: 表示遍歷出來(lái)的元素,我們到時(shí)候要拼接SQL語(yǔ)句就得使用這個(gè)元素: 如果遍歷出來(lái)的元素是POJO對(duì)象, 那么我們就通過 #{遍歷出來(lái)的元素.POJO的屬性} 獲取數(shù)據(jù);如果遍歷出來(lái)的元素是簡(jiǎn)單類型的數(shù)據(jù),那么我們就使用 #{遍歷出來(lái)的元素} 獲取這個(gè)簡(jiǎn)單類型數(shù)據(jù)
- separator屬性: 遍歷出來(lái)的元素之間的分隔符
- open屬性: 在遍歷出來(lái)的第一個(gè)元素之前添加前綴
- close屬性: 在遍歷出來(lái)的最后一個(gè)元素之后添加后綴
測(cè)試程序
import mapper.EmployeeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import pojo.Employee;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Test {
private EmployeeMapper employeeMapper;
private InputStream is;
private SqlSession sqlSession;
@Before
public void init() throws Exception{
//目標(biāo):獲取EmployeeMapper接口的代理對(duì)象,并且使用該對(duì)象調(diào)用selectEmployee(1)方法,然后返回Employee對(duì)象
//1. 將全局配置文件轉(zhuǎn)成字節(jié)輸入流
is = Resources.getResourceAsStream("mybatisConfig.xml");
//2. 創(chuàng)建SqlSessionFactoryBuilder對(duì)象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//3. 使用構(gòu)建者模式創(chuàng)建SqlSessionFactory對(duì)象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//4. 使用工廠模式創(chuàng)建一個(gè)SqlSession對(duì)象
sqlSession = sqlSessionFactory.openSession();
//5. 使用動(dòng)態(tài)代理模式,創(chuàng)建EmployeeMapper接口的代理對(duì)象
employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
}
@After
public void after() throws Exception{
//提交事務(wù)!!!
sqlSession.commit();
//7. 關(guān)閉資源
is.close();
sqlSession.close();
}
@org.junit.Test
public void testSelectEmployeeListByEmpId(){
System.out.println(employeeMapper.selectEmployeeListByEmpId(null));
}
@org.junit.Test
public void testSelectEmployeeListByEmpIdAndEmpSalary(){
System.out.println(employeeMapper.selectEmployeeListByEmpIdAndEmpSalary(2, 300d));
}
@org.junit.Test
public void testUpdateEmployee(){
Employee employee = new Employee(3,"celia", 9000d);
employeeMapper.updateEmployee(employee);
}
@org.junit.Test
public void testSelectEmployeeList(){
System.out.println(employeeMapper.selectEmployeeList(7));
}
@org.junit.Test
public void testInsertEmployee(){
employeeMapper.insertEmployee(new Employee(null,"tom",300d));
}
@org.junit.Test
public void testInsertEmployeeList(){
List<Employee> employeeList = new ArrayList<>();
for (int i = 11; i <=20 ; i++) {
employeeList.add(new Employee(null,"aobama"+i,2000d));
}
employeeMapper.insertEmployeeList(employeeList);
}
@org.junit.Test
public void testSelectEmployeeListByEmpIdList(){
List<Integer> idList = new ArrayList<>();
idList.add(23);
idList.add(33);
idList.add(32);
idList.add(21);
idList.add(22);
System.out.println(employeeMapper.selectEmployeeListByEmpIdList(idList));
}
}
到此這篇關(guān)于MyBatis 動(dòng)態(tài)SQL全面詳解的文章就介紹到這了,更多相關(guān)MyBatis 動(dòng)態(tài)SQL內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信