mybatis动态SQL语句
三、動(dòng)態(tài)SQL語(yǔ)句
??????? 有些時(shí)候,sql語(yǔ)句where條件中,需要一些安全判斷,例如按性別檢索,如果傳入的參數(shù)是空的,此時(shí)查詢出的結(jié)果很可能是空的,也許我們需要參數(shù)為空時(shí),是查出全部的信息。這是我們可以使用動(dòng)態(tài)sql,增加一個(gè)判斷,當(dāng)參數(shù)不符合要求的時(shí)候,我們可以不去判斷此查詢條件。
??????? 下文均采用mysql語(yǔ)法和函數(shù)(例如字符串鏈接函數(shù)CONCAT)。
1 if標(biāo)簽
<!-- 查詢學(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或空字符串,此語(yǔ)句很可能報(bào)錯(cuò)或查詢結(jié)果為空。此時(shí)我們使用if動(dòng)態(tài)sql語(yǔ)句先進(jìn)行判斷,如果值為null或等于空字符串,我們就不進(jìn)行此條件的判斷。
修改為:
<!-- 查詢學(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中判斷。
<!-- 查詢學(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>2 where、set、trim標(biāo)簽
2.1 where
? ?當(dāng)if標(biāo)簽較多時(shí),這樣的組合可能會(huì)導(dǎo)致錯(cuò)誤。例如,like姓名,等于指定性別等:
如果上面例子,參數(shù)studentName為null或’’,則或?qū)е麓藄ql組合成“WHERE AND”之類的關(guān)鍵字多余的錯(cuò)誤SQL。
?這時(shí)我們可以使用where動(dòng)態(tài)語(yǔ)句來(lái)解決。這個(gè)“where”標(biāo)簽會(huì)知道如果它包含的標(biāo)簽中有返回值的話,它就插入一個(gè)‘where’。此外,如果標(biāo)簽返回的內(nèi)容是以AND 或OR 開(kāi)頭的,則它會(huì)剔除掉。
?上面例子修改為:
2.2 set
當(dāng)在update語(yǔ)句中使用if標(biāo)簽時(shí),如果前面的if沒(méi)有執(zhí)行,則或?qū)е露禾?hào)多余錯(cuò)誤。使用set標(biāo)簽可以將動(dòng)態(tài)的配置SET 關(guān)鍵字,和剔除追加到條件末尾的任何不相關(guān)的逗號(hào)。
沒(méi)有使用if標(biāo)簽時(shí),如果有一個(gè)參數(shù)為null,都會(huì)導(dǎo)致錯(cuò)誤,如下示例:
使用set+if標(biāo)簽修改后,如果某項(xiàng)為null則不進(jìn)行更新,而是保持?jǐn)?shù)據(jù)庫(kù)原值。如下示例:
<!-- 更新學(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>
2.3 trim
?trim是更靈活的去處多余關(guān)鍵字的標(biāo)簽,他可以實(shí)踐where和set的效果。
?where例子的等效trim語(yǔ)句:
set例子的等效trim語(yǔ)句:
<!-- 更新學(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 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 語(yǔ)句,choose為switch,when為case,otherwise則為default。
???????? if是與(and)的關(guān)系,而choose是或(or)的關(guān)系。
???????? 例如下面例子,同樣把所有可以限制的條件都寫上,方面使用。選擇條件順序,when標(biāo)簽的從上到下的書(shū)寫順序:
4 foreach
對(duì)于動(dòng)態(tài)SQL 非常必須的,主是要迭代一個(gè)集合,通常是用于IN 條件。
List 實(shí)例將使用“l(fā)ist”做為鍵,數(shù)組實(shí)例以“array” 做為鍵。
foreach屬性
| item | 循環(huán)體中的具體對(duì)象。支持屬性的點(diǎn)路徑訪問(wèn),如item.age,item.info.details。 具體說(shuō)明:在list和數(shù)組中是其中的對(duì)象,在map中是value。 該參數(shù)為必選。 |
| collection | 要做foreach的對(duì)象,作為入?yún)r(shí),List<?>對(duì)象默認(rèn)用list代替作為鍵,數(shù)組對(duì)象有array代替作為鍵,Map對(duì)象沒(méi)有默認(rèn)的鍵。 當(dāng)然在作為入?yún)r(shí)可以使用@Param("keyName")來(lái)設(shè)置鍵,設(shè)置keyName后,list,array將會(huì)失效。 除了入?yún)⑦@種情況外,還有一種作為參數(shù)對(duì)象的某個(gè)字段的時(shí)候。舉個(gè)例子: 如果User有屬性List ids。入?yún)⑹荱ser對(duì)象,那么這個(gè)collection = "ids" 如果User有屬性Ids ids;其中Ids是個(gè)對(duì)象,Ids有個(gè)屬性List id;入?yún)⑹荱ser對(duì)象,那么collection = "ids.id" 上面只是舉例,具體collection等于什么,就看你想對(duì)那個(gè)元素做循環(huán)。 該參數(shù)為必選。 |
| separator | 元素之間的分隔符,例如在in()的時(shí)候,separator=","會(huì)自動(dòng)在元素中間用“,“隔開(kāi),避免手動(dòng)輸入逗號(hào)導(dǎo)致sql錯(cuò)誤,如in(1,2,)這樣。該參數(shù)可選。 |
| open | foreach代碼的開(kāi)始符號(hào),一般是(和close=")"合用。常用在in(),values()時(shí)。該參數(shù)可選。 |
| close | foreach代碼的關(guān)閉符號(hào),一般是)和open="("合用。常用在in(),values()時(shí)。該參數(shù)可選。 |
| index | 在list和數(shù)組中,index是元素的序號(hào),在map中,index是元素的key,該參數(shù)可選。 |
4.1參數(shù)為list實(shí)例的寫法:
SQL寫法:
<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>接口的方法聲明:
4.2參數(shù)為Array實(shí)例的寫法:
?
SQL語(yǔ)句:
<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代碼??測(cè)試代碼,查詢學(xué)生中,在20000002、20000003這兩個(gè)班級(jí)的學(xué)生:
Java代碼??總結(jié)
以上是生活随笔為你收集整理的mybatis动态SQL语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [转]打造自己的LINQ Provide
- 下一篇: MS SQL 2000 分配权限