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

时间:2022-06-30 05:07:25

阅读目录

回到顶部

一:动态SQL

  1.1.定义

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

  1.2.案例需求

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

  1.3.UserMapper.xml

mybatis入门基础(五)----动态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 <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>
mybatis入门基础(五)----动态SQL

  1.4.测试代码

mybatis入门基础(五)----动态SQL
 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 }
mybatis入门基础(五)----动态SQL
回到顶部

二:SQL片段

  2.1.需求

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

  2.2.定义sql片段  

mybatis入门基础(五)----动态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>
mybatis入门基础(五)----动态SQL

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

mybatis入门基础(五)----动态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>
mybatis入门基础(五)----动态SQL
回到顶部

三: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

mybatis入门基础(五)----动态SQL
 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 }
mybatis入门基础(五)----动态SQL

3.3.mapper.xml代码

mybatis入门基础(五)----动态SQL
 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>
mybatis入门基础(五)----动态SQL
select * from t_user where id in(1,2,3)的mapper.xml配置
mybatis入门基础(五)----动态SQL
 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>
mybatis入门基础(五)----动态SQL

userMapper.java代码

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

Junit测试代码

mybatis入门基础(五)----动态SQL
 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