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 ...
随机推荐
- Linux 安装PHP PECL 百分百成功
1. 下载 需要安装的组件 http://pecl.php.net/packages.php 2. 解压 tar zxf 你的扩展包路径 3. 进入你解压的扩展包路径后 访问 /usr/bin ...
- console对象-转
console对象 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 目录 开发者工具 console对象 console.log() console.debug() ...
- DataTable的数据批量写入数据库
最近在将excel中的文件导入到数据库中,用程序进行编写,由于数据量较大所以速度很慢,后来采用了SqlBulkCopy类,解决了速度的问题,我就insert语句,sqldataadapter.upda ...
- QSort函数对不同类型数据快速排序浅谈
一.对int类型数组排序 int num[100]; int cmp ( const void *a , const void *b ){return *(int *)a - *(int *)b;} ...
- dev GridControl 根据鼠标坐标 选中行
if (e.Button == System.Windows.Forms.MouseButtons.Right) { DevExpress.XtraGrid.Views.Grid.ViewInfo.G ...
- 小知识 安卓线程和ui
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- TDDL分库分表规则
规则如下: 判断一个ID在哪个库里的公式是:id % 4 / 2判断一个ID在哪个表里的公式是:id % 4 % 2 其中4表示总共有多少个分表,2表示总共有多少个数据库:上面这个例子,表示总共有2个 ...
- 相识HTML5 canvas
Canvas是HTML的API,我们可以用它在网页中实时的来生成图像. 文章导读 1.必备技能 2.用于画图的函数 例子: -会话气泡- -心形- -钟表- -星球里的星星- - ...
- ASP.NET MVC 从零开始 - 自动化部署(其一)
本文是从我的 github 博客 http://lxconan.github.io 导入的. 这是这个系列的第四篇了,前三篇请参见: ASP.NET MVC 从零开始 – Create and Run ...
- struts2 标签怪事
<s:if test="4 ge 3">111</s:if><s:else>222</s:else> 结果竟然是222 难道不是 4 ...