mybatis系列-08-动态sql
8.1 什么是动态sql
mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。
8.2 需求
用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。
对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。
8.3 mapper.xml
<!-- 动态sql -->
<select id="findUserListDynamic" parameterType="UserQueryVo" resultType="User">
SELECT * FROM USER
<!-- where 可以自动去掉第一个and -->
<where>
<if test="userCustom != null">
<if test="userCustom.sex != null and userCustom.sex != ''"></if>
and sex = #{userCustom.sex}
</if>
<if test="userCustom.username != null and userCustom.username != ''">
and username LIKE '%${userCustom.username}%'
</if>
</where>
</select>
<!-- 动态sql -->
<select id="findUserCountDynamic" parameterType="UserQueryVo" resultType="int">
SELECT count(*) FROM USER
<!-- where 可以自动去掉第一个and -->
<where>
<if test="userCustom != null">
<if test="userCustom.sex != null and userCustom.sex != ''"></if>
and sex = #{userCustom.sex}
</if>
<if test="userCustom.username != null and userCustom.username != ''">
and username LIKE '%${userCustom.username}%'
</if>
</where>
</select>
8.4 测试代码
//08 使用动态sql 如果sex为空,count应该为0,否则正常查询
@Test
public void testfindUserCountDynamic() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //创建包装对象,设置查询条件
UserQueryVo userQueryVo = new UserQueryVo();
User user = new User();
//user.setSex("1"); //如果注释,count应该为0
user.setUsername("张");
userQueryVo.setUserCustom(user); //调用userMapper的方法
int count = userMapper.findUserCountDynamic(userQueryVo);
System.out.println(count);
} // 08 动态sql 如果sex为空,list不应该有成员
@Test
public void testfindUserListDynamic(){ SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //创建包装对象,设置查询条件
UserQueryVo userQueryVo = new UserQueryVo(); User user = new User();
user.setId(24);
//user.setSex("1");
user.setUsername("张");
userQueryVo.setUserCustom(user); //调用userMapper的方法
List<User> list = userMapper.findUserListDynamic(userQueryVo);
System.out.println(list.size());
}
8.5 sql片段
8.5.1 需求
将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。
方便程序员进行开发。
8.5.2 定义sql片段
<!-- 定义sql片段
id:sql片段的唯 一标识
经验:是基于单表来定义sql片段,这样话这个sql片段可重用性才高
在sql片段中不要包括 where
-->
<sql id="query_user_where">
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex = #{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username LIKE '%${userCustom.username}%'
</if>
</if>
</sql>
8.5.3 引用sql片段
<!-- 引用sql片段 -->
<select id="findUserListPart" parameterType="UserQueryVo" resultType="User">
SELECT * FROM USER
<where>
<!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前边加namespace -->
<include refid="query_user_where"></include>
<!-- 在这里还要引用其它的sql片段 -->
</where>
</select> <!-- 引用sql片段 -->
<select id="findUserCountPart" parameterType="UserQueryVo" resultType="int">
SELECT count(*) FROM USER
<where>
<!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前边加namespace -->
<include refid="query_user_where"></include>
<!-- 在这里还要引用其它的sql片段 -->
</where>
</select>
测试:和动态sql测试结果一样
8.6 foreach
向sql传递数组或List,mybatis使用foreach解析
8.6.1 需求
在用户查询列表和查询总数的statement中增加多个id输入查询。
sql语句如下:
两种方法:
SELECT * FROM USER WHERE id=1 OR id=10 OR id=16
SELECT * FROM USER WHERE id IN(1,10,16)
8.6.2 在输入参数类型中添加List<Integer> ids传入多个id
public class UserQueryVo {
//传入多个id
private List<Integer> ids;
8.6.3 修改mapper.xml
WHERE id=1 OR id=10 OR id=16
在查询条件中,查询条件定义成一个sql片段,需要修改刚才的sql片段。
<if test="ids!=null">
<!-- 使用 foreach遍历传入ids
collection:指定输入 对象中集合属性
item:每个遍历生成对象中
open:开始遍历时拼接的串
close:结束遍历时拼接的串
separator:遍历的两个对象中需要拼接的串
-->
<!-- 使用实现下边的sql拼接:
AND (id=1 OR id=10 OR id=16)
-->
<foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
<!-- 每个遍历需要拼接的串 -->
id=#{user_id}
</foreach>
</if>
8.6.4 测试代码
// 08 动态sql 测试foreach
@Test
public void testfindUserListPartForeach(){ SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //创建包装对象,设置查询条件
UserQueryVo userQueryVo = new UserQueryVo();
User userCustom = new User();
//由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中
//userCustom.setSex("1");
userCustom.setUsername("小明"); //传入多个id
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(10);
ids.add(16); //将ids通过userQueryVo传入statement中
userQueryVo.setIds(ids);
userQueryVo.setUserCustom(userCustom); //调用userMapper的方法
List<User> list = userMapper.findUserListPart(userQueryVo); System.out.println(list.size());
}
8.6.5 另外一个实现
<!-- 实现 “ and id IN(1,10,16)”拼接 -->
<foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">
<!--每个遍历需要拼接的串 -->
#{user_id}
</foreach>
mybatis系列-08-动态sql的更多相关文章
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- Mybatis入门之动态sql
Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...
- mybatis 详解------动态SQL
mybatis 详解------动态SQL 目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...
- mybatis中的动态SQL
在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...
- Mybatis映射文件动态SQL语句-01
因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...
- 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)
动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...
- MyBatis注解配置动态SQL
MySQL创建表 DROP TABLE IF EXISTS `tb_employee`; CREATE TABLE `tb_employee` ( `id` int(11) NOT NULL AUTO ...
- mybatis框架(5)---动态sql
那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误.Mybatis的动态S ...
- MyBatis进阶使用——动态SQL
MyBatis的强大特性之一就是它的动态SQL.如果你有使用JDBC或者其他类似框架的经验,你一定会体会到根据不同条件拼接SQL语句的痛苦.然而利用动态SQL这一特性可以彻底摆脱这一痛苦 MyBati ...
随机推荐
- 老韩思考:一个卖豆腐的能转行IT吗? 你的卖点在哪里?
前言: 我带过的学生很多,各行各业都有,泰牛程序员招生消息放出去后,还有一个在菜市场上卖豆腐的也看我的视频教程,决定转换IT行业,我想,北大毕业的可以卖猪肉,那么卖豆腐的为什么就不能从事IT行业呢?那 ...
- 一个酷炫的,基于HTML5,Jquery和Css的全屏焦点图特效,兼容各种浏览器
基于HTML5和CSS的焦点图特效,梅花图案的背景很有中国特色,而且还会动哦,效果超炫,推荐下载! 演示图 html代码 <!DOCTYPE html PUBLIC "-//W3C// ...
- 87. Scramble String
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- C++:运算符重载函数
5.运算符重载 5.1 在类外定义的运算符重载函数 C++为运算符重载提供了一种方法,即在运行运算符重载时,必须定义一个运算符重载函数,其名字为operator,后随一个要重载的运算符.例如,要重载& ...
- RestTemplateIntegrationTests
摘录RestTemplate的集成测试类/* 2. * Copyright 2002-2010 the original author or authors. 3. * 4. * L ...
- Flex 国际化(flex Localize)
先说编译到主程序中去的方法: 1.创建资源文件夹 譬如可以在src文件夹下创建Locale文件夹,然后在此文件夹再次创建每个地区的资源文件夹,譬如de_DE,zh_CN. 然后分别创建后缀名为.pro ...
- sql server 读取表结构
SELECT 表名 then d.name else '' end, 字段序号=a.colorder, 主键 FROM sysobjects where xtype='PK' and name in ...
- [Codeforces137C]History(排序,水题)
题目链接:http://codeforces.com/contest/137/problem/C 题意:给n对数,分别是一个事件的起始和终止时间.问被有几个事件被其他事件包含. 思路:先排序,按照起始 ...
- Git show-branch显示提交信息
git中查看日志,我们用的比较多的就是 git log 以及带一些参数,如: 以一行显示提交日志: $ git log --pretty=oneline 显示最后的几次提交日志: $ git log ...
- 新浪实时股票数据接口http://hq.sinajs.cn/list=code
股票数据的获取目前有如下两种方法可以获取:1. http/javascript接口取数据2. web-service接口 1.http/javascript接口取数据1.1Sina股票数据接口以大秦铁 ...