mybatis学习之动态SQL
2017-06-21 15:43
495 查看
概念
mybatis核心就是对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活的拼接和组装需求
用户信息的综合查询列表和用户信息查询的列表总数这两个statement使用动态sql对查询条件进行判断,如果输入参数不为空,才进行查询条件拼接
where 与 if
mapper.xml:<!-- 用户信息的综合查询 #{userCustom.sex} : 取出pojo包装对象中性别的值 ${userCustom.username} : 取出pojo包装对象中用户名的值 --> <select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom" > select * from user <!-- where 可以自动去掉条件中的第一个and --> <where> <if test="UserCustom != null"> <if test="UserCustom.sex != null and UserCustom.sex != '' "> and user.sex = #{userCustom.sex} and </if> <if test="UserCustom.username != null and UserCustom.username != '' "> and user.username LIKE '%${userCustom.username}%' </if> </if> </where> </select> <!-- 用户信息的综合查询总数 parameterType:和findUserList一样 resultType:输出的结果类型 --> <select id="findUserCount" parameterType="UserQueryVo" resultType="int"> select COUNT(*) from user <!-- where 可以自动去掉条件中的第一个and --> <where> <if test="UserCustom != null"> <if test="UserCustom.sex != null and UserCustom.sex != '' "> and user.sex = #{userCustom.sex} and </if> <if test="UserCustom.username != null and UserCustom.username != '' "> and user.username LIKE '%${userCustom.username}%' </if> </if> </where> </select>可以使用where 和 if 条件语句进行sql语句的拼接
注意:
在此处的条件语句中,我们不能将包装pojo里面的pojo对象名写错,需要跟包装pojo类内部命名一致,不然就会报这个错
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'UserCustom' in 'class pojo.UserQueryVo' ### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'UserCustom' in 'class pojo.UserQueryVo'
sql片段
需求将上面实现的动态sql判断代码块,抽取出来,组成一个sql片段,其他的statement中可以引用该sql片段,方便程序员开发
定义
<!-- 定义一个sql片段 id:唯一标识 经验参考:定义sql片段是基于单标,这样的sql片段可重用性才高 在sql片段中不要包括where --> <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>
引用:
<select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom" > select * from user <!-- where 可以自动去掉条件中的第一个and --> <where> <!-- 引用sql片段 若sql片段不在本文件中,需要在前面加上mapper的namespace --> <include refid="query_user_where"/> </where> </select>
foreach
向sql中传递一个List,mybatis使用foreach解析需求
在用户查询列表和查询总数的statement中,增加多个id的输入查询
sql语句如下:
SELECT * FROM USER WHERE id IN (1,2,16,28,29)OR
SELECT * FROM USER WHERE id = 1 OR id = 2 OR id = 16 OR id = 28 OR id = 29
在输入的参数类型中添加List<Integer>ids 传入多个ID
// 传入多个ID private List<Integer> ids;同时生成get set方法
修改mapper.xml
在sql片段中加入:
<if test="ids != null "> <!-- 使用foreach 遍历传入的ids集合 collection:指定输入对象中的集合属性名 items:每次遍历生成的对象名 open; 开始遍历时的拼接串 and( 。。。 开始位置 close:结束遍历时的拼接串 ).. 结束位置 separator:遍历两个对象中间需要拼接的串 --> <foreach collection="ids" item="foreach_id" open="and(" close=")" separator="or"> <!-- 每次遍历需要拼接的串 --> id = #{foreach_id}; </foreach> </if>另外一种:
<foreach collection="ids" item="user_id" open="and id in(" close=")" separator=","> #{user_id} </foreach>
测试:
@Test public void testFindUserList() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); //创建一个UserMapper的对象,mybatis自动生成mapper代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //创建pojo包装对象 UserQueryVo userQueryVo = new UserQueryVo(); //创建pojo扩展对象 UserCustom userCustom = new UserCustom(); // userCustom.setSex("1"); userCustom.setUsername("测试"); List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(16); ids.add(28); ids.add(29); //将pojo扩展对象set进pojo包装对象 userQueryVo.setUserCustom(userCustom); userQueryVo.setIds(ids); //调用UserMapper的方法 List<UserCustom> list = userMapper.findUserList(userQueryVo); System.out.println(list); }
相关文章推荐
- MyBatis学习 之 三、动态SQL语句
- MyBatis学习 之 三、动态SQL语句
- MyBatis学习 之 三、动态SQL语句
- Mybatis学习(十二)mybatis理解动态sql及sql片段
- myBatis学习笔记(6)——动态SQL & 模糊查询
- MyBatis学习 之 三、动态SQL语句
- MyBatis学习 之 三、动态SQL语句
- 学习流水账:MyBatis -- helloworld,动态SQL
- MyBatis学习之三--动态SQL语句
- Mybatis学习(十一)mybatis动态sql传入pojo的包装类型
- MyBatis学习 之 三、动态SQL语句
- Github Mybatis深入学习之动态SQL
- Java学习笔记33:mybatis动态SQL语句
- Mybatis学习(8)动态sql语句
- mybatis 学习 三、动态SQL语句
- MyBatis学习 之 三、动态SQL语句
- MyBatis学习 之 三、动态SQL语句
- MyBatis学习 之 三、动态SQL语句
- Mybatis学习笔记-动态sql
- MyBatis学习笔记(五)---mybatis 动态sql语句