Mybatis Series 9

In the previous series 8, the query of mybatis was briefly introduced. So far, CRUD has been finished.

This article will introduce the powerful dynamic SQL of mybatis.

So, here comes the question: What is dynamic SQL? What does dynamic SQL do?

In the traditional method of using JDBC, I believe that when you combine complex SQL statements, you need to splicing them. The dynamic SQL function of Mybatis is to solve this problem. It can be combined into very flexible SQL statements through the if, choose, when, otherwise, trim, where, set, and foreach tags, thereby improving the efficiency of developers.

Let's feel the charm of Mybatis dynamic SQL:

1. if: You can judge, so can I!

As a programmer, who doesn't understand if! You can also use if in mybatis:

1 <select id="findUserById" resultType="user">
2            select * from user where 
3            <if test="id != null">
4                id=#{id}
5            </if>
6             and deleteFlag=0;
7 </select>

The above example: If the incoming id is not empty, then the SQL will splicing id = #{id}. I believe everyone can understand this.

Careful people will find a problem: "You are wrong! If the id you pass in is null, then your final SQL statement will become select * from user where and deleteFlag=0, there is a problem with this statement!"

Yes, at this time, the where tag of mybatis should make a grand debut:

2. where, with me, SQL statement splicing conditions are all clouds!

Let's transform the above example through where:

 1 <select id="findUserById" resultType="user">
 2            select * from user 
 3            <where>
 4                <if test="id != null">
 5                    id=#{id}
 6                </if>
 7                and deleteFlag=0;
 8            </where>
 9  </select>
10  

Some people are about to ask: "What are you doing! Compared with the above, isn't there an extra where tag! Then will this appear?

select * from user where and deleteFlag=0 ?”

Indeed, on the surface, it is just an extra where tag, but in essence, mybatis processes it. When it encounters AND or OR, it knows how to deal with it.

In fact, we can customize this processing rule through the trim tag.

3. trim : My site, I call the shots!

The above where tag, in fact, the trim can be expressed as follows:

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

It means: When WHERE is followed by AND or OR, remove AND or OR.

In addition to WHERE, there is actually a more classic implementation, that is SET.

4. set: trust me, no mistakes!
 1 <update id="updateUser" parameterType="com.dy.entity.User">
 2           update user set 
 3           <if test="name != null">
 4               name = #{name},
 5           </if> 
 6           <if test="password != null">
 7               password = #{password},
 8           </if> 
 9           <if test="age != null">
10               age = #{age}
11           </if> 
12           <where>
13               <if test="id != null">
14                   id = #{id}
15               </if>
16               and deleteFlag = 0;
17           </where>
18 </update>

The problem comes again: "If I only have name not null, then this SQL will not become

update set name = #{name}, where ........ ?

The comma after your name will cause an error! "

Yes, at this time, you can use the set tag provided by mybatis for us. The following is after the transformation through the set tag:

 1 <update id="updateUser" parameterType="com.dy.entity.User">
 2            update user
 3         <set>
 4           <if test="name != null">name = #{name},</if> 
 5              <if test="password != null">password = #{password},</if> 
 6              <if test="age != null">age = #{age},</if> 
 7         </set>
 8            <where>
 9                <if test="id != null">
10                    id = #{id}
11                </if>
12                and deleteFlag = 0;
13            </where>
14 </update>

This can be expressed in trim as:

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

WHERE is the prefixOverrides (prefix) used, SET is the suffixOverrides (suffix) used, see it!

5. foreach: You have for, I have foreach, don't think it's just you!

There is for in java, which can be looped through for. Similarly, there is foreach in mybatis, which can be used for looping. Of course, the objects of the loop are mainly java containers and arrays.

1 <select id="selectPostIn" resultType="domain.blog.Post">
2   SELECT *
3   FROM POST P
4   WHERE ID in
5   <foreach item="item" index="index" collection="list"
6       open="(" separator="," close=")">
7         #{item}
8   </foreach>
9 </select>

Pass a List instance or array as a parameter object to MyBatis, when doing so, MyBatis will automatically wrap it in a Map with the name as the key. List instances will have "list" as the key, while array instances will have the key "array". Similarly, when the object of the loop is a map, the index is actually the key of the map.

6. choose: I chose you, you chose me!

Java has switch, and mybatis has choose.

 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>

In the above example: When both title and author are not null, then choose one of the two (the former is preferred), if both are null, then choose otherwise, if only one of tilte and author is not null, then choose The one that is not null. Looking at the dynamic SQL of mybatis, it is powerful and simple. I believe you can use it with a simple look.

The next article will combine the source code of mybatis to analyze the whole process of executing a sql statement.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325332452&siteId=291194637