前言

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

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

动态sql

1.先看一下模块目录结构

在类路径的resources下的mapper包下创建sql.xml文件(共性抽取)

2.物理建模和逻辑建模

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

package pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublic class Employee {    private Integer empId;    private String empName;    private Double empSalary;}
<?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>

5.sql共性抽取文件

在类路径resources下的包mapper下创建一个sql.xml(因为我们sql是要写在映射文件中,自己本身也是映射文件,所以需要写在mapper下)。到要用的时候,在映射路径文件中需要用到这个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>

6.mapper接口

一共有七个方法

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);}

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

静态sql:

<select id="selectEmployeeListByEmpId" resultType="Employee">    <include refid="mapper.sql.mySelectSql"></include> where emp_id>#{empId}</select>

where

目标:

  • 查询大于传入的empId并且工资大于传入的empSalary的员工集合
  • 如果传入的empId为null,则不考虑empId条件
  • 传入的empSalary为null则不考虑empSalary的条件

Dao接口方法:

List<Employee> selectEmployeeListByEmpIdAndEmpSalary(@Param("empId") Integer empId, @Param("empSalary") Double empSalary);

用if标签的动态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和if的动态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关键字
  • 自动去掉第一个条件前的连接符(AND、OR等等)

trim

trim是修建的意思,其实就是去头去尾,这里还是根据上面那个方法

trim的动态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>

Dao接口方法:
void updateEmployee(Employee employee);
我们先用上面的trim标签来解决一下这个问题,

trim的动态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标签的作用:

  • 自动在要修改的第一个字段之前添加SET关键字
  • 去掉要修改的第一个字段前的连接符(,)

choose、when、otherwise

目标:

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

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

动态sql

<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>
  • 如果某一个when的条件成立,则不会继续判断后续的when
  • 如果所有的when都不成立,则会拼接otherwise标签中的内容

foreach

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

Dao接口方法:

void insertEmployeeList(@Param("employeeList") List employeeList);

1.动态sql

<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>

Dao接口

List selectEmployeeListByEmpIdList(List idList);

2.动态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>

更多相关文章

  1. MySQL系列多表连接查询92及99语法示例详解教程
  2. Android(安卓)- Manifest 文件 详解
  3. Android的Handler机制详解3_Looper.looper()不会卡死主线程
  4. Android最全面试题库(转)
  5. Selector、shape详解(一)
  6. android2.2资源文件详解4--menu文件夹下的菜单定义
  7. Android发送短信方法实例详解
  8. Android(安卓)读取资源文件实例详解
  9. 详解Android中的屏幕方向

随机推荐

  1. Zookeeper官网文档—第三章 2.Zookeeper
  2. Java Mail发送邮件
  3. php 调用java 接口
  4. Java虚拟机:类的初始化
  5. 当我运行.jar时,在java.library中会得到一
  6. 朋友要去外包,很兴奋的同我炫耀薪水,我想我
  7. 我希望我知道的七个JavaScript技巧
  8. 导入证书后的Java Keytool错误,“Keytool
  9. Cassandra Java驱动程序错误 - 所有主机
  10. Java NIO简单介绍(一)