mybatis入门基础(五)----动态SQL
一:动态SQL
1.1.定义
mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。
1.2.案例需求
用户信息综合查询列表这个statement的定义使用动态sql,对查询条件进行判断,如果输入参数不为空才进行查询拼接。
1.3.UserMapper.xml
<!-- 用户信息综合查询
#{userCustom.sex}:取出pojo包装对象中性别值
${userCustom.username}:取出pojo对象中用户名称
-->
<select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo"
resultType="com.mybatis.entity.UserCustom">
select * from t_user
<!-- 动态sql查询:where可以自动去掉第一个and -->
<where>
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!='' ">
and sex=#{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!='' ">
and username=#{userCustom.username}
</if>
</if>
</where>
<!-- where sex=#{userCustom.sex} and username LIKE '%${userCustom.username}%' -->
</select>
1.4.测试代码
@Test
public void testFindUserList() {
SqlSession sqlSession = sqlSessionFactory.openSession();
//创造查询条件
UserQueryVo userQueryVo = new UserQueryVo();
UserCustom userCustom = new UserCustom();
// userCustom.setSex("2");
//这里使用动态sql,如果不设置某个值,条件不会拼接sql中
userCustom.setUsername("小");
userQueryVo.setUserCustom(userCustom);
// 创建Usermapper对象,mybatis自动生成mapper代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<UserCustom>list=mapper.findUserList(userQueryVo);
//测试动态sql,属性的非空判断测试
// List<UserCustom>list=mapper.findUserList(null);
System.out.println(list);
sqlSession.commit();
sqlSession.close();
}
二:SQL片段
2.1.需求
将上边的动态sql判断代码抽取出来,组成一个sql片段,其它的statement中就可以引用sql片段,方便开发。
2.2.定义sql片段
<!-- 定义sql片段,Id是唯一标识
建议:是基于单表来定义sql片段,这样的话sql片段的可重用性才高,在sql片段中不要包含where
-->
<sql id="query_user_where" >
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!='' ">
and sex=#{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!='' ">
and username=#{userCustom.username}
</if>
</if>
</sql>
2.3.在mapper.xml中定义的statement中引用sql片段
<!-- 用户信息综合查询
#{userCustom.sex}:取出pojo包装对象中性别值
${userCustom.username}:取出pojo对象中用户名称
-->
<select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo"
resultType="com.mybatis.entity.UserCustom">
select * from t_user
<!-- 动态sql查询:where可以自动去掉第一个and -->
<where>
<!-- 引用sql片段的id,如果refid指定的不在本mapper.xml中,需要前边加namespace -->
<include refid="query_user_where"></include>
<!-- 这里可以引用其它的sql片段 -->
</where>
</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
package com.mybatis.entity; import java.util.List; /**
*
* @ClassName: UserQueryVo
* @Description: TODO(包装类型)
* @author warcaft
*
*/
public class UserQueryVo { public List<Integer> ids; public List<Integer> getIds() {
return ids;
} public void setIds(List<Integer> ids) {
this.ids = ids;
}
}
3.3.mapper.xml代码
<!-- 实现下边的sql拼接
select * from t_user where id=1 OR id=2 OR id=3
-->
<select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo"
resultType="com.mybatis.entity.User">
select * from t_user
<where>
<if test="ids!=null">
<!-- 使用foreach遍历ids
collection:指定输入对象的集合属性
item:每个遍历生成对象中
open:开始遍历时拼接的串
close:技术遍历时拼接的串
separator:遍历的两个对象中需要拼接的串
-->
<foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
id=#{user_id}
</foreach>
</if>
</where>
</select>
select * from t_user where id in(1,2,3)的mapper.xml配置
<select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo"
resultType="com.mybatis.entity.User">
select * from t_user
<where>
<if test="ids!=null">
<!--
使用foreach遍历ids
collection:指定输入对象的集合属性
item:每个遍历生成对象中
open:开始遍历时拼接的串
close:技术遍历时拼接的串
separator:遍历的两个对象中需要拼接的串
-->
<!-- 实现“ select * from t_user where id in(1,2,3)”拼接 -->
<foreach collection="ids" item="user_id" open="AND id in (" close=")" separator=",">
id=#{user_id}
</foreach>
</if>
</where>
</select>
userMapper.java代码
public interface UserMapper {
//ids查询用户数据
public List<User> findUserByIds(UserQueryVo userQueryVo);
}
Junit测试代码
@Test
public void findUserByIdsTest() {
SqlSession sqlSession = sqlSessionFactory.openSession();
// 创建Usermapper对象,mybatis自动生成mapper代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//创造查询条件
UserQueryVo userQueryVo = new UserQueryVo();
//传入多个id
List<Integer> ids=new ArrayList<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
//将ids通过userQueryVo传入statement中
userQueryVo.setIds(ids);
//调用userMapper的代码
List<UserCustom> userList= mapper.findUserList(userQueryVo);
System.out.println(userList);
sqlSession.close();
}
mybatis入门基础(五)----动态SQL的更多相关文章
- mybatis学习记录五——动态sql
8 动态sql 8.1 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 8.2 需求 用户信息综合查询列表 ...
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...
- Spring mybatis源码篇章-动态SQL节点源码深入
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...
- 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标签 我们根据实体类的不 ...
- mybatis入门基础----动态SQL
原文:http://www.cnblogs.com/selene/p/4613035.html 阅读目录 一:动态SQL 二:SQL片段 三:foreach 回到顶部 一:动态SQL 1.1.定义 m ...
- Spring mybatis源码篇章-动态SQL基础语法以及原理
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-Mybatis的XML文件加载 前话 前文通过Spring中配置mapperLocations属性来进行对m ...
- Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——My ...
随机推荐
- Ubuntu安装c++编译器
打开终端输入sudo apt-get install build-essential 安装gcc和一些库函数.提供C/C++的编译环境 注意编译c++程序要用g++
- 文件过滤驱动框架Minispy解析一
因工作需要,研究minispy文件过滤框架,上图为我整理出的其内核部分代码的逻辑.
- ASP.NET MVC 5 Web编程2 -- URL映射(路由原理)
本章将讲述ASP.NET MVC5 的路由原理,即URL映射机制. 简单点就是解释:为什么MVC在浏览器输入地址就能访问到类(或类中的方法)?这是怎么做到的?我自己可以通过.NET写出一个自己的MVC ...
- EasyPR--开发详解(3)高斯模糊、灰度化和Sobel算子
在上篇文章中我们了解了PlateLocate的过程中的所有步骤.在本篇文章中我们对前3个步骤,分别是高斯模糊.灰度化和Sobel算子进行分析. 一.高斯模糊 1.目标 对图像去噪,为边缘检测算法做准备 ...
- CSS3 Flexbox不迷路指南
Flexbox早有耳闻,但是决定切实尝试一番,还是因为看了这条围脖: 我觉得用flexbox可以实现,但是发觉无从下手,属性特性都不了解.趁此机会,学习下. 英文原版参考资料在这里:A Complet ...
- PHP钩子机制
什么是钩子 大家想必听过插件,wordpress插件特别多,这个就是用钩子机制实现的. 当代码在运行的过程中,我们预先在运行的几个特殊点里执行一些特殊方法:例如在运行方法(例如Blog::add的ad ...
- for 循环打印图形
public class For { public static void main(String[] args) { //"使用双层for循环打印图形时,外层管行,内层管列",那 ...
- windows server 2008 r2 企业版 hyper v做虚拟化的相关问题处理
windows server 2008 r2 企业版 hyper v做虚拟化的相关问题处理 今天在dell r710 上用windows server 2008 r2企业版hyper v 做虚拟化,添 ...
- 把w3schools英文版的所有属性扒下来了,免费分享。
为了方便查手册等,把w3schools上的所以属性弄成json版的了,到时候再修改一下,查起来就比较方便了,这里免费分享一下.一共为两份,一份为选择器部分,还有一部分是属性. 选择器部分json 属性 ...
- webBrowser 加载网页
事件 webBrowser_DocumentCompleted private void webBrowser_DocumentCompleted(object sender, WebBrowserD ...