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 ...
随机推荐
- 省市县三级联动(jqurey+json)
1.效果图 2.联动js /** * jquery.choosearea.js - 地区联动封装 */ ; (function ($) { var choosearea = function (opt ...
- Windows Server 2012 支持的逻辑盘容量最大是多少?
这个问题似乎看起来是问系统支持最大硬盘参数?其实不然,这和文件系统有着很大关系. 磁盘在系统应用之前,要先初始化,然后创建卷,再进行格式化后完成在系统的挂载.完成这些操作之后,磁盘空间可以被系统使用. ...
- winform设置文本框宽度 根据文字数量和字体返回宽度
_LinkLabel.Width = TextRenderer.MeasureText(_LinkLabel.Text, _LinkLabel.Font).Width;
- 《CLR.via.C#第三版》第二部分第12章节 泛型 读书笔记(六)
终于讲到泛型了.当初看到这个书名,最想看的就是作者对泛型,委托,反射这些概念的理解.很多人对泛型的理解停留在泛型集合上,刚开始我也是,随着项目越做越多,对待泛型的认识也越来越深刻. 泛型的概念:泛型是 ...
- 喜大普奔,微软Microsoft JDBC Driver For SQL Server已发布到maven中央仓库
相信通过java和SQLServer开发应用的同学们都经历过如下类似的问题. 微软提供的JDBC官方驱动没有放置在Maven仓库中,这样如果你的Java应用需要访问SQL Server,你不得不下载s ...
- 模拟淘宝购物,运用cookie,记录登录账号信息,并且记住购物车内所选的商品
1.登录界面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEn ...
- weblogic10内存溢出解决方法
在开发过程中经常会遇到weblogic内存溢出问题,用下面的办法解决了. 找到domain/bin下的setDomainEnv.cmd文件,里面可以找到以下四行代码,将值该打一倍,重启服务. set ...
- WaitType:ASYNC_IO_COMPLETION
项目组有一个数据库备份的Job运行异常,该Job将备份数据存储到remote server上,平时5个小时就能完成的备份操作,现在运行19个小时还没有完成,backup命令的Wait type是 AS ...
- Constraint5:unique 约束和null
unique约束使用unique index来限制列值的唯一性: 创建unique约束之后,column中允许插入null值,unique 约束将两个null值看作是相同的(即null=null为tr ...
- ios语音输入崩溃
游戏中任何可以输入的地方,只要调用语音输入,必然会导致app崩溃,解决方法如下: ok, so essentially the gist of it is that siri wants gl con ...