MyBatis的动态SQL是基于OGNL表达式的,它能够帮助咱们方便的在SQL语句中实现某些逻辑。java
MyBatis中用于实现动态SQL的元素主要有:数组
<select id="dynamicIfTest" parameterType="Blog" resultType="Blog"> select * from t_blog where 11 = 1 <if test="title != null"> and title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> and owner = #{owner} </if> </select>
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog"> select * from t_blog where 11 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "owner1" </otherwise> </choose> </select>
<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog"> select * from t_blog <where> <if test="title != null"> title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> and owner = #{owner} </if> </where> </select>
where元素的做用是会在写入where元素的地方输出一个where,另一个好处是你不须要考虑where元素里面的条件输出是什么样子的,MyBatis会智能的帮你处理,若是全部的条件都不知足那么MyBatis就会查出全部的记录,若是输出后是and 开头的,MyBatis会把第一个and忽略,固然若是是or开头的,MyBatis也会把它忽略;此外,在where元素中你不须要考虑空格的问题,MyBatis会智能的帮你加上。像上述例子中,若是title=null, 而content != null,那么输出的整个语句会是select * from t_blog where content = #{content},而不是select * from t_blog where and content = #{content},由于MyBatis会智能的把首个and 或 or 给忽略。session
<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog"> select * from t_blog <trim prefix="where" prefixOverrides="and |or"> <if test="title != null"> title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> or owner = #{owner} </if> </trim> </select>
set元素主要是用在更新操做的时候,它的主要功能和where元素实际上是差很少的,主要是在包含的语句前输出一个set,而后若是包含的语句是以逗号结束的话将会把该逗号忽略,若是set包含的内容为空的话则会出错。有了set元素咱们就能够动态的更新那些修改了的字段。下面是一段示例代码:app
<update id="dynamicSetTest" parameterType="Blog"> update t_blog <set> <if test="title != null"> title = #{title}, </if> <if test="content != null"> content = #{content}, </if> <if test="owner != null"> owner = #{owner} </if> </set> where id = #{id} </update>
上述示例代码中,若是set中一个条件都不知足,即set中包含的内容为空的时候就会报错。框架
<select id="dynamicForeachTest" resultType="Blog"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection的值为list,对应的Mapper是这样的ide
public List<Blog> dynamicForeachTest(List<Integer> ids);
@Test public void dynamicForeachTest() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(3); ids.add(6); List<Blog> blogs = blogMapper.dynamicForeachTest(ids); for (Blog blog : blogs) System.out.println(blog); session.close(); }
2.单参数array数组的类型:测试
<select id="dynamicForeach2Test" resultType="Blog"> select * from t_blog where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection为array,对应的Mapper代码:spa
public List<Blog> dynamicForeach2Test(int[] ids);
java测试代码code
@Test public void dynamicForeach2Test() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); int[] ids = new int[] {1,3,6,9}; List<Blog> blogs = blogMapper.dynamicForeach2Test(ids); for (Blog blog : blogs) System.out.println(blog); session.close(); }
3.本身把参数封装成Map的类型对象
<select id="dynamicForeach3Test" resultType="Blog"> select * from t_blog where title like "%"#{title}"%" and id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码:
public List<Blog> dynamicForeach3Test(Map<String, Object> params);
java测试代码
@Test public void dynamicForeach3Test() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); final List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(3); ids.add(6); ids.add(7); ids.add(9); Map<String, Object> params = new HashMap<String, Object>(); params.put("ids", ids); params.put("title", "中国"); List<Blog> blogs = blogMapper.dynamicForeach3Test(params); for (Blog blog : blogs) System.out.println(blog); session.close(); }
<select id="dynamicForeachTest" resultType="Blog"> select * from t_blog where id in <!-- 遍历的对象是Map.Entry时,index表明对应的key,item表明对应的value --> <foreach collection="collection" index="key" item="value" open="(" separator="," close=")"> #{key}, #{value} </foreach> </select>
其对应的接口方法大概是以下这样:
public List<Blog> dynamicForeachTest(Set<Map.Entry<Integer, Integer>> ids);
二、bind标签,动态SQL中已经包含了这样一个新的标签,它的功能是在当前OGNL上下文中建立一个变量并绑定一个值。有了它之后咱们之前的模糊查询就能够改为这个样子:
<select id="fuzzyQuery" resultType="Blog" parameterType="java.lang.String"> <!-- bind标签用于建立新的变量 --> <bind name="titleLike" value="'%'+_parameter+'%'"/> select * from t_blog where title like #{titleLike} </select>