<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    StevenBot-Saltsam

    眼睛能裝下世界,為何卻裝不下眼淚? 一只風(fēng)箏一輩子只為一根線冒險(xiǎn)。 那不是一場(chǎng)游戲,為何總有一根線牽著心懷,隱隱作疼? 那不是一段邂逅,為何飄在橋上的影子,總纏進(jìn)夢(mèng)鄉(xiāng)? 那不是一個(gè)夢(mèng)境,為何你的溫柔私語,總是不經(jīng)意的響起?

    導(dǎo)航

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    datas link

    OSChinal Sources codes Library

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    動(dòng)態(tài)SQL語句--mybatis

                                                                              動(dòng)態(tài)SQL語句
            有些時(shí)候,sql語句where條件中,需要一些安全判斷,例如按性別檢索,如果傳入的參數(shù)是空的,此時(shí)查詢出的結(jié)果很可能是空的,也許我們需要參數(shù)為空 時(shí),是查出全部的信息。這是我們可以使用動(dòng)態(tài)sql,增加一個(gè)判斷,當(dāng)參數(shù)不符合要求的時(shí)候,我們可以不去判斷此查詢條件。
            下文均采用mysql語法和函數(shù)(例如字符串鏈接函數(shù)CONCAT)。

            源代碼http://limingnihao.javaeye.com/admin/blogs/782190

    3.1 if標(biāo)簽
     一個(gè)很普通的查詢:

    Xml代碼 
    <!-- 查詢學(xué)生list,like姓名 -->  
    <select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">  
        SELECT * from STUDENT_TBL ST    
    WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')   
    </select>  
    <!-- 查詢學(xué)生list,like姓名 -->
    <select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">
     SELECT * from STUDENT_TBL ST
    WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
    </select>

    但是此時(shí)如果studentName是null或空字符串,此語句很可能報(bào)錯(cuò)或查詢結(jié)果為空。此時(shí)我們使用if動(dòng)態(tài)sql語句先進(jìn)行判斷,如果值為null或等于空字符串,我們就不進(jìn)行此條件的判斷。

    修改為:

    Xml代碼 
    <!-- 查詢學(xué)生list,like姓名 -->  
    <select id=" getStudentListLikeName " parameterType="StudentEntity" resultMap="studentResultMap">  
        SELECT * from STUDENT_TBL ST   
        <if test="studentName!=null and studentName!='' ">  
            WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')   
        </if>  
    </select>  
    <!-- 查詢學(xué)生list,like姓名 -->
    <select id=" getStudentListLikeName " parameterType="StudentEntity" resultMap="studentResultMap">
     SELECT * from STUDENT_TBL ST
     <if test="studentName!=null and studentName!='' ">
      WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
     </if>
    </select>
     此時(shí),當(dāng)studentName的值為null或’’的時(shí)候,我們并不進(jìn)行where條件的判斷,所以當(dāng)studentName值為null或’’值,不附帶這個(gè)條件,所以查詢結(jié)果是全部。

     由于參數(shù)是Java的實(shí)體類,所以我們可以把所有條件都附加上,使用時(shí)比較靈活, new一個(gè)這樣的實(shí)體類,我們需要限制那個(gè)條件,只需要附上相應(yīng)的值就會(huì)where這個(gè)條件,相反不去賦值就可以不在where中判斷。


       代碼中的where標(biāo)簽,請(qǐng)參考3.2.1.

    Xml代碼 
    <!-- 查詢學(xué)生list,like姓名,=性別、=生日、=班級(jí),使用where,參數(shù)entity類型 -->  
    <select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">  
        SELECT * from STUDENT_TBL ST   
        <where>  
            <if test="studentName!=null and studentName!='' ">  
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')   
            </if>  
            <if test="studentSex!= null and studentSex!= '' ">  
                AND ST.STUDENT_SEX = #{studentSex}   
            </if>  
            <if test="studentBirthday!=null">  
                AND ST.STUDENT_BIRTHDAY = #{studentBirthday}   
            </if>  
            <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">  
                AND ST.CLASS_ID = #{classEntity.classID}   
            </if>  
        </where>  
    </select>  
    <!-- 查詢學(xué)生list,like姓名,=性別、=生日、=班級(jí),使用where,參數(shù)entity類型 -->
    <select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">
     SELECT * from STUDENT_TBL ST
     <where>
      <if test="studentName!=null and studentName!='' ">
       ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
      </if>
      <if test="studentSex!= null and studentSex!= '' ">
       AND ST.STUDENT_SEX = #{studentSex}
      </if>
      <if test="studentBirthday!=null">
        AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
      </if>
      <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">
       AND ST.CLASS_ID = #{classEntity.classID}
      </if>
     </where>
    </select>
    查詢,姓名中有‘李’,男,生日在‘1985-05-28’,班級(jí)在‘20000002’的學(xué)生。

    Java代碼 
    StudentEntity entity = new StudentEntity();   
    entity.setStudentName("李");   
    entity.setStudentSex("男");   
    entity.setStudentBirthday(StringUtil.parse("1985-05-28"));   
    entity.setClassEntity(classMapper.getClassByID("20000002"));   
    List<StudentEntity> studentList = studentMapper.getStudentListWhereEntity(entity);   
    for( StudentEntity entityTemp : studentList){   
        System.out.println(entityTemp.toString());   
    }  
    StudentEntity entity = new StudentEntity();
    entity.setStudentName("李");
    entity.setStudentSex("男");
    entity.setStudentBirthday(StringUtil.parse("1985-05-28"));
    entity.setClassEntity(classMapper.getClassByID("20000002"));
    List<StudentEntity> studentList = studentMapper.getStudentListWhereEntity(entity);
    for( StudentEntity entityTemp : studentList){
     System.out.println(entityTemp.toString());
    }

    3.2 where、set、trim標(biāo)簽

    3.2.1 where
    當(dāng)if標(biāo)簽較多時(shí),這樣的組合可能會(huì)導(dǎo)致錯(cuò)誤。例如,like姓名,等于指定性別等:

    Xml代碼 
    <!-- 查詢學(xué)生list,like姓名,=性別 -->  
    <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">  
        SELECT * from STUDENT_TBL ST   
            WHERE   
            <if test="studentName!=null and studentName!='' ">  
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')   
            </if>  
            <if test="studentSex!= null and studentSex!= '' ">  
                AND ST.STUDENT_SEX = #{studentSex}   
            </if>  
    </select>  
    <!-- 查詢學(xué)生list,like姓名,=性別 -->
    <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">
     SELECT * from STUDENT_TBL ST
      WHERE
      <if test="studentName!=null and studentName!='' ">
       ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
      </if>
      <if test="studentSex!= null and studentSex!= '' ">
       AND ST.STUDENT_SEX = #{studentSex}
      </if>
    </select>
     如果上面例子,參數(shù)studentName為null或’’,則或?qū)е麓藄ql組合成“WHERE AND”之類的關(guān)鍵字多余的錯(cuò)誤SQL。
     這時(shí)我們可以使用where動(dòng)態(tài)語句來解決。這個(gè)“where”標(biāo)簽會(huì)知道如果它包含的標(biāo)簽中有返回值的話,它就插入一個(gè)‘where’。此外,如果標(biāo)簽返回的內(nèi)容是以AND 或OR 開頭的,則它會(huì)剔除掉。
     上面例子修改為:

    Xml代碼 
    <!-- 查詢學(xué)生list,like姓名,=性別 -->  
    <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">  
        SELECT * from STUDENT_TBL ST   
        <where>  
            <if test="studentName!=null and studentName!='' ">  
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')   
            </if>  
            <if test="studentSex!= null and studentSex!= '' ">  
                AND ST.STUDENT_SEX = #{studentSex}   
            </if>  
        </where>  
    </select>  
    <!-- 查詢學(xué)生list,like姓名,=性別 -->
    <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">
     SELECT * from STUDENT_TBL ST
     <where>
      <if test="studentName!=null and studentName!='' ">
       ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
      </if>
      <if test="studentSex!= null and studentSex!= '' ">
       AND ST.STUDENT_SEX = #{studentSex}
      </if>
     </where>
    </select>

    3.2.2 set
    當(dāng)在update語句中使用if標(biāo)簽時(shí),如果前面的if沒有執(zhí)行,則或?qū)е露禾?hào)多余錯(cuò)誤。使用set標(biāo)簽可以將動(dòng)態(tài)的配置SET 關(guān)鍵字,和剔除追加到條件末尾的任何不相關(guān)的逗號(hào)。
    沒有使用if標(biāo)簽時(shí),如果有一個(gè)參數(shù)為null,都會(huì)導(dǎo)致錯(cuò)誤,如下示例:

    Xml代碼 
    <!-- 更新學(xué)生信息 -->  
    <update id="updateStudent" parameterType="StudentEntity">  
        UPDATE STUDENT_TBL   
           SET STUDENT_TBL.STUDENT_NAME = #{studentName},   
               STUDENT_TBL.STUDENT_SEX = #{studentSex},   
               STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},   
               STUDENT_TBL.CLASS_ID = #{classEntity.classID}   
         WHERE STUDENT_TBL.STUDENT_ID = #{studentID};   
    </update>  
    <!-- 更新學(xué)生信息 -->
    <update id="updateStudent" parameterType="StudentEntity">
     UPDATE STUDENT_TBL
        SET STUDENT_TBL.STUDENT_NAME = #{studentName},
            STUDENT_TBL.STUDENT_SEX = #{studentSex},
            STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
            STUDENT_TBL.CLASS_ID = #{classEntity.classID}
      WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
    </update>

     使用set+if標(biāo)簽修改后,如果某項(xiàng)為null則不進(jìn)行更新,而是保持?jǐn)?shù)據(jù)庫原值。如下示例:

    Xml代碼 
    <!-- 更新學(xué)生信息 -->  
    <update id="updateStudent" parameterType="StudentEntity">  
        UPDATE STUDENT_TBL   
        <set>  
            <if test="studentName!=null and studentName!='' ">  
                STUDENT_TBL.STUDENT_NAME = #{studentName},   
            </if>  
            <if test="studentSex!=null and studentSex!='' ">  
                STUDENT_TBL.STUDENT_SEX = #{studentSex},   
            </if>  
            <if test="studentBirthday!=null ">  
                STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},   
            </if>  
            <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">  
                STUDENT_TBL.CLASS_ID = #{classEntity.classID}   
            </if>  
        </set>  
        WHERE STUDENT_TBL.STUDENT_ID = #{studentID};   
    </update>  
    <!-- 更新學(xué)生信息 -->
    <update id="updateStudent" parameterType="StudentEntity">
     UPDATE STUDENT_TBL
     <set>
      <if test="studentName!=null and studentName!='' ">
       STUDENT_TBL.STUDENT_NAME = #{studentName},
      </if>
      <if test="studentSex!=null and studentSex!='' ">
       STUDENT_TBL.STUDENT_SEX = #{studentSex},
      </if>
      <if test="studentBirthday!=null ">
       STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
      </if>
      <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">
       STUDENT_TBL.CLASS_ID = #{classEntity.classID}
      </if>
     </set>
     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
    </update>
    3.2.3 trim
     trim是更靈活的去處多余關(guān)鍵字的標(biāo)簽,他可以實(shí)踐where和set的效果。


     where例子的等效trim語句:

    Xml代碼 
    <!-- 查詢學(xué)生list,like姓名,=性別 -->  
    <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">  
        SELECT * from STUDENT_TBL ST   
        <trim prefix="WHERE" prefixOverrides="AND|OR">  
            <if test="studentName!=null and studentName!='' ">  
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')   
            </if>  
            <if test="studentSex!= null and studentSex!= '' ">  
                AND ST.STUDENT_SEX = #{studentSex}   
            </if>  
        </trim>  
    </select>  
    <!-- 查詢學(xué)生list,like姓名,=性別 -->
    <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">
     SELECT * from STUDENT_TBL ST
     <trim prefix="WHERE" prefixOverrides="AND|OR">
      <if test="studentName!=null and studentName!='' ">
       ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
      </if>
      <if test="studentSex!= null and studentSex!= '' ">
       AND ST.STUDENT_SEX = #{studentSex}
      </if>
     </trim>
    </select>
    set例子的等效trim語句:

    Xml代碼 
    <!-- 更新學(xué)生信息 -->  
    <update id="updateStudent" parameterType="StudentEntity">  
        UPDATE STUDENT_TBL   
        <trim prefix="SET" suffixOverrides=",">  
            <if test="studentName!=null and studentName!='' ">  
                STUDENT_TBL.STUDENT_NAME = #{studentName},   
            </if>  
            <if test="studentSex!=null and studentSex!='' ">  
                STUDENT_TBL.STUDENT_SEX = #{studentSex},   
            </if>  
            <if test="studentBirthday!=null ">  
                STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},   
            </if>  
            <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">  
                STUDENT_TBL.CLASS_ID = #{classEntity.classID}   
            </if>  
        </trim>  
        WHERE STUDENT_TBL.STUDENT_ID = #{studentID};   
    </update>  
    <!-- 更新學(xué)生信息 -->
    <update id="updateStudent" parameterType="StudentEntity">
     UPDATE STUDENT_TBL
     <trim prefix="SET" suffixOverrides=",">
      <if test="studentName!=null and studentName!='' ">
       STUDENT_TBL.STUDENT_NAME = #{studentName},
      </if>
      <if test="studentSex!=null and studentSex!='' ">
       STUDENT_TBL.STUDENT_SEX = #{studentSex},
      </if>
      <if test="studentBirthday!=null ">
       STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
      </if>
      <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">
       STUDENT_TBL.CLASS_ID = #{classEntity.classID}
      </if>
     </trim>
     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
    </update>

    3.3 choose (when, otherwise)
             有時(shí)候我們并不想應(yīng)用所有的條件,而只是想從多個(gè)選項(xiàng)中選擇一個(gè)。MyBatis提供了choose 元素,按順序判斷when中的條件出否成立,如果有一個(gè)成立,則choose結(jié)束。當(dāng)choose中所有when的條件都不滿則時(shí),則執(zhí)行 otherwise中的sql。類似于Java 的switch 語句,choose為switch,when為case,otherwise則為default。
             if是與(and)的關(guān)系,而choose是或(or)的關(guān)系。


             例如下面例子,同樣把所有可以限制的條件都寫上,方面使用。選擇條件順序,when標(biāo)簽的從上到下的書寫順序:

    Xml代碼 
    <!-- 查詢學(xué)生list,like姓名、或=性別、或=生日、或=班級(jí),使用choose -->  
    <select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">  
        SELECT * from STUDENT_TBL ST   
        <where>  
            <choose>  
                <when test="studentName!=null and studentName!='' ">  
                        ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')   
                </when>  
                <when test="studentSex!= null and studentSex!= '' ">  
                        AND ST.STUDENT_SEX = #{studentSex}   
                </when>  
                <when test="studentBirthday!=null">  
                    AND ST.STUDENT_BIRTHDAY = #{studentBirthday}   
                </when>  
                <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">  
                    AND ST.CLASS_ID = #{classEntity.classID}   
                </when>  
                <otherwise>  
                       
                </otherwise>  
            </choose>  
        </where>  
    </select>  
    <!-- 查詢學(xué)生list,like姓名、或=性別、或=生日、或=班級(jí),使用choose -->
    <select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">
     SELECT * from STUDENT_TBL ST
     <where>
      <choose>
       <when test="studentName!=null and studentName!='' ">
         ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
       </when>
       <when test="studentSex!= null and studentSex!= '' ">
         AND ST.STUDENT_SEX = #{studentSex}
       </when>
       <when test="studentBirthday!=null">
        AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
       </when>
       <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">
        AND ST.CLASS_ID = #{classEntity.classID}
       </when>
       <otherwise>
        
       </otherwise>
      </choose>
     </where>
    </select>
    3.4 foreach
    對(duì)于動(dòng)態(tài)SQL 非常必須的,主是要迭代一個(gè)集合,通常是用于IN 條件。
    List 實(shí)例將使用“list”做為鍵,數(shù)組實(shí)例以“array” 做為鍵。

     

     3.4.1參數(shù)為list實(shí)例的寫法:
    SQL寫法:

    Xml代碼 
    <select id="getStudentListByClassIDs" resultMap="studentResultMap">  
        SELECT * FROM STUDENT_TBL ST   
         WHERE ST.CLASS_ID IN    
         <foreach collection="list" item="classList"  open="(" separator="," close=")">  
            #{classList}   
         </foreach>      
    </select>  
    <select id="getStudentListByClassIDs" resultMap="studentResultMap">
     SELECT * FROM STUDENT_TBL ST
      WHERE ST.CLASS_ID IN
      <foreach collection="list" item="classList"  open="(" separator="," close=")">
       #{classList}
      </foreach> 
    </select>
    接口的方法聲明:

    Java代碼 
    public List<StudentEntity> getStudentListByClassIDs(List<String> classList);  
    public List<StudentEntity> getStudentListByClassIDs(List<String> classList); 測(cè)試代碼,查詢學(xué)生中,在20000002、20000003這兩個(gè)班級(jí)的學(xué)生:

    Java代碼 
    List<String> classList = new ArrayList<String>();   
    classList.add("20000002");   
    classList.add("20000003");   
      
    List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(classList);   
    for( StudentEntity entityTemp : studentList){   
        System.out.println(entityTemp.toString());   
    }  
    List<String> classList = new ArrayList<String>();
    classList.add("20000002");
    classList.add("20000003");

    List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(classList);
    for( StudentEntity entityTemp : studentList){
     System.out.println(entityTemp.toString());
    }

     3.4.2參數(shù)為Array實(shí)例的寫法:
    SQL語句:

    Xml代碼 
    <select id="getStudentListByClassIDs" resultMap="studentResultMap">  
        SELECT * FROM STUDENT_TBL ST   
         WHERE ST.CLASS_ID IN    
         <foreach collection="array" item="ids"  open="(" separator="," close=")">  
            #{ids}   
         </foreach>  
    </select>  
    <select id="getStudentListByClassIDs" resultMap="studentResultMap">
     SELECT * FROM STUDENT_TBL ST
      WHERE ST.CLASS_ID IN
      <foreach collection="array" item="ids"  open="(" separator="," close=")">
       #{ids}
      </foreach>
    </select>

     接口的方法聲明:

    Java代碼 
    public List<StudentEntity> getStudentListByClassIDs(String[] ids);  
    public List<StudentEntity> getStudentListByClassIDs(String[] ids);測(cè)試代碼,查詢學(xué)生中,在20000002、20000003這兩個(gè)班級(jí)的學(xué)生:

    Java代碼 
    String[] ids = new String[2];   
    ids[0] = "20000002";   
    ids[1] = "20000003";   
    List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(ids);   
    for( StudentEntity entityTemp : studentList){   
        System.out.println(entityTemp.toString());   
    }  

    posted on 2011-11-10 20:34 Steven_bot 閱讀(3791) 評(píng)論(0)  編輯  收藏 所屬分類: 遇到的一些問題

    主站蜘蛛池模板: 亚洲精品无码午夜福利中文字幕| 黄色a级免费网站| 国产精品亚洲а∨无码播放| 午夜精品在线免费观看| 99re免费视频| 91福利免费网站在线观看| 亚洲AV日韩综合一区| 亚洲乱码一二三四区乱码| 久久亚洲精品国产精品| 在线a亚洲v天堂网2019无码| 青青青国产免费一夜七次郎| 亚洲三级在线免费观看| 久久青草91免费观看| 久久久WWW成人免费精品| 苍井空亚洲精品AA片在线播放| 亚洲一级毛片免费看| 久久久亚洲精品视频| 国产精品亚洲成在人线| 亚洲熟女少妇一区二区| 亚洲午夜精品久久久久久浪潮| 国产精品无码素人福利免费| 好吊妞788免费视频播放 | 波多野结衣免费视频观看| 老司机在线免费视频| **俄罗斯毛片免费| 久久久久国产精品免费网站| a毛看片免费观看视频| 国产精品福利片免费看| fc2成年免费共享视频18| 国产精品亚洲专区一区| 色噜噜的亚洲男人的天堂| 亚洲国产成人久久精品软件| 亚洲乱妇熟女爽到高潮的片| 日本亚洲色大成网站www久久| 亚洲私人无码综合久久网| 国产亚洲福利在线视频| 亚洲av无码偷拍在线观看| 亚洲AV色欲色欲WWW| 一级毛片视频免费观看| 伊人免费在线观看| 亚洲精品免费在线视频|