Mybatis基於xml的動態sql實現


動態sql可以很方便的拼接sql語句,主要用於復合條件查詢;

主要通過這幾個標簽實現:

if 標簽:

where 標簽

choose標簽:

foreach標簽:


if 標簽:

<select id="selectStudentByIf" resultType="Student">
        select id,name,age,score from student where 1=1
        <if test="name != null and name != ''">
            and name like '%' #{name} '%'
        </if>
        <if test="age > 0">
            and age < #{age}
        </if>
 </select>

使用if標簽判斷參數,可以使用where 1=1,后面跟上if標簽,這樣可以避免因為‘and’導致sql語句沖突;

where 標簽

<select id="selectStudentByWhere" resultType="Student">
        select id,name,age,score from student
        <where>
            <if test="name != null and name != ''">
                and name like '%' #{name} '%'
            </if>
            <if test="age > 0">
                and age < #{age}
            </if>
        </where>
 </select>

  

由於使用where 1=1 對查詢性能也有一定的影響;可以使用where嵌套 if 標簽,可以不需要使用上面“where 1=1”;

where標簽會自動過濾掉if標簽里sql語句的第一個and連接詞;

作用:用來簡化SQL語句中的where條件,可以嵌套其他標簽;

choose標簽:

<select id="selectStudentByChoose" resultType="Student">
        select id,name,age,score from student
        <where>
            <choose>
                <when test="name != null and name != ''">
                    name like '%' #{name} '%'
                </when>
                <when test="age > 0">
                    age < #{age}
                </when>
                <otherwise>
                    1 != 1
                </otherwise>
            </choose>
        </where>
  </select>

  

 表示:

當查詢條件有name時,不管有沒有age,都只按name查詢;

當查詢條件沒有name時,才按age進行查詢;

當name和age都沒有時,查詢不到任何數據。

作用:類似於switch語句,選擇多個條件中的一個;

foreach標簽:

foreach用來遍歷,遍歷的對象可以是數組,也可以是集合。

屬性:

collection可以指定遍歷對象的類型:array,list

item是指定的集合中每一個元素迭代時候的別名;

separator表示在每次進行迭代之間以什么符號作為分隔符;

open表示該語句以什么開始,close表示以什么結束;

 

<select id="selectStudentByForeachArray" resultType="Student">
        select id,name,age,score from student
         <if test="array != null and array.length > 0">
             where id in
             <foreach collection="array" item="id" open="(" close=")" separator=",">
                 #{id}
             </foreach>
         </if>
    </select>

  

表示遍歷一個數組,迭代別名為id;

    <select id="selectStudentByForeachList" resultType="Student">
        select id,name,age,score from student
         <if test="list != null and list.size > 0">
             where id in
             <foreach collection="list" item="id" open="(" close=")" separator=",">
                 #{id}
             </foreach>
         </if>
    </select>

  

遍歷一個list集合;

    <select id="selectStudentByForeachList2" resultType="Student">
        select id,name,age,score from student
         <if test="list != null and list.size > 0">
             where id in
             <foreach collection="list" item="stu" open="(" close=")" separator=",">
                 #{stu.id}
             </foreach>
         </if>
    </select>

遍歷一個list,list中每個元素為對象;遍歷對象的id屬性;


免責聲明!

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



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