原文:http://www.cnblogs.com/selene/p/4613035.html

一:动态SQL

  1.1.定义

    mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

  1.2.案例需求

    用户信息综合查询列表这个statement的定义使用动态sql,对查询条件进行判断,如果输入参数不为空才进行查询拼接。

  1.3.UserMapper.xml

 1 <!-- 用户信息综合查询
2 #{userCustom.sex}:取出pojo包装对象中性别值
3 ${userCustom.username}:取出pojo对象中用户名称
4 -->
5 <select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo"
6 resultType="com.mybatis.entity.UserCustom">
7 select * from t_user
8 <!-- 动态sql查询:where可以自动去掉第一个and -->
9 <where>
10 <if test="userCustom!=null">
11 <if test="userCustom.sex!=null and userCustom.sex!='' ">
12 and sex=#{userCustom.sex}
13 </if>
14 <if test="userCustom.username!=null and userCustom.username!='' ">
15 and username=#{userCustom.username}
16 </if>
17 </if>
18 </where>
19 <!-- where sex=#{userCustom.sex} and username LIKE '%${userCustom.username}%' -->
20 </select>

  1.4.测试代码

 1     @Test
2 public void testFindUserList() {
3 SqlSession sqlSession = sqlSessionFactory.openSession();
4 //创造查询条件
5 UserQueryVo userQueryVo = new UserQueryVo();
6 UserCustom userCustom = new UserCustom();
7 // userCustom.setSex("2");
8 //这里使用动态sql,如果不设置某个值,条件不会拼接sql中
9 userCustom.setUsername("小");
10 userQueryVo.setUserCustom(userCustom);
11 // 创建Usermapper对象,mybatis自动生成mapper代理对象
12 UserMapper mapper = sqlSession.getMapper(UserMapper.class);
13 List<UserCustom>list=mapper.findUserList(userQueryVo);
14 //测试动态sql,属性的非空判断测试
15 // List<UserCustom>list=mapper.findUserList(null);
16 System.out.println(list);
17 sqlSession.commit();
18 sqlSession.close();
19 }

二:SQL片段

  2.1.需求

    将上边的动态sql判断代码抽取出来,组成一个sql片段,其它的statement中就可以引用sql片段,方便开发。

  2.2.定义sql片段  

 1 <!-- 定义sql片段,Id是唯一标识
2 建议:是基于单表来定义sql片段,这样的话sql片段的可重用性才高,在sql片段中不要包含where
3 -->
4 <sql id="query_user_where" >
5 <if test="userCustom!=null">
6 <if test="userCustom.sex!=null and userCustom.sex!='' ">
7 and sex=#{userCustom.sex}
8 </if>
9 <if test="userCustom.username!=null and userCustom.username!='' ">
10 and username=#{userCustom.username}
11 </if>
12 </if>
13 </sql>

  2.3.在mapper.xml中定义的statement中引用sql片段

 1 <!-- 用户信息综合查询
2 #{userCustom.sex}:取出pojo包装对象中性别值
3 ${userCustom.username}:取出pojo对象中用户名称
4 -->
5 <select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo"
6 resultType="com.mybatis.entity.UserCustom">
7 select * from t_user
8 <!-- 动态sql查询:where可以自动去掉第一个and -->
9 <where>
10 <!-- 引用sql片段的id,如果refid指定的不在本mapper.xml中,需要前边加namespace -->
11 <include refid="query_user_where"></include>
12 <!-- 这里可以引用其它的sql片段 -->
13 </where>
14 </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

 1 package com.mybatis.entity;
2
3 import java.util.List;
4
5 /**
6 *
7 * @ClassName: UserQueryVo
8 * @Description: TODO(包装类型)
9 * @author warcaft
10 *
11 */
12 public class UserQueryVo {
13
14 public List<Integer> ids;
15
16 public List<Integer> getIds() {
17 return ids;
18 }
19
20 public void setIds(List<Integer> ids) {
21 this.ids = ids;
22 }
23 }

3.3.mapper.xml代码

 1     <!-- 实现下边的sql拼接
2 select * from t_user where id=1 OR id=2 OR id=3
3 -->
4 <select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo"
5 resultType="com.mybatis.entity.User">
6 select * from t_user
7 <where>
8 <if test="ids!=null">
9 <!-- 使用foreach遍历ids
10 collection:指定输入对象的集合属性
11 item:每个遍历生成对象中
12 open:开始遍历时拼接的串
13 close:技术遍历时拼接的串
14 separator:遍历的两个对象中需要拼接的串
15 -->
16 <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
17 id=#{user_id}
18 </foreach>
19 </if>
20 </where>
21 </select>
select * from t_user where id in(1,2,3)的mapper.xml配置
 1  <select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo"
2 resultType="com.mybatis.entity.User">
3 select * from t_user
4 <where>
5 <if test="ids!=null">
6 <!--
7 使用foreach遍历ids
8 collection:指定输入对象的集合属性
9 item:每个遍历生成对象中
10 open:开始遍历时拼接的串
11 close:技术遍历时拼接的串
12 separator:遍历的两个对象中需要拼接的串
13 -->
14 <!-- 实现“ select * from t_user where id in(1,2,3)”拼接 -->
15 <foreach collection="ids" item="user_id" open="AND id in (" close=")" separator=",">
16 id=#{user_id}
17 </foreach>
18 </if>
19 </where>
20 </select>

userMapper.java代码

1 public interface UserMapper {
2 //ids查询用户数据
3 public List<User> findUserByIds(UserQueryVo userQueryVo);
4 }

Junit测试代码

 1 @Test
2 public void findUserByIdsTest() {
3 SqlSession sqlSession = sqlSessionFactory.openSession();
4 // 创建Usermapper对象,mybatis自动生成mapper代理对象
5 UserMapper mapper = sqlSession.getMapper(UserMapper.class);
6 //创造查询条件
7 UserQueryVo userQueryVo = new UserQueryVo();
8 //传入多个id
9 List<Integer> ids=new ArrayList<Integer>();
10 ids.add(1);
11 ids.add(2);
12 ids.add(3);
13 //将ids通过userQueryVo传入statement中
14 userQueryVo.setIds(ids);
15 //调用userMapper的代码
16 List<UserCustom> userList= mapper.findUserList(userQueryVo);
17 System.out.println(userList);
18 sqlSession.close();
19 }

mybatis入门基础----动态SQL的更多相关文章

  1. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  2. <MyBatis>入门六 动态sql

    package org.maple.mapper; import org.apache.ibatis.annotations.Param; import org.maple.pojo.Employee ...

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

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

  4. 【mybatis深度历险系列】mybatis中的动态sql

    最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...

  5. mybatis 详解------动态SQL

    mybatis 详解------动态SQL   目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...

  6. mybatis中的动态SQL

    在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...

  7. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  8. mybatis入门基础(五)----动态SQL

    一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...

  9. 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)

    动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...

随机推荐

  1. 图论:Stoer-Wagner算法

    利用Stoer-Wagner算法求无向图最小割 直接给出算法描述和过程实现: 算法步骤: . 设最小割cut=INF, 任选一个点s到集合A中, 定义W(A, p)为A中的所有点到A外一点p的权总和. ...

  2. 【转载】字符串最小表示法-O(n)算法

    原博客链接:http://blog.csdn.net/zy691357966/article/details/39854359 未授权,侵权删. 因为这篇博客写得真好..转载了.. 红色的字是原博主写 ...

  3. [BZOJ3098]Hash Killer II解题报告

    这天天气不错,hzhwcmhf神犇给VFleaKing出了一道题:给你一个长度为N的字符串S,求有多少个不同的长度为L的子串.子串的定义是S[l].S[l + 1].... S[r]这样连续的一段.两 ...

  4. GridPanel分页条插件

    GridPanel的分页条没有设置当前页显示条数的功能,会不大方便 主要是抄袭的http://www.cnblogs.com/badwps/archive/2011/04/15/2016440.htm ...

  5. objc_msgSend arm64 崩溃问题

    http://blog.csdn.net/chenyong05314/article/details/42121001 2014-12-24 10:49 878人阅读 评论(0) 收藏 举报 转载自: ...

  6. 如何重新签名ipa文件

    http://www.cocoachina.com/bbs/read.php?tid=185963 求ipa文件修改后重新打包的方法,我已经有开发者账号了. 替换了ipa里的图片后,就无法安装了,似乎 ...

  7. 安全测试===Mysql 注入技巧学习 MySQL注入技巧(2)

    原文地址:http://websec.files.wordpress.com/2010/11/sqli2.pdf 0x00.介绍 也可以参考瞌腄龙的mysql注入科普:http://drops.woo ...

  8. mysql索引语法及示例

    注:本篇文章是对菜鸟教程中的mysql索引(http://www.runoob.com/mysql/mysql-index.html)的翻译版本:添加了示例,便于理解: 索引分单列索引和组合索引.单列 ...

  9. Linux下挂载存储设备

    1. 安装iscsi (文件iscsi-initiator-utils-6.2.0.871-0.10.el5.x86_64.rpm在系统光盘可以找到)   rpm -ivh iscsi-initiat ...

  10. JavaScript ES6部分语法

    ES6是JavaScript语言的新版本,它也可以叫做ES2015,之前学习的JavaScript属于ES5,ES6在它的基础上增加了一些语法,ES6是未来JavaScript的趋势,而且vue组件开 ...