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

一:动态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     }
时间: 2024-08-02 10:36:24

mybatis入门基础(五)----动态SQL的相关文章

MyBatis 之 使用五 动态SQL

MyBatis 提供使用 ognl 表达式动态生成 SQL的功能. 1. if 2. where where 可以自动处理掉第一个拼接条件里的 and <!-- 动态 sql 查询用户信息 -->   <select id="findUserByDynamicSql" parameterType="user" resultType="user">    select * from users    <!-- wher

mybatis入门篇3 ---- 动态sql,缓存,以及分页jar包的使用

首先我们来看一下动态sql,动态sql就是传递的参数不确定的时候,使用if,where,select,choose,set等标签,先来看一下 lib,rescources,以及utils里面文件不变,直接来看使用 直接看if跟where,if使用比较简单,就是if会有一个条件判断,如果条件满足,就会把if里面的sql语句块加入slq语句,where就是帮助我们加载一个where条件判断,并且会把拼接语句中的第一个and删除掉,接下来看一下例子 看一下UserMapper public inter

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

阅读目录 一:原始dao开发方法 二:mapper代理方法(只需要mapper接口,相当于dao接口) 承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先苦后甜嘛! 回到顶部 一:原始dao开发方法 概要:1.在上篇中搭建好的框价中编写dao接口和dao实现类 2.向dao接口实现类中注入SqlSessionFactory,在方法体内通过SqlSe

MyBatis学习总结_11_MyBatis动态Sql语句

MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) trim where set foreach 1.if 对属性进行判断,如果不为空则执行判断条件 [html] view plaincopy <select id="selectByCriteria" parameterType="com.mu

MyBatis学习总结(六)——动态SQL

MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) foreach where set trim 下面我们主要说 where set trim 这三个标签 1,where标签 <!-- 查询学生list,like姓名,=性别 --> <select id="getStudentListWhere" parameterTy

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

原文:http://www.cnblogs.com/selene/p/4613035.html 阅读目录 一:动态SQL 二:SQL片段 三:foreach 回到顶部 一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态sql,对查询条件进行判断,如果输入参数不为空才进行查询拼接. 1.3.UserMapper.xml 1 <!-- 用户信息综合查询

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

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射 mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. mybatis提供各种标签方法实现动态拼接sql. 1. if&where 1.2 需求 用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sq

Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql(转发同上)

原地址:http://www.cnblogs.com/shanheyongmu/p/7121807.html mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. mybatis提供各种标签方法实现动态拼接sql. 1. if&where 1.2 需求 用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql. 对查询条件进行判断,如果输入参数不为空才进行查询条件拼接. 1.3 mapper.xml <select id

MyBatis(五) 动态SQL

1.动态查询(动态生成Where语句): 这篇文章中叙述的功能,统一使用下表测试: CREATE TABLE `test_order_detail_mm` (   `id` int(20) NOT NULL AUTO_INCREMENT,   `order_id` int(20) NOT NULL,   `goods_name` varchar(50) DEFAULT NULL,   `single_price` decimal(19,2) DEFAULT NULL,   `num` int(2