标签:from jdb nbsp rri app 表示 包含 详解 复制
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。
MyBatis中用于实现动态SQL的元素主要有:
1 <select id="dynamicIfTest" parameterType="Blog" resultType="Blog"> 2 select * from t_blog where 11 = 1 3 <if test="title != null"> 4 and title = #{title} 5 </if> 6 <if test="content != null"> 7 and content = #{content} 8 </if> 9 <if test="owner != null"> 10 and owner = #{owner} 11 </if> 12 </select>
1 <select id="dynamicChooseTest" parameterType="Blog" resultType="Blog"> 2 select * from t_blog where 11 = 1 3 <choose> 4 <when test="title != null"> 5 and title = #{title} 6 </when> 7 <when test="content != null"> 8 and content = #{content} 9 </when> 10 <otherwise> 11 and owner = "owner1" 12 </otherwise> 13 </choose> 14 </select>
1 <select id="dynamicWhereTest" parameterType="Blog" resultType="Blog"> 2 select * from t_blog 3 <where> 4 <if test="title != null"> 5 title = #{title} 6 </if> 7 <if test="content != null"> 8 and content = #{content} 9 </if> 10 <if test="owner != null"> 11 and owner = #{owner} 12 </if> 13 </where> 14 </select>
1 <select id="dynamicTrimTest" parameterType="Blog" resultType="Blog"> 2 select * from t_blog 3 <trim prefix="where" prefixOverrides="and |or"> 4 <if test="title != null"> 5 title = #{title} 6 </if> 7 <if test="content != null"> 8 and content = #{content} 9 </if> 10 <if test="owner != null"> 11 or owner = #{owner} 12 </if> 13 </trim> 14 </select>
set元素主要是用在更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。有了set元素我们就可以动态的更新那些修改了的字段。下面是一段示例代码:
1 <update id="dynamicSetTest" parameterType="Blog"> 2 update t_blog 3 <set> 4 <if test="title != null"> 5 title = #{title}, 6 </if> 7 <if test="content != null"> 8 content = #{content}, 9 </if> 10 <if test="owner != null"> 11 owner = #{owner} 12 </if> 13 </set> 14 where id = #{id} 15 </update>
1 <select id="dynamicForeachTest" resultType="Blog"> 2 select * from t_blog where id in 3 <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> 4 #{item} 5 </foreach> 6 </select>
上述collection的值为list,对应的Mapper是这样的
public List<Blog> dynamicForeachTest(List<Integer> ids);
测试代码:
1 @Test 2 public void dynamicForeachTest() { 3 SqlSession session = Util.getSqlSessionFactory().openSession(); 4 BlogMapper blogMapper = session.getMapper(BlogMapper.class); 5 List<Integer> ids = new ArrayList<Integer>(); 6 ids.add(1); 7 ids.add(3); 8 ids.add(6); 9 List<Blog> blogs = blogMapper.dynamicForeachTest(ids); 10 for (Blog blog : blogs) 11 System.out.println(blog); 12 session.close(); 13 }
2.单参数array数组的类型:
1 <select id="dynamicForeach2Test" resultType="Blog"> 2 select * from t_blog where id in 3 <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> 4 #{item} 5 </foreach> 6 </select>
上述collection为array,对应的Mapper代码:
public List<Blog> dynamicForeach2Test(int[] ids);
对应的测试代码:
1 @Test 2 public void dynamicForeach2Test() { 3 SqlSession session = Util.getSqlSessionFactory().openSession(); 4 BlogMapper blogMapper = session.getMapper(BlogMapper.class); 5 int[] ids = new int[] {1,3,6,9}; 6 List<Blog> blogs = blogMapper.dynamicForeach2Test(ids); 7 for (Blog blog : blogs) 8 System.out.println(blog); 9 session.close(); 10 }
3.自己把参数封装成Map的类型
1 <select id="dynamicForeach3Test" resultType="Blog"> 2 select * from t_blog where title like "%"#{title}"%" and id in 3 <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> 4 #{item} 5 </foreach> 6 </select>
上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码:
public List<Blog> dynamicForeach3Test(Map<String, Object> params);
对应测试代码:
1 @Test 2 public void dynamicForeach3Test() { 3 SqlSession session = Util.getSqlSessionFactory().openSession(); 4 BlogMapper blogMapper = session.getMapper(BlogMapper.class); 5 final List<Integer> ids = new ArrayList<Integer>(); 6 ids.add(1); 7 ids.add(2); 8 ids.add(3); 9 ids.add(6); 10 ids.add(7); 11 ids.add(9); 12 Map<String, Object> params = new HashMap<String, Object>(); 13 params.put("ids", ids); 14 params.put("title", "中国"); 15 List<Blog> blogs = blogMapper.dynamicForeach3Test(params); 16 for (Blog blog : blogs) 17 System.out.println(blog); 18 session.close(); 19 }
标签:from jdb nbsp rri app 表示 包含 详解 复制
原文地址:http://www.cnblogs.com/efforts-will-be-lucky/p/7388806.html