知识点:主要介绍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. win下自动sftp脚本定时下载文件

    缘起一个BA与客户交流的软件.但因为数据不能通过系统直连的方式进行获取. 对方只提供每天一份全量数据到指定文件夹下,我方自动通过sftp的方式去拉取. 一个只有简单几行的操作,想必肯定是不可能需写程序 ...

  2. linux UIO

    UIO(linux Userspace I/O子系统)用户空间设备驱动I/O技术介绍(由www.169it.com搜集整理) UIO(Userspace I/O)是运行在用户空间的I/O技术.Linu ...

  3. Android实现按两次back键退出应用

    重写onKeyDown()方法 System.currentTimeMillis():该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0 ...

  4. Linux命令(基础1)

    一  命令的基本构成 (PS:Linux发行版本命令大概有200多个,熟练掌握个百八的就行了,其余的有个大概了解) 命令体 选项 参数(对象) ls -l /var 1.1参数:文件 文件类型: d ...

  5. mxGraph画图区域使用鼠标滚轮实现放大/缩小

    // 重写鼠标滚轮事件 mxEvent.addMouseWheelListener = function (funct) { } // 添加初次载入事件 window.onload = functio ...

  6. 003-shell 传递参数

    一.概述 可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 二.实例 以下实例我们向 ...

  7. python 学习笔记(十四)有依赖关系的接口开发

    接口开发中存在很多有依赖关系的接口,例如:BBS中发帖的时候就需要进行校验用户是否登录,那么此时发帖的接口就与用户登录接口有依赖关系.在发帖时就需要先获取用户的session,与当前登录用户进行校验对 ...

  8. PAT 1078 Hashing[一般][二次探查法]

    1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...

  9. MySQL,sqlalchemy

    Mariadb 数据库是一堆表的集合 主键 外键 索引 安装: Centos7 [root@host]# mysqladmin -u root password "new_password& ...

  10. session和token的区别

    session的使用方式是客户端cookie里存id,服务端session存用户数据,客户端访问服务端的时候,根据id找用户数据 而token一般翻译成令牌,一般是用于验证表明身份的数据或是别的口令数 ...