剛用到新框架的時候,總是要熟悉新框架的一些事情
比如Mybatis的動態sql就和之前的那些拼寫sql不同,下面根據網上看的來總結一下:
MyBatis的動態SQL是基於OGNL表達式的,它可以幫助我們方便的在SQL語句中實現某些邏輯。
MyBatis中用於實現動態SQL的元素主要有:
- if
- choose(when,otherwise)
- trim
- where
- set
- foreach
- <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>
- <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>
- <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>
- 如果傳入的是單參數且參數類型是一個List的時候,collection屬性值爲list
- 如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值爲array
- 如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map了,當然單參數也可以封裝成map,實際上如果你在傳入參數的時候,在MyBatis裏面也是會把它封裝成一個Map的,map的key就是參數名,所以這個時候collection屬性值就是傳入的List或array對象在自己封裝的map裏面的key
- <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>
- 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();
- }
- <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>
- public List<Blog> dynamicForeach2Test(int[] ids);
- @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();
- }
- <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>
- public List<Blog> dynamicForeach3Test(Map<String, Object> params);
- @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();
- }