mybatis入门基础(五)----动态SQL
阅读目录
- 一:动态SQL
- 二:SQL片段
- 三:foreach
一:动态SQL
1.1.定义
mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。
1.2.案例需求
用户信息综合查询列表这个statement的定义使用动态sql,对查询条件进行判断,如果输入参数不为空才进行查询拼接。
1.3.UserMapper.xml
1 <!-- 用户信息综合查询 2 #{userCustom.sex}:取出pojo包装对象中性别值 3 ${userCustom.username}:取出pojo对象中用户名称 4 --> 5 <select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo" 6 resultType="com.mybatis.entity.UserCustom"> 7 select * from t_user 8 <!-- 动态sql查询:where可以自动去掉第一个and --> 9 <where> 10 <if test="userCustom!=null"> 11 <if test="userCustom.sex!=null and userCustom.sex!='' "> 12 and sex=#{userCustom.sex} 13 </if> 14 <if test="userCustom.username!=null and userCustom.username!='' "> 15 and username=#{userCustom.username} 16 </if> 17 </if> 18 </where> 19 <!-- where sex=#{userCustom.sex} and username LIKE '%${userCustom.username}%' --> 20 </select>
1.4.测试代码
1 @Test 2 public void testFindUserList() { 3 SqlSession sqlSession = sqlSessionFactory.openSession(); 4 //创造查询条件 5 UserQueryVo userQueryVo = new UserQueryVo(); 6 UserCustom userCustom = new UserCustom(); 7 // userCustom.setSex("2"); 8 //这里使用动态sql,如果不设置某个值,条件不会拼接sql中 9 userCustom.setUsername("小"); 10 userQueryVo.setUserCustom(userCustom); 11 // 创建Usermapper对象,mybatis自动生成mapper代理对象 12 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 13 List<UserCustom>list=mapper.findUserList(userQueryVo); 14 //测试动态sql,属性的非空判断测试 15 // List<UserCustom>list=mapper.findUserList(null); 16 System.out.println(list); 17 sqlSession.commit(); 18 sqlSession.close(); 19 }
二:SQL片段
2.1.需求
将上边的动态sql判断代码抽取出来,组成一个sql片段,其它的statement中就可以引用sql片段,方便开发。
2.2.定义sql片段
1 <!-- 定义sql片段,Id是唯一标识 2 建议:是基于单表来定义sql片段,这样的话sql片段的可重用性才高,在sql片段中不要包含where 3 --> 4 <sql id="query_user_where" > 5 <if test="userCustom!=null"> 6 <if test="userCustom.sex!=null and userCustom.sex!='' "> 7 and sex=#{userCustom.sex} 8 </if> 9 <if test="userCustom.username!=null and userCustom.username!='' "> 10 and username=#{userCustom.username} 11 </if> 12 </if> 13 </sql>
2.3.在mapper.xml中定义的statement中引用sql片段
1 <!-- 用户信息综合查询 2 #{userCustom.sex}:取出pojo包装对象中性别值 3 ${userCustom.username}:取出pojo对象中用户名称 4 --> 5 <select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo" 6 resultType="com.mybatis.entity.UserCustom"> 7 select * from t_user 8 <!-- 动态sql查询:where可以自动去掉第一个and --> 9 <where> 10 <!-- 引用sql片段的id,如果refid指定的不在本mapper.xml中,需要前边加namespace --> 11 <include refid="query_user_where"></include> 12 <!-- 这里可以引用其它的sql片段 --> 13 </where> 14 </select>
三:foreach
作用:向sql传递数组或者list,mybatis使用foreach解析
在用户查询列表和查询总数的statement中增加多个id输入查询。
3.1.需求
sql语句如下:
两种方法:
SELECT * FROM t_user WHERE id=1 OR id=10 OR id=16
SELECT * FROM t_user WHERE id IN(1,10,16)
3.2.在输入参数包装类型中添加List<Integer> ids 传入多个id
1 package com.mybatis.entity; 2 3 import java.util.List; 4 5 /** 6 * 7 * @ClassName: UserQueryVo 8 * @Description: TODO(包装类型) 9 * @author warcaft 10 * 11 */ 12 public class UserQueryVo { 13 14 public List<Integer> ids; 15 16 public List<Integer> getIds() { 17 return ids; 18 } 19 20 public void setIds(List<Integer> ids) { 21 this.ids = ids; 22 } 23 }
3.3.mapper.xml代码
1 <!-- 实现下边的sql拼接 2 select * from t_user where id=1 OR id=2 OR id=3 3 --> 4 <select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo" 5 resultType="com.mybatis.entity.User"> 6 select * from t_user 7 <where> 8 <if test="ids!=null"> 9 <!-- 使用foreach遍历ids 10 collection:指定输入对象的集合属性 11 item:每个遍历生成对象中 12 open:开始遍历时拼接的串 13 close:技术遍历时拼接的串 14 separator:遍历的两个对象中需要拼接的串 15 --> 16 <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or"> 17 id=#{user_id} 18 </foreach> 19 </if> 20 </where> 21 </select>
select * from t_user where id in(1,2,3)的mapper.xml配置
1 <select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo" 2 resultType="com.mybatis.entity.User"> 3 select * from t_user 4 <where> 5 <if test="ids!=null"> 6 <!-- 7 使用foreach遍历ids 8 collection:指定输入对象的集合属性 9 item:每个遍历生成对象中 10 open:开始遍历时拼接的串 11 close:技术遍历时拼接的串 12 separator:遍历的两个对象中需要拼接的串 13 --> 14 <!-- 实现“ select * from t_user where id in(1,2,3)”拼接 --> 15 <foreach collection="ids" item="user_id" open="AND id in (" close=")" separator=","> 16 id=#{user_id} 17 </foreach> 18 </if> 19 </where> 20 </select>
userMapper.java代码
1 public interface UserMapper { 2 //ids查询用户数据 3 public List<User> findUserByIds(UserQueryVo userQueryVo); 4 }
Junit测试代码
1 @Test 2 public void findUserByIdsTest() { 3 SqlSession sqlSession = sqlSessionFactory.openSession(); 4 // 创建Usermapper对象,mybatis自动生成mapper代理对象 5 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 6 //创造查询条件 7 UserQueryVo userQueryVo = new UserQueryVo(); 8 //传入多个id 9 List<Integer> ids=new ArrayList<Integer>(); 10 ids.add(1); 11 ids.add(2); 12 ids.add(3); 13 //将ids通过userQueryVo传入statement中 14 userQueryVo.setIds(ids); 15 //调用userMapper的代码 16 List<UserCustom> userList= mapper.findUserList(userQueryVo); 17 System.out.println(userList); 18 sqlSession.close(); 19 }
mybatis入门基础(五)----动态SQL相关推荐
- Mybatis入门:3(动态sql)
动态sql语句 if标签 基本使用 一.在ProductDao接口中创建一个查询方法findByType import com.domain.Product;import java.util.List ...
- MyBatis入门基础3 ---代理
代理:给目标对象提供一个代理对象,并且由代理对象提供对目标对象的引用 代理的作用:功能的增强.控制访问:通过代理对象间接访问目标对象 ,以防直接访问目标对象产生不必要的风险. 首先提出一个问题:代理类 ...
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
阅读目录 一:原始dao开发方法 二:mapper代理方法(只需要mapper接口,相当于dao接口) 承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisS ...
- MyBatis:学习笔记(4)——动态SQL
MyBatis:学习笔记(4)--动态SQL 转载于:https://www.cnblogs.com/MrSaver/p/7453949.html
- 【Java从0到架构师】MyBatis - 增删改、动态 SQL
MyBatis - 增删改.动态 SQL 动态 SQL if 标签 where 标签 sql 标签 foreach 标签 添加 主键设置 批量添加 - 利用 foreach 标签 更新 删除 批量删除 ...
- Java神鬼莫测之MyBatis注解开发之动态SQL语句(六)
1.Mybatis注解开发之动态SQL语句 背景:使用mybatis的注解开发动态Sql会比较麻烦, 很不方便, 所以不太推荐使用,该文章以查询作为案例,演示动态sql语句. 注意:Mybatis的动 ...
- Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql
mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. mybatis提供各种标签方法实现动态拼接sql. 1. if&where 1.2 需求 用户信 ...
- mybatis入门(五)之Java API
转载自 mybatis Java API Java API 既然你已经知道如何配置 MyBatis 和创建映射文件,你就已经准备好来提升技能了.MyBatis 的 Java API 就是你收获你所 ...
- Mybatis(三)动态SQL
Mybatis动态SQL Mybatis的强大特征之一就是动态SQL.Mybatis采用功能强大的基于OGNL表达式来淘汰其他大部分元素.使用mybatis提供的各种标签方法实现动态拼接sql.动态传 ...
最新文章
- Android MarsDaemon实现进程及Service常驻
- XHProf的安装和使用(PHP性能测试神器)
- TabelDiff实用工具
- Java枚举:小小enum,优雅而干净
- 归并排序相关题目(待编辑)
- oracle中dbms_并发和由于DBMS中的并发导致的问题
- html button跳转页面_Html
- 深度解析HashMap底层实现架构
- .NET中操作SQLite
- python的xlwt库的作用_Python:使用第三方库xlwt来写Excel
- 37. 解决线程问题方式二(同步函数)
- Atitit 图片 验证码生成attilax总结
- Python opencv 简单的车牌识别 —— 简单学习
- 如何把软件Origin切换变成中文显示?
- .NET/C#大型项目研发必备(12)--使用MQ消息队列
- JS 逆向之 Hook,吃着火锅唱着歌,突然就被麻匪劫了!
- 74HC595原理图”中“ RCLK SRCLK SRCLR”是什么意思
- python识别重影验证码_python+opencv实现移动侦测(帧差法)
- 微信小程序开发——设置默认图片、错误加载图片
- 假面舞会狂欢节·圆桌 | 当Thinker遇上Artist