Mybatis learn three (Dynamic sql statement)

The main dynamic sql statement following statement

1, dynamic SQL: if statement
2, dynamic SQL: if + where statement
3, dynamic SQL: if + set statement
4, Dynamic SQL: choose (when, otherwise) statement
5, dynamic SQL: trim statement
6, Dynamic SQL: SQL fragment
7, dynamic SQL: foreach statement

Several previous blog I learn CRUD operations on a table with mybatis, but SQL statements are relatively simple, if there are more complex SQL statements, frequently require stitching, and stitching SQL little attention it is easy to mistake this time we can mybatis with dynamic SQL, by if, choose, when, otherwise, trim, where, set, foreach and other labels to solve this problem, these labels can be combined into a very flexible SQL statements, but also very comfortable with them

  . 1  <? XML Version = "1.0" encoding = "UTF-. 8" ?> 
  2  <! DOCTYPE Mapper the PUBLIC "- // mybatis.org//DTD Mapper 3.0 // EN"
   . 3  "http://mybatis.org/ DTD /. 3-MyBatis-mapper.dtd " > 
  . 4  <-! namespace: namespace represented. Now the aim is to distinguish id. -> 
  . 5  < Mapper namespace = "com.zhiyou100.zhl.dao.UsersDao" > 
  . 6      < SQL id = "Detail" > 
  . 7          id, name, Age, Sex, Day
   . 8      </ SQL > 
  . 9      
10      <="com.zhiyou100.zhl.bean.Users">
 11         select * from users
 12         <where>
 13             <if test="name!=null">
 14                 and name=#{name}
 15             </if>
 16             <if test="sex!=null and sex!=''">
 17                 and sex=#{sex}
 18             </if>
 19         </where>
 20     </select>
 21     
 22     <update id="updateWhere">
 23         update users
 24         <set>
 25             <if test="name!=null">
 26                 name=#{name},
 27             </if>
 28             <if test="sex!=null">
 29                 sex=#{sex},
 30             </if>
 31             <if test="age>0">
 32                 age=#{age},
 33             </if>
 34             <if test="day!=null">
 35                 day=#{day}
 36             </if>
 37         </set>
 38         where id=#{id}
 39     </update>
 40     
 41     <select id="selByWhere2" resultType="com.zhiyou100.zhl.bean.Users">
 42         select
 43         <include refid="detail"/>
 44         from users
 45         <where>
 46             <choose>
 47                 <when test="name!=null and name!=''">
 48                     name like concat('%',#{name},'%')
 49                 </when>
 50                 <when test="sex!=null and sex!=''">
 51                     sex=#{sex}
 52                 </when>
 53                 <otherwise>
 54                     age>=#{age}
 55                 </otherwise>
 56             </choose>
 57         </where>
 58     </select>
 59     
 60     <select id="selByWhere3" resultType="com.zhiyou100.zhl.bean.Users">
 61         select
 62         <include refid="detail"/>
 63         from users
 64         <trim prefix="where" prefixOverrides="and | or">
 65             <if test="name!=null and name!=''">
 66                 and name=#{name}
 67             </if>
 68             <if test="sex!=null and sex!=''">
 69                 and sex=#{sex}
 70             </if>
 71             <if test="age>0">
 72                 and age=#{age}
 73             </if>
 74             <if test="day!=null and day!=''">
 75                 and day=#{day}
 76             </if>
 77         </trim>
 78     </select>
 79     
 80     <update id="updateWhere2">
 81         update users
 82         <trim prefix="set" suffixOverrides=",">
 83             <if test="name!=null">
 84                 name=#{name},
 85             </if>
 86             <if test="sex!=null">
 87                 sex=#{sex},
 88             </if>
 89             <if test="age>0">
 90                 age=#{age},
 91             </if>
 92             <if test="day!=null">
 93                 day=#{day}
 94             </if>
 95         </trim>
 96         where id=#{id}
 97     </update>
 98     
 99     <delete id="deleteByIds">
100         delete from users
101         <where>
102             <foreach collection="ids" open="id in(" close=")" separator="," item="id">
103                 #{id}
104             </foreach>
105         </where>
106     </delete>
107     
108     
109     <delete id="deleteByIds2">
110         delete from users where id in
111         <foreach collection="ids" open="(" close=")" separator="," item="id">
112             #{id}
113         </foreach>
114     </delete>
115     
116     
117 </mapper>

 

Guess you like

Origin www.cnblogs.com/murmansk/p/11442894.html