Dynamic sql statement (ie splicing sql statement)

10.1 if tags (attributes: test (determination condition))

  Disadvantage: If only the second condition is satisfied results in an error sql statement (so use if + where)

1 <select id="select1" resultType="com.zhiyou.clg.bean.User">
2         select *from user
3         <if test="name!=null">
4             where name=#{name}
5         </if>
6         <if test="age!=null and age!=''">
7             and age=#{age}
8         </if>
9     </select>

10.2 if + where (where the label will know if it is included in the tag that returns a value, then, if it will be inserted to where and or or and or or will begin culling)

 1 <select id="select2" resultType="com.zhiyou.clg.bean.User">
 2         select *from user
 3         <where>
 4             <if test="name!=null">
 5                 and name=#{name}
 6             </if>
 7             <if test="sex!=null">
 8                 and sex=#{sex}
 9             </if>
10             <if test="age!=null and age!=''">
11                 and age=#{age}
12             </if>
13         </where>
14     </select>

10.3 if + set (set tag if the tag knows it is included in the return value, then will insert and remove the last set to satisfy a condition comma ",")

 1 <update id="update1" parameterType="com.zhiyou.clg.bean.User">
 2         update user
 3         <set>
 4             <if test="name!=null">
 5                 name=#{name},
 6             </if>
 7             <if test="sex!=null">
 8                 sex=#{sex},
 9             </if>
10             <if test="age!=null and age!=''">
11                 age=#{age},
12             </if>
13         </set>
14         <where>
15             <if test="id!=null">
16                 id=#{id}
17             </if>
18         </where>
19     </update

10.4 choose (tag when + label otherwise) ---- similar to the switch statement, and only one of the conditions will be satisfied

 1 <sql id="usercolumn" >
 2          id,name,age,sex
 3      </sql>
 4     <select id="select3" resultType="com.zhiyou.clg.bean.User">
 5         select 
 6             <include refid="usercolumn"></include>
 7          from user
 8         <where>
 9             <choose>
10                 <when test="name!=null">
11                     and name=#{name}
12                 </when>
13                 <when test="age!=null and age!=''">
14                     and age=#{age}
15                 </when>
16                 <otherwise>
17                     and sex=#{sex}
18                 </otherwise>
19             </choose>
20         </where>
21     </select>

10.5 trim (trim tag is a formatting tag, where a complete set or selected function;)

  Properties: prefix: the prefix      

        prefixoverrides: remove prefix

        suffix: the suffix      

        suffixoverrides: remove the suffix

 1 <update id="update2" parameterType="com.zhiyou.clg.bean.User">
 2         update user
 3             <trim prefix="set" suffixOverrides=",">
 4                 <if test="name!=null">
 5                     name=#{name},
 6                 </if>
 7                 <if test="sex!=null">
 8                     sex=#{sex},
 9                 </if>
10                 <if test="age!=null and age!=''">
11                     age=#{age},
12                 </if>
13             </trim>
14         <where>
15             <if test="id!=null">
16                 id=#{id}
17             </if>
18         </where>
19     </update>

------ 10.6 sql fragment used sql tag definition (attributes: id); label include a reference fragment (attribute: refid)

10.7 foreach

  Properties: collection: Specifies the attributes set in the input object

               item: Object generated each iteration

               open: concatenate strings at the start of traversing

               close: stitching at the end of the string

               separator: the splice between the need to traverse the object string

 1 <sql id="namecolumn" >
 2          name
 3      </sql>
 4     <select id="select4" resultType="com.zhiyou.clg.bean.User">
 5         select 
 6             <include refid="namecolumn"></include>
 7         from user where id in 
 8             <foreach collection="ids" open="(" close=")" separator="," item="id">
 9                 #{id}
10             </foreach>
11     </select>

After 10.8 ----- like using fuzzy inquiry function concat splicing ( '%', # {name}, '%')

        name  like  concat(‘%’,#{name},’%’)

Guess you like

Origin www.cnblogs.com/lwgok1003/p/11442717.html