原文: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. IDEA新建时选项没有java class问题

    解决办法: 点击你的project F4打开project stucture 点击左边的module 点击右边的source后添加src就可以了  添加的src就是源码可以放置的地址

  2. 【poj3294-不小于k个字符串中最长公共子串】后缀数组

    1.注意每两个串之间的连接符要不一样. 2.分组的时候要注意最后一组啊!又漏了! 3.开数组要考虑连接符的数量.100010是不够的至少要101000. #include<cstdio> ...

  3. Sublime Text 3 遇到的一些小坑的解决方法

    1.[不停弹出更新框]Sublime Text 3 软件会弹出“Update Available”对话框,点击“Cancel”按钮取消:取消之后还是会频繁出现 解决方法:点击菜单栏“Preferenc ...

  4. Jackson对多态和多子类序列化的处理配置

    目录 Jackson 多态类型的处理 Jackson Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 多态类型的处理 jacks ...

  5. [Leetcode Week8]Edit Distance

    Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...

  6. Git-cheatsheet

  7. js反混淆工具

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  8. 【bzoj2796】 [Poi2012]Fibonacci Representation

    给出一个数字,用FIB数列各项加加减减来得到. 问最少要多少个(可以重复使用) 大概试了一下,fibonacci数列的增长是很快的,大概到了90+项就超过了题目范围…… 所以每次找一个最近的fibon ...

  9. 某dp题2

    P2401 不等数列 题目描述 将1到n任意排列,然后在排列的每两个数之间根据他们的大小关系插入">"和"<".问在所有排列中,有多少个排列恰好有k ...

  10. django定义模型类

    模型类被定义在应用文件夹下的model.py中 模型类必须继承Django的models.Model类 属性名不能用连续的两条下划线__ 主键:primary key,简写 pk 不需要主动定义,dj ...