mybatis(6)动态sql
2015-10-25 21:15
411 查看
1.1 什么是动态sql
mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。1.2 需求
用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。
1.3 mapper.xml
<span style="font-size:14px;"><!-- 用户信息综合查询 --> <select id="findUserList" parameterType="com.xdy.mybatis.po.UserQueryVo" resultType="com.xdy.mybatis.po.UserCustom"> SELECT * FROM USER <where> <if test="userCustom!=null"> <if test="userCustom.sex!=null and userCustom.sex!=''"> and user.sex=#{userCustom.sex} </if> <if test="userCustom.username!=null and userCustom.username!=''"> and user.username like '%${userCustom.username}%' </if> </if> </where> </select> <!-- 用户信息综合查询 --> <select id="findUserCount" parameterType="com.xdy.mybatis.po.UserQueryVo" resultType="int"> SELECT count(*) FROM USER <where> <if test="userCustom!=null"> <if test="userCustom.sex!=null and userCustom.sex!=''"> and user.sex=#{userCustom.sex} </if> <if test="userCustom.username!=null and userCustom.username!=''"> and user.username like '%${userCustom.username}%' </if> </if> </where> </select></span>
1.4 测试代码
@Test public void testFindUserList() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); UserQueryVo userQueryVo = new UserQueryVo(); UserCustom userCustom = new UserCustom(); userCustom.setSex("1"); userCustom.setUsername("小明"); userQueryVo.setUserCustom(userCustom); List<UserCustom> list = userMapper.findUserList(userQueryVo); System.out.println(list); }
1.5 sql片段
1.5.1 需求
将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。方便程序员进行开发。1.5.2 定义sql片段
<!-- 定义SQL片段 --> <sql id="query_user_where"> <if test="userCustom!=null"> <if test="userCustom.sex!=null and userCustom.sex!=''"> and user.sex=#{userCustom.sex} </if> <if test="userCustom.username!=null and userCustom.username!=''"> and user.username like '%${userCustom.username}%' </if> </if> </sql>
1.5.3 引用sql片段
在mapper.xml中定义的statement中引用sql片段:<!-- 用户信息综合查询 --> <select id="findUserList" parameterType="com.xdy.mybatis.po.UserQueryVo" resultType="com.xdy.mybatis.po.UserCustom"> SELECT * FROM USER <where> <include refid="query_user_where"></include> </where> </select> <!-- 用户信息综合查询 --> <select id="findUserCount" parameterType="com.xdy.mybatis.po.UserQueryVo" resultType="int"> SELECT count(*) FROM USER <where> <!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前边加namespace --> <include refid="query_user_where"></include> </where> </select>
1.6 foreach
向sql传递数组或List,mybatis使用foreach解析1.6.1 需求
在用户查询列表和查询总数的statement中增加多个id输入查询。sql语句如下:
两种方法:
SELECT * FROM USER WHERE id=1 OR id=10 OR id=16
SELECT * FROM USER WHERE id IN(1,10,16)
1.6.2 在输入参数类型中添加List<Integer> ids传入多个id
package com.xdy.mybatis.po; import java.util.List; public class UserQueryVo { //传入多个id private List<Integer> ids; //用户查询条件 private UserCustom userCustom; public UserCustom getUserCustom() { return userCustom; } public void setUserCustom(UserCustom userCustom) { this.userCustom = userCustom; } public List<Integer> getIds() { return ids; } public void setIds(List<Integer> ids) { this.ids = ids; } }
1.6.3 修改mapper.xml
WHERE id=1 OR id=10 OR id=16在查询条件中,查询条件定义成一个sql片段,需要修改sql片段。
<if t
4000
est="ids!=null">
<!-- 使用 foreach遍历传入ids
collection:指定输入 对象中集合属性
item:每个遍历生成对象中
open:开始遍历时拼接的串
close:结束遍历时拼接的串
separator:遍历的两个对象中需要拼接的串
-->
<!-- 使用实现下边的sql拼接:
AND (id=1 OR id=10 OR id=16)
-->
<!-- <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
每个遍历需要拼接的串
id=#{user_id}
</foreach> -->
<!-- 实现 “ and id IN(1,10,16)”拼接 -->
<foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">
<!-- 每个遍历需要拼接的串 -->
#{user_id}
</foreach>
</if>
1.6.4 测试代码
@Test public void testFindUserList() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); UserQueryVo userQueryVo = new UserQueryVo(); UserCustom userCustom = new UserCustom(); userCustom.setSex("1"); userCustom.setUsername("小明"); //传入多个id List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(16); ids.add(22); userQueryVo.setIds(ids); userQueryVo.setUserCustom(userCustom); List<UserCustom> list = userMapper.findUserList(userQueryVo); System.out.println(list); }
相关文章推荐
- sql连表查询
- 云时代的分布式数据库:阿里分布式数据库服务DRDS
- 数据库范式
- mysql 性能优化方案
- 数据库安全
- ubuntu mongodb报错:mongo - couldn't connect to server 127.0.0.1:27017
- ubuntu mongodb报错:mongo - couldn't connect to server 127.0.0.1:27017
- 【mysql】数据库Schema的优化
- iOS 中SQLite数据库操作
- Mysql cookbook(1)
- 大型分布式数据库集群的研究
- 在一条SQL语句中向2个表中插入数据解决方案
- 黄聪:优化清理WordPress数据库wp_options表(缩小autoload体积)
- SQLite数据库总结二
- MongoDB入门篇
- 后台数据库优化——板机
- sql 存储过程 循环
- SQL查询语句大全集锦
- MsSqlServer 复制分发概述
- MySQL自带的性能压力测试工具mysqlslap详解