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>