MyBatis的使用八(动态SQL)
本主要讲述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)的更多相关文章
- MyBatis学习总结_11_MyBatis动态Sql语句
MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...
- MyBatis:学习笔记(4)——动态SQL
MyBatis:学习笔记(4)——动态SQL
- SSM框架之Mybatis(6)动态SQL
Mybatis(6)动态SQL 1.动态SQL 出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了 1.1.if标签 我们根据实体类的不 ...
- Spring mybatis源码篇章-动态SQL节点源码深入
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...
- Mybatis系列全解(八):Mybatis的9大动态SQL标签你知道几个?提前致女神!
封面:洛小汐 作者:潘潘 2021年,仰望天空,脚踏实地. 这算是春节后首篇 Mybatis 文了~ 跨了个年感觉写了有半个世纪 ... 借着女神节 ヾ(◍°∇°◍)ノ゙ 提前祝男神女神们越靓越富越嗨 ...
- 深入浅出Mybatis系列 强大的动态SQL
上篇文章<深入浅出Mybatis系列(八)---mapper映射文件配置之select.resultMap>简单介绍了mybatis的查询,至此,CRUD都已讲完.本文将介绍mybatis ...
- mybatis入门基础(五)----动态SQL
一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...
- (转)Mybatis高级映射、动态SQL及获得自增主键
原文:http://www.cnblogs.com/edwinchen/p/4105278.html?utm_source=tuicool&utm_medium=referral 一.动态SQ ...
- Mybatis高级映射、动态SQL及获得自增主键
一.动态SQL 相信大家在用mybatis操作数据库时时都会碰到一个问题,假如现在我们有一个关于作者的list authorList,需要根据authorList里已有的作者信息在数据库中查询相应作者 ...
- MyBatis 源码分析——动态SQL语句
有几年开发经验的程序员应该都有暗骂过原生的SQL语句吧.因为他们不能一句就搞定一个业务,往往还要通过代码来拼接相关的SQL语句.相信大家会理解SQL里面的永真(1=1),永假(1=2)的意义吧.所以m ...
随机推荐
- 对于python中“FileNotFoundError: [Errno 2] No such file or directory”的解决办法
在我们使用vscode运行Python代码时遇到的情况 一.出现原因:这里是由于Vscode中,python里的路径是相对与工作目录来进行定位的.所以在多级目录情况下,若不设置绝对路径,往往找不到相应 ...
- C#多线程之线程基础篇
目录 一.概念 二.原理 硬件结构 运行时 三.基础 创建与启动 传递参数 前台/后台线程 异常处理 中断与中止 中断(Interrupt) 中止(Abort) 协作取消模式 四.异步编程模式 异步编 ...
- Java开发学习(四十一)----MyBatisPlus标准数据层(增删查改分页)开发
一.标准CRUD使用 对于标准的CRUD功能都有哪些以及MyBatisPlus都提供了哪些方法可以使用呢? 我们先来看张图: 1.1 环境准备 这里用的环境就是Java开发学习(四十)----MyBa ...
- (C++) C++ 中 shared_ptr weak_ptr
shared_ptr std::shared_ptr<int> sp1 = new int(); // shared count = 1, weak count = 0 std::shar ...
- Mysql5.6.44版本安装及基本配置
内容概要 存储数据的演变史 数据库软件应用史 MySQL简介 MySQL下载及安装 MySQL配置 存储数据的演变史 1.文本文件: 文件路径不固定,并且数据格式不统一 2.软件开发目录规范: 规定了 ...
- 将现有源码添加进repo管理
将现有源码添加进repo管理 适用于大型项内无源码管理(git/repo)的源码 前言 公司在进行一些项目的开发时,从供应商原厂给的code内没有包含任何源码管理的文件.需要多人协同开发,但由于项 ...
- ajax 跨域请求jsonp
最近一段时间为这个事情走了不少弯路,现将成功经验分享,避免后来人再绕远路,不过也是第一次使用中间有什么问题大家可以留言探讨. ajax的跨域请求jsonp主要运用于不同系统的交互,一个系统想通过该种方 ...
- 长度最小子数组-LeetCode209 滑动窗口
力扣:https://leetcode.cn/problems/minimum-size-subarray-sum/ 题目 给定一个含有 n 个正整数的数组和一个正整数 target .找出该数组中满 ...
- MyBatis02:流程分析、注解、代理dao实现CRUD、参数深入、传统DAO、配置
今日内容 回顾 mybatis的自定义.分析和环境搭建 完善基于注解的mybatis mybatis的curd(基于代理dao的方式)※ mybatis的参数深入及结果集的深入 mybatis中基于传 ...
- ORM数据增删改查 django请求生命周期 django路由层 反向解析
目录 可视化界面之数据增删改查 补充 1.建表 2.数据展示功能 3.数据添加功能 4.数据编辑功能 5.数据删除功能 django请求生命周期流程图 crsf wsgirel 与 uwsgi ngi ...