本主要讲述mybatis处理动态sql语句

一. 问题引入

  前端展示的数据表格中,查询条件可能不止一个,如何将用户输入的多个查询条件,拼接到sql语句中呢?

  DynamicMapper接口声明如下

public interface DynamicMapper {

    // 动态查询员工信息
List<Employee> selectEmpDynamic(@Param("emp") Employee emp);
}

  如何在DynamicMapper.xml文件中,编写sql语句,使其动态查询呢?

二. 动态SQL查询

  1. <if>标签

  DynamicMapper.xml文件声明如下

    <!--// 动态查询员工信息
List<Employee> selectEmpDynamic(Employee emp);-->
<select id="selectEmpDynamic" resultType="Employee">
select * from t_emp where 1=1
<if test="emp.empName != null and emp.empName != '' ">
and emp_name = #{emp.empName}
</if>
<if test="emp.age != null and emp.age != '' ">
and age = #{emp.age}
</if>
<if test="emp.gender != null and emp.gender != '' ">
and gender = #{emp.gender}
</if>
</select>

  测试test

    @Test
// 测试动态查询
public void test01(){
SqlSession sqlSession = SQLSessionUtils.getSqlSession();
DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class);
Employee emp = new Employee(null, "", 23, "");
List<Employee> list = mapper.selectEmpDynamic(emp);
System.out.println(list);
}

  运行结果如下

DEBUG 02-05 13:49:28,390 ==>  Preparing: select * from t_emp where 1=1 and age = ? (BaseJdbcLogger.java:137)
DEBUG 02-05 13:49:28,432 ==> Parameters: 23(Integer) (BaseJdbcLogger.java:137)
DEBUG 02-05 13:49:28,461 <== Total: 1 (BaseJdbcLogger.java:137)
[Employee{empId=1, empName='张三', age=23, gender='男', dept=null}]

  注意:单独使用 < if > 标签时,需要在where后面添加恒成立的条件,例如 1=1,之后在< if >标签中的test写判断条件

  2. <where> + <if>标签

  DynamicMapper.xml文件声明如下

    <select id="selectEmpDynamicTwo" resultType="Employee">
select * from t_emp
<where>
<if test="emp.empName != null and emp.empName != '' ">
and emp_name = #{emp.empName}
</if>
<if test="emp.age != null and emp.age != '' ">
and age = #{emp.age} and
</if>
<if test="emp.gender != null and emp.gender != '' ">
and gender = #{emp.gender}
</if>
</where>
</select>

  测试test1

    @Test
// 测试动态查询
public void test01(){
SqlSession sqlSession = SQLSessionUtils.getSqlSession();
DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class);
Employee emp = new Employee(null, "张三", 23, "");
List<Employee> list = mapper.selectEmpDynamic(emp);
System.out.println(list);
}

  test1 运行结果如下

DEBUG 02-05 14:00:47,159 ==>  Preparing: select * from t_emp WHERE emp_name = ? and age = ? (BaseJdbcLogger.java:137)
DEBUG 02-05 14:00:47,190 ==> Parameters: 张三(String), 23(Integer) (BaseJdbcLogger.java:137)
DEBUG 02-05 14:00:47,211 <== Total: 1 (BaseJdbcLogger.java:137)
[Employee{empId=1, empName='张三', age=23, gender='男', dept=null}]

  注意:<where>标签只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句。而且,若语句的开头为“AND”或“OR”,<where>标签也会将它们去除。

  但是当DynamicMapper.xml声明如下

    <select id="selectEmpDynamic" resultType="Employee">
select * from t_emp
<where>
<if test="emp.empName != null and emp.empName != '' ">
emp_name = #{emp.empName} and
</if>
<if test="emp.age != null and emp.age != '' ">
age = #{emp.age} and
</if>
<if test="emp.gender != null and emp.gender != '' ">
gender = #{emp.gender}
</if>
</where>
</select>

  测试test2

    @Test
// 测试动态查询
public void test01(){
SqlSession sqlSession = SQLSessionUtils.getSqlSession();
DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class);
Employee emp = new Employee(null, "张三", 23, "");
List<Employee> list = mapper.selectEmpDynamic(emp);
System.out.println(list);
}

  test2 运行结果如下

DEBUG 02-05 14:08:29,175 ==>  Preparing: select * from t_emp WHERE emp_name = ? and age = ? and (BaseJdbcLogger.java:137)
DEBUG 02-05 14:08:29,206 ==> Parameters: 张三(String), 23(Integer) (BaseJdbcLogger.java:137)

  sql语句出现错误,即<where>标签无法去除SQL子句后面的and或者or。

  3. <trim> + <if> 标签

  DynamicMapper.xml文件声明如下

     <select id="selectEmpDynamic" resultType="Employee">
select * from t_emp
<!--prefix="where" 前缀添加where
suffix="where" 后缀添加where
prefixOverrides="and" 前缀删除and
suffixOverrides="and" 后缀删除and-->
<trim prefix="where" suffixOverrides="and">
<if test="emp.empName != null and emp.empName != '' ">
emp_name = #{emp.empName} and
</if>
<if test="emp.age != null and emp.age != '' ">
age = #{emp.age} and
</if>
<if test="emp.gender != null and emp.gender != '' ">
gender = #{emp.gender}
</if>
</trim>
</select>

  注意:<trim>标签的功能比<where>标签的功能更加丰富

  test2运行结果如下

DEBUG 02-05 14:14:51,893 ==>  Preparing: select * from t_emp where emp_name = ? and age = ? (BaseJdbcLogger.java:137)
DEBUG 02-05 14:14:51,924 ==> Parameters: 张三(String), 23(Integer) (BaseJdbcLogger.java:137)
DEBUG 02-05 14:14:51,943 <== Total: 1 (BaseJdbcLogger.java:137)
[Employee{empId=1, empName='张三', age=23, gender='男', dept=null}]

  完美解决and在SQL子句后面的问题。

三. 批量增加【删除】

  1. 批量增加

  DynamicMapper接口声明如下

public interface DynamicMapper {

    // 批量添加emp对象
int insertEmpList(@Param("emps") List<Employee> emps);
}

  DynamicMapper.xml文件声明如下

    <!--// 批量添加emp对象
int insertEmpList(@Param("emps") List<Employee> emps);-->
<insert id="insertEmpList" >
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{emp.empName},#{emp.age},#{emp.gender},null)
</foreach>
</insert>

  2. 批量删除

  DynamicMapper接口声明如下

public interface DynamicMapper {

    // 批量删除emp对象
int deleteEmpList(@Param("empIds") Integer[] empIds);
}

  DynamicMapper.xml文件声明如下

    <!--// 批量删除emp对象-->
<!--int deleteEmpList(@Param("empIds") Integer[] empIds);-->
<!--方式1: -->
<delete id="deleteEmpListOne" >
delete from t_emp
where emp_id in
(
<foreach collection="empIds" item="empId" separator=",">
#{empId}
</foreach>
)
</delete> <!--方式2: -->
<delete id="deleteEmpListTwo" >
delete from t_emp
where emp_id in
<foreach collection="empIds" item="empId" separator="," open="(" close=")">
#{empId}
</foreach>
</delete> <!--方式3: -->
<delete id="deleteEmpList" >
delete from t_emp
where
<foreach collection="empIds" item="empId" separator="or">
emp_id = #{empId}
</foreach>
</delete>

  注意:< foreach >标签的使用,

    collecion:传入的集合【数组】名;

    item:集合【数组】中元素的类型;

    separator:以指定字符串为分隔符;

    open:以指定字符串开始,close:以指定字符串结尾。

    

MyBatis的使用八(动态SQL)的更多相关文章

  1. MyBatis学习总结_11_MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  2. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  3. SSM框架之Mybatis(6)动态SQL

    Mybatis(6)动态SQL 1.动态SQL 出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了 1.1.if标签 我们根据实体类的不 ...

  4. Spring mybatis源码篇章-动态SQL节点源码深入

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...

  5. Mybatis系列全解(八):Mybatis的9大动态SQL标签你知道几个?提前致女神!

    封面:洛小汐 作者:潘潘 2021年,仰望天空,脚踏实地. 这算是春节后首篇 Mybatis 文了~ 跨了个年感觉写了有半个世纪 ... 借着女神节 ヾ(◍°∇°◍)ノ゙ 提前祝男神女神们越靓越富越嗨 ...

  6. 深入浅出Mybatis系列 强大的动态SQL

    上篇文章<深入浅出Mybatis系列(八)---mapper映射文件配置之select.resultMap>简单介绍了mybatis的查询,至此,CRUD都已讲完.本文将介绍mybatis ...

  7. mybatis入门基础(五)----动态SQL

    一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...

  8. (转)Mybatis高级映射、动态SQL及获得自增主键

    原文:http://www.cnblogs.com/edwinchen/p/4105278.html?utm_source=tuicool&utm_medium=referral 一.动态SQ ...

  9. Mybatis高级映射、动态SQL及获得自增主键

    一.动态SQL 相信大家在用mybatis操作数据库时时都会碰到一个问题,假如现在我们有一个关于作者的list authorList,需要根据authorList里已有的作者信息在数据库中查询相应作者 ...

  10. MyBatis 源码分析——动态SQL语句

    有几年开发经验的程序员应该都有暗骂过原生的SQL语句吧.因为他们不能一句就搞定一个业务,往往还要通过代码来拼接相关的SQL语句.相信大家会理解SQL里面的永真(1=1),永假(1=2)的意义吧.所以m ...

随机推荐

  1. 小菜鸡学习---<正则表达式学习笔记2>

    正则表达式学习笔记2 一.修饰符 前面我们学习的都是用于匹配的基本的关键的一些表达式符号,现在我们来学习修饰符.修饰符不写在正则表达式里,修饰符位于表达式之外,比如/runoob/g,这个最后的g就是 ...

  2. 【Zulip】邮件系统配置

    通过docker-compose(docker-zulip)部署Zulip实例时需要配置邮件系统 SETTING_ZULIP_ADMINISTRATOR: '...@qq.com' SETTING_E ...

  3. Python 学习思路 思维导图 Xmind

    如果需要,请在评论区留下邮箱,我看到后会一次发送.

  4. GAMES101课程 作业6 源代码概览

    GAMES101课程 作业6 源代码概览 Written by PiscesAlpaca(双鱼座羊驼) 一.概述 本篇将从main函数为出发点,按照各cpp文件中函数的调用顺序和层级嵌套关系,简单分析 ...

  5. Go语言书籍推荐

    附录: 其他书籍的介绍:https://github.com/golang/go/wiki/Books Go程序设计语言 (圣经) 作者 艾伦 A.A.多诺万 Kernighan 下载地址 中文版PD ...

  6. [排序算法] 双向冒泡排序 (C++)

    前言 本文章是建立在冒泡排序的基础上写的,如还有对 冒泡排序 不了解的童鞋,可以看看这里哦~ 冒泡排序 C++ 双向冒泡排序原理 双向冒泡排序 的基本思想与 冒泡排序还是一样的.冒泡排序 每次将相邻的 ...

  7. go get 报错:dial tcp 142.251.43.17:443: i/o timeout

    自动下载 go env -w GO111MODULE=on 设置环境为国内代理 go env -w GOPROXY=https://goproxy.cn,direct 注:go 版本需要支持 mod

  8. 在Java Web中setContentType与setCharacterEncoding中设置字符编码格式的区别

    在Java Web中setContentType与setCharacterEncoding中设置字符编码格式的区别 通用解释 setCharacterEncoding只是设置字符的编码方式 setCo ...

  9. 使用repo上传代码

    前言~ repo是一款安卓用于管理源码的工具,由python实现,基于git工具 本文介绍了repo的常用使用方式. 一,下载代码 1. repo init 初始化命令 此命令常用选项就那几个,此处取 ...

  10. 【书籍知识回顾与总结-2022】Java语言重点知识-多线程编程、流式编程

    一.多线程编程 二.流式编程 1.目的 简化集合和数组的操作 注意:每个流只能使用一次 2.获取流的方式 (1)单列集合:stream方法 KeySet()/values()/EntrySet() ( ...