Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MyBatis Dynamic Sql Statement (OGNL Syntax)


May 16, 2021 MyBatis


Table of contents


1、if

  1. <select id="select"
  2. resultType="Blog">
  3. SELECT * FROM BLOG
  4. WHERE state = ‘ACTIVE’
  5. <if test="title != null">
  6. AND title like #{title}
  7. </if>
  8. <if test="name!= null">
  9. AND name like #{title}
  10. </if>
  11. </select>

2、where

In a situation like the one above, if there are no conditions behind where, then you need to write the if judgment directly (the beginning is and / or, it will be removed)

  1. <select id="select"
  2. resultType="Blog">
  3. SELECT * FROM BLOG
  4. <where>
  5. <if test="title != null">
  6. AND title like #{title}
  7. </if>
  8. <if test="name!= null">
  9. AND name like #{title}
  10. </if>
  11. <where>
  12. </select>

3、choose(when、otherwise)

choose is equivalent to the switch statement in java. otherwise (otherwise)

  1. <select id="findActiveBlogLike"
  2. resultType="Blog">
  3. SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  4. <choose>
  5. <when test="title != null">
  6. AND title like #{title}
  7. </when>
  8. <when test="author != null and author.name != null">
  9. AND author_name like #{author.name}
  10. </when>
  11. <otherwise>
  12. AND featured = 1
  13. </otherwise>
  14. </choose>
  15. </select>

4、tirm

tirm

prefix: Prefixoverride: Remove the first and or or

select * from test
<trim prefix="WHERE" prefixoverride="AND丨OR">
      <if test="a!=null and a!=' '">AND a=#{a}<if>
      <if test="b!=null and b!=' '">AND a=#{a}<if>
</trim>


5、set

The set element is primarily used in update operations, and if the contained statement ends with a comma, the comma is ignored and an error occurs if the set contains empty content.

  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>

6、foreach

Foreach is primarily used in building in conditions

  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>

open separator close

Equivalent to in (? , ? ,

What if it's a 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 corresponds to the map key, like this

  1. List<Integer> ids = new ArrayList<Integer>();
  2. ids.add(1);
  3. ids.add(2);
  4. ids.add(3);
  5. ids.add(6);
  6. ids.add(7);
  7. ids.add(9);
  8. Map<String, Object> params = new HashMap<String, Object>();
  9. params.put("ids", ids);