MyBatis動態SQL語句

關鍵字

1.if 條件

2.choose , when 和 otherwise條件

3.where 條件

4.trim 條件

5.forEach循環

6.set 條件

 

一、if主要針對Map集合或者實體類

複製代碼
    <select id="selectduo" parameterType="Map" resultType="student" >
        select * from student s where 1=1
            <if test="sno!=null">
            and s.sno=#{sno} 
            </if>
            <if test="sname!=null">
            and s.sname like #{sname}
            </if>
    </select>
複製代碼

 JUnit測試用例,判斷map集合裏的值

複製代碼
    @Test
    public void selectduo() {
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("sno", 107);
        map.put("sname", "%王%");
        List<Student> st=sm.selectduo(map);
        for(Student tt:st){
            System.out.println(tt);
        }
    }
複製代碼

 

二、choose , when 和 otherwise條件

複製代碼
    <select id="selectduo" parameterType="Map" resultType="student" >
        select * from student s where 1=1
        <choose>
            <when test="aaa=='zzz'">
                and s.sno=#{sno}
            </when>
            <when test="aaa=='bbb'">
                and s.sname like#{sname}
            </when>
            <!-- 都不符合時走這個 -->
            <otherwise>
                and 2=2
            </otherwise>            
        </choose>
    </select>
複製代碼

 JUnit測試用例

複製代碼
    @Test
    public void selectduo() {
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("aaa", "bbb");
        map.put("sno", 107);
        map.put("sname", "%王%");
        List<Student> st=sm.selectduo(map);
        for(Student tt:st){
            System.out.println(tt);
        }
    }
複製代碼

 

三、where:自動加上where,如果where子句以and或者or開頭,則自動刪除第一個and或者or。所以我們不需要自己加where

複製代碼
    <select id="selectduo" parameterType="Map" resultType="student" >
        select * from student s 
        <where>
            <if test="sno!=null">
            and s.sno=#{sno} 
            </if>
            <if test="sname!=null">
            and s.sname like #{sname}
            </if>
        </where>
    </select>
複製代碼

 JUnit測試用例

複製代碼
    @Test
    public void selectduo() {
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("sno", 107);
        map.put("sname", "%王%");
        List<Student> st=sm.selectduo(map);
        for(Student tt:st){
            System.out.println(tt);
        }
    }
複製代碼

 

 

四、trim 條件:trim條件和where條件類似但是功能更強大:不僅可以替換掉子句開頭的and或者or,還提供了加前綴和後綴的功能。

複製代碼
     <select id="selectduo" parameterType="Map" resultType="student" >
        select * from student s 
        <!-- 把第一個and或者or替換成where,替換最後一個用suffix="" suffixOverrides="" -->
        <trim prefix="where" prefixOverrides="and|or" >
            <if test="sno!=null">
            and s.sno=#{sno} 
            </if>
            <if test="sname!=null">
            and s.sname like #{sname}
            </if>
        </trim>
    </select>
複製代碼

 JUnit測試用例

複製代碼
    @Test
    public void selectduo() {
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("sno", 107);
        map.put("sname", "%王%");
        List<Student> st=sm.selectduo(map);
        for(Student tt:st){
            System.out.println(tt);
        }
    }
複製代碼

五、forEach循環

1.用list傳值是

複製代碼
     <select id="selectduo" parameterType="list" resultType="student" >
        select * from student s 
         <where>
         s.sno in
         <!-- 自己定義的名稱,傳來是什麼類型變量 ,後面三個屬性拼接成(,,,) -->
         <foreach item="test" collection="list" open="(" separator="," close=")">
             #{test}
         </foreach>
         </where>
    </select>
複製代碼

JUnit測試用例

複製代碼
    @Test
    public void selectduo() {
        List<Integer> snos=new ArrayList<>();
        snos.add(66);
        snos.add(110);
        snos.add(107);
        List<Student> st=sm.selectduo(snos);
        for(Student tt:st){
            System.out.println(tt);
        }
    }
複製代碼

2.用map傳值時

複製代碼
     <select id="selectduo" parameterType="Map" resultType="student" >
        select * from student s 
         <where>
         s.sno in
         <!-- 自己定義的名稱,Map的key名字 ,後面三個屬性拼接成(,,,) -->
         <foreach item="test" collection="snoslist" open="(" separator="," close=")">
             #{test}
         </foreach>
         </where>
    </select>
複製代碼

JUnit測試用例

複製代碼
    @Test
    public void selectduo() {
        List<Integer> snos=new ArrayList<>();
        snos.add(66);
        snos.add(110);
        snos.add(107);
        Map<String , Object> map=new HashMap<String,Object>();
        map.put("snoslist", snos);
        List<Student> st=sm.selectduo(map);
        for(Student tt:st){
            System.out.println(tt);
        }
    }
複製代碼

結果都是一樣的

六、set 條件

自動加上set,自動去除最後一個逗號

複製代碼
    <update id="update" parameterType="student">
        update student s
        <set>
            <if test="sname!=null">s.sname=#{sname},</if>
            <if test="ssex!=null">s.ssex=#{ssex},</if>
            <if test="sclass!=null">s.sclass=#{sclass}</if>
            <where>
                s.sno=#{sno}
            </where>
        </set>
    </update>
複製代碼

JUnit測試用例

複製代碼
    @Test
    public void update() {
        Student st=new Student(66, "濛濛", "男", 95033,null);
        int m=sm.update(st);
        System.out.println(m);
    }
    
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章