知识点:主要介绍mybatis中,动态sql中的if,where,trim,set,foreach的用法

自学谷粒学院mybatis学习视频,参考mybatis官方文档

java包:log4j.jar

mybatis-3.4.1jar

mysql-connector-java-5.1.37-bin.jar

实体类:

Employee.java类

package com.hand.mybatis.bean;
public class Employee {
    
    private Integer eId;
    private String eName;
    private Integer gender;
    private String email;
    private Department dept;
    
    public Employee() {
        super();
    }
    
    public Employee(Integer eId,String eName, Integer gender, String email) {
        super();
        this.eId=eId;
        this.eName = eName;
        this.gender = gender;
        this.email = email;
    }
  
    public Employee(Integer eId, String eName, Integer gender, String email, Department dept) {
        super();
        this.eId = eId;
        this.eName = eName;
        this.gender = gender;
        this.email = email;
        this.dept = dept;
    }

public Integer geteId() {
        return eId;
    }
    public void seteId(Integer eId) {
        this.eId = eId;
    }
   
    public String getEName() {
        return eName;
    }
    public void setEname(String ename) {
        this.eName = ename;
    }
    public Integer getGender() {
        return gender;
    }
    public void setGender(Integer gender) {
        this.gender = gender;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Department getDept() {
        return dept;
    }

public void setDept(Department dept) {
        this.dept = dept;
    }
    @Override
    public String toString() {
        return "Employee [eId=" + eId + ", ename=" + eName + ", gender=" + gender + ", email=" + email + "]";
    }
  
}

Department.java实体类

package com.hand.mybatis.bean;
import java.util.List;

public class Department {
    private Integer id;
    private String departName;
    private List<Employee> empList;
  
    public Department() {
        super();
    }
    public Department(Integer id) {
        super();
        this.id = id;
    }

public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getDepartName() {
        return departName;
    }
    public void setDepartName(String departName) {
        this.departName = departName;
    }
    
    public List<Employee> getEmpList() {
        return empList;
    }
    public void setEmpList(List<Employee> empList) {
        this.empList = empList;
    }
    @Override
    public String toString() {
        return "Department [id=" + id + ", departName=" + departName + "]";
    }
}

EmployeeMapperDynamicSQL.java   mapper接口

package com.hand.mybatis.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;

import com.hand.mybatis.bean.Employee;
public interface EmployeeMapperDynamicSQL {
    
    //使用if
    List<Employee> getEmpsByconditionIf(Employee employee);
    
    //使用trim()
    List<Employee> getEmpsByconditionTrim(Employee employee);
    
    //使用choose
    List<Employee> getEmpsByconditionChoose(Employee employee);
    
    //跟新字段(set)
    int updateEmp(Employee employee);
    
    //根据list集合条件,查询集合(foreach遍历list集合条件)
    List<Employee> getEmpsByConditionForeach(@Param("idlist") List<Integer> idlist);
        
    //批量保存(foreach插入多条数据)
    int addEmpsBatch(@Param("emps") List<Employee> emps);
   
}

EmployeeMapperDynamicSQL.xml  sql映射文件

<?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="com.hand.mybatis.dao.EmployeeMapperDynamicSQL">

<!-- • if
    • choose (when, otherwise):分支选择,swtich-case
           如果带了eid就用eid查询,如果带了eName就用eName查询,只会进入其中一个
    • trim 字符串截取(where(封装查询条件), set(封装修改条件))
    • foreach -->
 
 
   <!-- //使用if -->//一般if用在sql条件的多字段拼接,if判断字段不为null,"",等情况下
   <!--  public List<Employee> getEmpsByconditionIf(Employee employee); -->
   <!-- 查询员工,要求,携带了那个字段查询条件就带上那个字段的值 -->
   <select id="getEmpsByconditionIf" resultType="com.hand.mybatis.bean.Employee">
        select * from emp
        <where>
        <!-- where 1=1  --><!-- test,判断表达式(OGNL)
                                                        从参数中取值判断,遇见特殊符号去写转义字符
                      w3c:  ISO 8859-1
                      &&,&amp;&amp;
                      "" &quot;&quot;-->
        <if test="eId!=null">
             eid=#{eId}
        </if>
        <if test="eName!=null  &amp;&amp; eName!= &quot;&quot;">
             and ename like #{eName}   //and一般放在字段前面,《where》标签不会报错
        </if>
        <if test="email!=null and email.trim()!=&quot;&quot;"> //一些字符如果出错,可以ws3=>HTML ISO-8859-1参考手册里查找对应的字符,如mybatis里写
             and email=#{email}                                                            大小与号(><)出错,改成<(&lt;)>(&gt;)则不会报错
        </if>
        <!--  ognl会进行字符串与数字的转换判断“0”==0 -->
        <if test="gender==0 or gender==1">
              and gender=#{gender}
        </if>
        </where>   
   </select>
   
   
   <!-- 使用trim -->
   <!-- List<Employee> getEmpsByconditionTrim(Employee employee); -->
   <select id="getEmpsByconditionTrim" resultType="com.hand.mybatis.bean.Employee">
        select * from emp
        <!--后面多出来的and或者or where 标签不能解决
        trim标签中是整个字符串拼接后的结果
            prefix="":给拼接后的整个字符串加一个前缀
            prefixOverrides="":前缀覆盖,去掉整个字符串前面多余的字符
            suffix="":后缀 suffix给拼接后的整个字符串加一个后缀
            suffixOverrides="" 后缀覆盖,去掉整个字符串后面多余的字符-->
         
         <!-- 自定义字符串的截取规则 -->   
        <trim prefix="where" suffixOverrides="and">//这里意思是在条件最前加where,去掉每个if条件里的字段后面多余的and,就不会报错
        <if test="eId!=null">
             eid=#{eId} and
        </if>
        <if test="eName!=null  &amp;&amp; eName!= &quot;&quot;">
             ename like #{eName} and
        </if>
        <if test="email!=null and email.trim()!=&quot;&quot;">
            email=#{email}  and
        </if>
        <!--  ognl会进行字符串与数字的转换判断“0”==0 -->
        <if test="gender==0 or gender==1">
             gender=#{gender}
        </if>
        </trim>
        
   </select>
   
   
    <!-- //使用choose
    List<Employee> getEmpsByconditionChoose(Employee employee); -->
    <select id="getEmpsByconditionChoose" resultType="com.hand.mybatis.bean.Employee">
        select * from emp
        <where>
              <!--  如果带了eid就用eid查询,如果带了eName就用eName查询,只会进入其中一个 -->
              <choose>
                   <when test="eId!=null">//只会选择一条路径,若同时有两个字段传入,则优先选择前面的字段
                      eid=#{eId}
                   </when>
                   <when test="eName!=null">
                      ename=#{eName}
                   </when>
                   <when test="gender!=null">
                      gender=#{gender}
                   </when>
                   <when test="email!=null">
                      email=#{email}
                   </when>
                   <otherwise>
                         1=1  //当所有字段都为空时,查询原表中所有记录
                   </otherwise>
              </choose>
        </where>
    </select>
    
    
    <!-- //跟新字段   传那个字段跟新那个字段(使用set)
    void updateEmp(Employee employee); -->
    <update id="updateEmp">
         UPDATE emp
         
         <set>  <!--  //方法一:使用set标签去掉多余的,号 -->
         <if test="eName!=null">
           ename=#{eName},
         </if>
         <if test="gender!=null">
          gender=#{gender},
         </if>
         <if test="email">
          email=#{email}
         </if>
         </set>
         
         <!-- 方法二:使用trim标签去掉多余的,号
         <trim prefix="set" suffixOverrides=",">
         <if test="eName!=null">
           ename=#{eName},
         </if>
         <if test="gender!=null">
          gender=#{gender},
         </if>
         <if test="email">
          email=#{email}
          </if>
          </trim>  -->
         WHERE eid=#{eId}
    </update>
    
    
     <!-- //根据list集合条件,查询集合(foreach遍历list集合条件)
     List<Employee> getEmpsByConditionForeach(List<Integer> idlist); -->
     <select id="getEmpsByConditionForeach" resultType="com.hand.mybatis.bean.Employee">
      <!--  select * from emp where eid in(101,102,103) 方法一:in(1,2,3)--> //原始传入多个id的情况,用in(1,2,3)
       select * from emp where eid in
       <!--
          collection:指定要遍历的集合
            list类型的参数会特殊处理封装在map中,map的key就是list
          item:将当前遍历的元素赋值给指定的变量
          separator:每个元素之间的分隔符
          open:遍历出所有结果拼接一个开始的字符
          close:遍历出所有结果拼接一个结束的字符
          index:索引,遍历list的时候,index就是索引,item就是当前值
                                                     遍历map的时候index表示就是map的key,item就是map的值
          #{变量名}:每个元素之间的分隔符
        -->
       <foreach collection="idlist" item="itemid" separator=","
       open="(" close=")">
         #{itemid}
       </foreach>
     </select>
    
    
     <!-- 批量保存(foreach插入多条数据两种方法)
       int addEmpsBatch(@Param("emps") List<Employee> emps); -->
     <!-- MySQL下批量保存,可以foreach遍历 mysql支持values(),(),()语法 --> //推荐使用
     <insert id="addEmpsBatch">
      INSERT INTO emp(ename,gender,email,did)
      VALUES
      <foreach collection="emps" item="emp" separator=",">
      (#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
      </foreach>
     </insert>
    
     <!-- 这种方式需要数据库连接属性allowMutiQueries=true的支持 -->  //在jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
    <!--  <insert id="addEmpsBatch">                                                                                                                 后加上allowMultiQueries=true
       <foreach collection="emps" item="emp" separator=";">                                                                        表示可以多次执行insert into语句,中间;不会错
         INSERT INTO emp(ename,gender,email,did)
         VALUES(#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
      </foreach>
     </insert> -->

</mapper>

代码:https://github.com/shuaishuaihand/mybatis.git

Mybatis动态SQL——if,where,trim,choose,set.foreach的用法的更多相关文章

  1. MyBatis从入门到精通(第4章):MyBatis动态SQL【if、choose 和 where、set、trim】

    (第4章):MyBatis动态SQL[if.choose 和 where.set.trim] MyBatis 的强大特性之一便是它的动态 SQL.MyBatis 3.4.6版本采用了功能强大的OGNL ...

  2. mybatis动态sql中的trim标签的使用

    trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码: 1. select * from user <trim prefix="WHERE" p ...

  3. mybatis动态sql中的trim标签的使用(转)

    trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码: 1. select * from user <trim prefix="WHERE" p ...

  4. mybatis中sql标签、where标签、foreach标签用法

    <sql id="query_user_where"> <!-- 如果 userQueryVo中传入查询条件,再进行sql拼接--> <!-- tes ...

  5. mybatis 动态SQL .2

    目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,otherwise) 语句 5.动态SQL:tri ...

  6. Mybatis 动态 sql 有什么用?执行原理?有哪些动态 sql?

    Mybatis 动态 sql 可以在 Xml 映射文件内,以标签的形式编写动态 sql,执行原理 是根据表达式的值 完成逻辑判断并动态拼接 sql 的功能. Mybatis 提供了 9 种动态 sql ...

  7. mybatis动态sql以及分页

    1.mybatis动态sql 2.模糊查询 3.查询返回结果集的处理 4.分页查询 5.特殊字符处理 1.mybatis动态sql If.trim.foreach If 标签判断某一字段是否为空 &l ...

  8. MyBatis动态SQL之一使用 if 标签和 choose标签

    bootstrap react https://segmentfault.com/a/1190000010383464 xml 中 < 转义 to thi tha <if test=&qu ...

  9. MyBatis基础入门《十九》动态SQL(set,trim)

    MyBatis基础入门<十九>动态SQL(set,trim) 描述: 1. 问题 : 更新用户表数据时,若某个参数为null时,会导致更新错误 2. 分析: 正确结果: 若某个参数为nul ...

随机推荐

  1. inode file 结构

    inode位图(inode Bitmap) 和块位图类似,本身占一个块,其中每个bit表示一个inode是否空闲可用. inode表(inode Table) 我们知道,一个文件除了数据需要存储之外, ...

  2. weblogic中eclipse远程调试

    1. weblogic 配置文件修改 修改文件: weblogic/weblogic103/user_projects/domains/xxxx/bin/setDomainEnv.sh(windows ...

  3. 在eclipse中,Python项目遇到:…… from appium import webdriver ImportError: No module named appium

    1) Traceback (most recent call last):   File "D:\python workspace\src\p_test01\__init__.py" ...

  4. JPG PNG GIF 的优缺点

    pg/jpeg:优点:体积比png小,打开速度比png快缺点:属于有损压缩,每次保存都会产生数据损失(jpeg artifacts), 故不适合用于多次编辑保存.压缩率过高图像会失真PS:网络上最最常 ...

  5. 批处理delims分割时遇到的问题。。

    今天写了个将文件每行按逗号分割并取第六行的批处理.但是结果不对.看图一目了然. for 循环的/f 后面的参数是这样的 然后文件的内容是这样的 亮点是倒数第二行..其实6才是第六列的值.其他行第六列都 ...

  6. linux安装以及相关配置

    计算机操作系统简介 操作系统是什么 操作系统的内核是什么 两种操作系统用户界面 安装Linux操作系统的准备工作 LINUX发行版(CENTOS.RHEL.FEDORA.UBUNTU.SUSE) RH ...

  7. Python的数据类型和常用方法大全

    数据类型 一.数字 整形int x=10 #x=int(10) print(id(x),type(x),x) 浮点型float salary=3.1 #salary=float(3.1) print( ...

  8. Google:移动端搜索正式上线移动友好标签

    Google移动端搜索正式上线“Mobile-Friendly”(移动友好)标签,并宣布如果卖家网站上没有移动友好标签或者错误地设置移动友好标签,那么卖家的网站将被降级. Google表示目前常见的智 ...

  9. PKU 3041 Asteroids 最小点覆盖(最大匹配模板题)

    题目大意:给你一个N*N的矩阵, 里面有K个星球, 我们可以让武器攻击矩阵的一行或者一列来使得这个星球被击碎, 现在问你最少需要几个这种武器才能把所有的星球击碎? 解题思路:关键是建模构图 把每一行当 ...

  10. 高并发下hystrix熔断超时及concurrent.RejectedExecutionException: Rejected command because thread-pool queueSize is at rejection threshold问题

    我的一个项目并发200+,观察服务器的日志发现出现了大量的熔断超时偶尔会闪现出RejectedExecutionException: Rejected command because thread-p ...