動態sql語句(即sql語句的拼接)


10.1  if標簽(屬性:test(判斷條件))

  缺點:如果僅僅是第二個條件滿足導致sql語句出錯(故使用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標簽會知道如果它包含的標簽中有返回值的話,就會插入where 若其以and或or開頭會將and或or剔除)

 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標簽會知道如果它包含的標簽中有返回值的話,就會插入set並且剔除最后一個滿足條件的逗號“,”)

 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(標簽when+標簽otherwise)----類似於switch語句,有且僅有一個條件會滿足

 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標記是一個格式化的標記,可以完成set或者是where標記的功能;)

  屬性:prefix:前綴      

        prefixoverrides:去掉前綴

        suffix:后綴      

        suffixoverrides:去掉后綴

 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片段------使用sql標簽定義(屬性:id);引用片段時用include標簽(屬性:refid)

10.7 foreach

  屬性:  collection:指定輸入對象中的集合屬性

               item:每次遍歷生成的對象

               open:開始遍歷時的拼接字符串

               close:結束時拼接的字符串

               separator:遍歷對象之間需要拼接的字符串

 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>

10.8模糊查詢-----like后使用concat函數拼接(‘%’,#{name},’%’)

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM