Easy-to-understand MyBatis dynamic SQL statement

1. If statement

Requirements: Query blogs based on author name and blog name! If the author's name is empty, then only query based on the blog name, otherwise, query based on the author's name

<!--需求1:
根据作者名字和博客名字来查询博客!
如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询
select * from blog where title = #{title} and author = #{author}
-->
<select id="queryBlogIf" parameterType="map" resultType="blog">
 select * from blog where
 <if test="title != null">
  title = #{title}
 </if>
 <if test="author != null">
  and author = #{author}
 </if>
</select>

In this way, we can see that if author is equal to null, then the query statement is  select * from user where title=#{title}, but what if the title is empty? Then the query statement is  select * from user where and author=#{author}, this is a wrong SQL statement, how to solve it? Look at the where statement below!

2. Where statement

Modify the above SQL statement:

<select id="queryBlogIf" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <if test="title != null">
   title = #{title}
  </if>
  <if test="author != null">
   and author = #{author}
  </if>
 </where>
</select>

The where element will only insert a "WHERE" clause if the child element returns nothing. Also, if clauses begin with "AND" or "OR", the where element strips them out as well.

If the where element is not what you expected, you can also customize the function of the where element by customizing the trim element.

2.1 A custom trim element equivalent to the where element

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>

3. Set statement

Similarly, the above query SQL statement contains the where keyword, if it contains the set keyword when performing an update operation, how do we deal with it?

<!--注意set是用的逗号隔开-->
<update id="updateBlog" parameterType="map">
 update blog
 <set>
  <if test="title != null">
   title = #{title},
  </if>
  <if test="author != null">
   author = #{author}
  </if>
 </set>
 where id = #{id};
</update>

In this example, the set element will dynamically insert the SET keyword at the beginning of the line, and will remove extra commas (these commas were introduced when assigning values ​​​​to columns using conditional statements)

3.1 Custom trim element equivalent to set element

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

4. Choose statement

Sometimes, we don't want to use all the query conditions, we just want to choose one of them, and only one of the query conditions is satisfied. Using the choose tag can solve this kind of problem, similar to Java's switch statement

<select id="queryBlogChoose" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <choose>
   <when test="title != null">
    title = #{title}
   </when>
   <when test="author != null">
    and author = #{author}
   </when>
   <otherwise>
    and views = #{views}
   </otherwise>
  </choose>
 </where>
</select>

5. Foreach statement

Change the id of the first three data in the database to 1,2,3;

Requirement: We need to query the blog information with ids 1, 2, and 3 in the blog table

<select id="queryBlogForeach" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <!--
  collection:指定输入对象中的集合属性
  item:每次遍历生成的对象
  open:开始遍历时的拼接字符串
  close:结束时拼接的字符串
  separator:遍历对象之间需要拼接的字符串
  select * from blog where 1=1 and (id=1 or id=2 or id=3)
  -->
  <foreach collection="ids" item="id" open="and (" close=")"
  separator="or">
   id=#{id}
  </foreach>
 </where>
</select>

6. SQL fragment

Sometimes we may use a certain sql statement a lot. In order to increase the reusability of the code and simplify the code, we need to extract these codes and call them directly when using them.

Extract the SQL fragment:

<sql id="if-title-author">
 <if test="title != null">
  title = #{title}
 </if>
 <if test="author != null">
  and author = #{author}
 </if>
</sql>

Quoting the SQL snippet:

<select id="queryBlogIf" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace-->
  <include refid="if-title-author"></include>
  <!-- 在这里还可以引用其他的 sql 片段 -->
 </where>
</select>

Notice:

  • It is best to define sql fragments based on a single table to improve the reusability of fragments

  • Do not include where in the sql fragment

7. Bind element

The bind element allows you to create a variable outside of an OGNL expression and bind it to the current context. for example:

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>
 

Guess you like

Origin blog.csdn.net/weixin_45740811/article/details/128929745