欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis 動(dòng)態(tài)SQL全面詳解

 更新時(shí)間:2021年09月30日 11:54:23   作者:素衣黛眉  
MyBatis 的強(qiáng)大特性之一便是它的動(dòng)態(tài) SQL。如果你有使用 JDBC 或其他類似框架的經(jīng)驗(yàn),你就能體會(huì)到根據(jù)不同條件拼接 SQL 語句有多么痛苦。拼接的時(shí)候要確保不能忘了必要的空格,還要注意省掉列名列表最后的逗號(hào)。利用動(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語句的功能,它存在的意義是為了解決拼接SQL語句字符串時(shí)的痛點(diǎn)問題。具體是通過標(biāo)簽來實(shí)現(xiàn)的。

動(dòng)態(tài)sql

1.先看一下模塊目錄結(jié)構(gòu)

在類路徑的resources下的mapper包下創(chuàng)建sql.xml文件(共性抽?。?/p>

2.物理建模和邏輯建模

這里省略物理建模步驟,要求數(shù)據(jù)庫的表與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語句的共性抽取,所以得加一個(gè)sql的路徑映射 <mapper resource="mapper/sql.xml"/>。

5.sql共性抽取文件

在類路徑resources下的包mapper下創(chuàng)建一個(gè)sql.xml(因?yàn)槲覀僺ql是要寫在映射文件中,自己本身也是映射文件,所以需要寫在mapper下)。到要用的時(shí)候,在映射路徑文件中需要用到這個(gè)sql語句的地方加入 <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í)行的語句重新編寫就行了。

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語句。如果是靜態(tài)sql,當(dāng)id為null時(shí),查詢出來的是空,動(dòng)態(tài)sql則可以查出全部。if標(biāo)簽里面有test屬性名,作為判斷語句。

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語句為select * from t_emp where emp_id >#{empId},但是如果empId為空,那么sql語句為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)簽來解決一下這個(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>
    <!--&lt;是<號(hào)的轉(zhuǎn)義字符-->
        <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
相當(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)簽

  1. collection屬性: 表示要遍歷的對(duì)象,如果要遍歷的參數(shù)使用@Param注解取名了就使用該名字,如果沒有取名List,或者collection。
  2. item屬性: 表示遍歷出來的元素,我們到時(shí)候要拼接SQL語句就得使用這個(gè)元素: 如果遍歷出來的元素是POJO對(duì)象, 那么我們就通過 #{遍歷出來的元素.POJO的屬性} 獲取數(shù)據(jù);如果遍歷出來的元素是簡單類型的數(shù)據(jù),那么我們就使用 #{遍歷出來的元素} 獲取這個(gè)簡單類型數(shù)據(jù)
  3. separator屬性: 遍歷出來的元素之間的分隔符
  4. open屬性: 在遍歷出來的第一個(gè)元素之前添加前綴
  5. close屬性: 在遍歷出來的最后一個(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 教您修復(fù)mysql數(shù)據(jù)庫的方法

    教您修復(fù)mysql數(shù)據(jù)庫的方法

    你可能在使用MySQL過程中,各種意外導(dǎo)致數(shù)據(jù)庫表的損壞,而且這些數(shù)據(jù)往往是最新的數(shù)據(jù),通常不可能在備份數(shù)據(jù)中找到。本章將繼上篇文章中檢查出表的問題后,告訴你如何修復(fù)表
    2014-05-05
  • mybatis mysql delete in操作只能刪除第一條數(shù)據(jù)的方法

    mybatis mysql delete in操作只能刪除第一條數(shù)據(jù)的方法

    這篇文章主要介紹了mybatis mysql delete in操作只能刪除第一條數(shù)據(jù)的問題及解決方法,需要的朋友可以參考下
    2018-09-09
  • MySQL日志文件詳解

    MySQL日志文件詳解

    這篇文章主要介紹了MySQL日志文件詳解,本文分別講解了錯(cuò)誤日志、二進(jìn)制日志、通用查詢?nèi)罩?、慢查詢?nèi)罩尽nnodb的在線redo日志、更新日志等日志類型和作用介紹,需要的朋友可以參考下
    2015-07-07
  • mysql 5.7.25 壓縮版安裝配置方法圖文教程

    mysql 5.7.25 壓縮版安裝配置方法圖文教程

    這篇文章主要為大家詳細(xì)介紹了mysql 5.7.25 壓縮版安裝配置方法圖文教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Mysql雙主配置的詳細(xì)步驟

    Mysql雙主配置的詳細(xì)步驟

    雙主可以在一定程度上保證主庫的高可用,在一臺(tái)主庫down掉之后,可以在極短的時(shí)間內(nèi)切換到另一臺(tái)主庫上,減少了主從同步給線上主庫帶來的壓力,這篇文章主要給大家介紹了關(guān)于Mysql雙主配置的詳細(xì)步驟,需要的朋友可以參考下
    2022-06-06
  • sql format()函數(shù)的用法及簡單實(shí)例

    sql format()函數(shù)的用法及簡單實(shí)例

    下面小編就為大家?guī)硪黄猻ql format函數(shù)()的用法及簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • 清空mysql 查詢緩存的可行方法

    清空mysql 查詢緩存的可行方法

    mysql對(duì)同一條sql進(jìn)行了緩存,在第二次運(yùn)行時(shí), 瞬間就完成了,若要清除緩存,可通過下面的方法來實(shí)現(xiàn)
    2014-07-07
  • MySql各種查詢方式詳解

    MySql各種查詢方式詳解

    如果是做數(shù)據(jù)分析,MySQL里面最重要、最常用的就是數(shù)據(jù)查詢,數(shù)據(jù)查詢不只是簡單查詢數(shù)據(jù)庫中存儲(chǔ)的數(shù)據(jù),還要根據(jù)需求對(duì)數(shù)據(jù)進(jìn)行篩選、聚合,以及確定數(shù)據(jù)以什么樣的格式進(jìn)行顯示。MySQL提供了強(qiáng)大、靈活的語句和函數(shù)來實(shí)現(xiàn)查詢的操作
    2022-07-07
  • MySQL學(xué)習(xí)之?dāng)?shù)據(jù)庫表五大約束詳解小白篇

    MySQL學(xué)習(xí)之?dāng)?shù)據(jù)庫表五大約束詳解小白篇

    本篇文章非常適合MySQl初學(xué)者,主要講解了MySQL數(shù)據(jù)庫的五大約束及約束概念和分類,有需要的朋友可以借鑒參考下,希望可以有所幫助
    2021-09-09
  • Mysql 1864 主從錯(cuò)誤解決方法

    Mysql 1864 主從錯(cuò)誤解決方法

    本文給大家?guī)砹薓ysql 1864 主從錯(cuò)誤解決方法,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08

最新評(píng)論