一:动态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的更多相关文章

  1. mybatis学习记录五——动态sql

    8       动态sql 8.1     什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 8.2     需求 用户信息综合查询列表 ...

  2. mybatis入门基础(二)----原始dao的开发和mapper代理开发

    承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...

  3. Spring mybatis源码篇章-动态SQL节点源码深入

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...

  4. MyBatis学习总结_11_MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  5. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  6. SSM框架之Mybatis(6)动态SQL

    Mybatis(6)动态SQL 1.动态SQL 出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了 1.1.if标签 我们根据实体类的不 ...

  7. mybatis入门基础----动态SQL

    原文:http://www.cnblogs.com/selene/p/4613035.html 阅读目录 一:动态SQL 二:SQL片段 三:foreach 回到顶部 一:动态SQL 1.1.定义 m ...

  8. Spring mybatis源码篇章-动态SQL基础语法以及原理

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-Mybatis的XML文件加载 前话 前文通过Spring中配置mapperLocations属性来进行对m ...

  9. Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——My ...

随机推荐

  1. VB.net 2010下关联与程序图标设置

    '*************************************************************************'**模 块 名:VB.net 2010下关联与程序 ...

  2. 如何在Windows7上完全卸载Oracle 11g(转)

    http://blog.csdn.net/haishu_zheng/article/details/19180081

  3. Tomcat 中响应头信息(Http Response Header) Content-Length 和 Transfer-Encoding

    户端(PC浏览器或者手机浏览器)在接受到Tomcat的响应的时候,头信息通常都会带上Content-Length ,一般情况下客户端会在接受完Content-Length长度的数据之后才会开始解析.而 ...

  4. [译]MVC网站教程(二):异常管理

    介绍 “MVC网站教程”系列的目的是教你如何使用 ASP.NET MVC 创建一个基本的.可扩展的网站. 1)   MVC网站教程(一):多语言网站框架 2)   MVC网站教程(二):异常管理 3) ...

  5. Step by step 活动目录中添加一个子域

    原创地址:http://www.cnblogs.com/jfzhu/p/4006545.html 转载请注明出处 前面介绍过如何创建一个域,下面再介绍一下如何在该父域中添加一个子域. 活动目录中的森林 ...

  6. 2016苹果春季发布会 iPhone SE发布

    配置如下 主屏尺寸:4英寸 主屏分辨率:1336x640像素 后置摄像头:1200万像素 前置摄像头:120万像素 电池容量:1624mAh 核心数:双核 操作系统:iOS 9 核心数:双核 CPU: ...

  7. Distributed2:Linked Server Login 添加和删除

    一,通过 sys.sp_addlinkedsrvlogin 创建Linked Server的Login 当在local Server 上需要访问Linked Server时,Local Server ...

  8. jquery 拖拽,框选的一点积累

    拖拽draggable,框选 selectable,按ctrl多选,临近辅助对齐,从工具栏拖工具  等,和jqueryui的selectable不同,是在一个父div里框选子div(类似框选文件),一 ...

  9. Iterate Files by Tcltk

    Iterate Files by Tcltk eryar@163.com Abstract. Tcl/Tk provide a programming system for developing an ...

  10. socket编程

    一.socket地址的数据类型及相关函数 socket API是一层抽象的网络编程接口,适用于各种底层网络协议,如IPv4.IPv6.UNIX Domain Socket.然而各种网络协议的地址格式并 ...