sql語句where條件中,須要一些安全判斷,例如按性別檢索,若是傳入的參數是空的,此時查詢出的結果極可能是空的,也許咱們須要參數爲空 時,是查出所有的信息。這是咱們可使用動態sql,增長一個判斷,當參數不符合要求的時候,咱們能夠不去判斷此查詢條件。
下文均採用mysql語法和函數(例如字符串連接函數CONCATmysql
3.1 if標籤
一個很普通的查詢:sql
Xml代碼
<!-- 查詢學生list,like姓名 -->
數據庫
<select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap"> SELECT * from STUDENT_TBL ST WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') </select>
可是此時若是studentName是null或空字符串,此語句極可能報錯或查詢結果爲空。此時咱們使用if動態sql語句先進行判斷,若是值爲null或等於空字符串,咱們就不進行此條件的判斷。數組
修改成:安全
Xml代碼
<!-- 查詢學生list,like姓名 -->
app
<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>
此時,當studentName的值爲null或’’的時候,咱們並不進行where條件的判斷,因此當studentName值爲null或’’值,不附帶這個條件,因此查詢結果是所有。
ide
因爲參數是Java的實體類,因此咱們能夠把全部條件都附加上,使用時比較靈活, new一個這樣的實體類,咱們須要限制那個條件,只須要附上相應的值就會where這個條件,相反不去賦值就能夠不在where中判斷。函數
代碼中的where標籤,請參考3.2.1.測試
Xml代碼
<!-- 查詢學生list,like姓名,=性別、=生日、=班級,使用where,參數entity類型 -->
spa
<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’,班級在‘20000002’的學生。
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());
}
3.2 where、set、trim標籤
3.2.1 where
當if標籤較多時,這樣的組合可能會致使錯誤。例如,like姓名,等於指定性別等:
Xml代碼
<!-- 查詢學生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>
若是上面例子,參數studentName爲null或’’,則或致使此sql組合成「WHERE AND」之類的關鍵字多餘的錯誤SQL。
這時咱們可使用where動態語句來解決。這個「where」標籤會知道若是它包含的標籤中有返回值的話,它就插入一個‘where’。此外,若是標籤返回的內容是以AND 或OR 開頭的,則它會剔除掉。
上面例子修改成:
Xml代碼
<!-- 查詢學生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
當在update語句中使用if標籤時,若是前面的if沒有執行,則或致使逗號多餘錯誤。使用set標籤能夠將動態的配置SET 關鍵字,和剔除追加到條件末尾的任何不相關的逗號。
沒有使用if標籤時,若是有一個參數爲null,都會致使錯誤,以下示例:
Xml代碼
<!-- 更新學生信息 -->
<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標籤修改後,若是某項爲null則不進行更新,而是保持數據庫原值。以下示例:
Xml代碼
<!-- 更新學生信息 -->
<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是更靈活的去處多餘關鍵字的標籤,他能夠實踐where和set的效果。
where例子的等效trim語句:
Xml代碼
<!-- 查詢學生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代碼
<!-- 更新學生信息 -->
<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)
有時候咱們並不想應用全部的條件,而只是想從多個選項中選擇一個。MyBatis提供了choose 元素,按順序判斷when中的條件出否成立,若是有一個成立,則choose結束。當choose中全部when的條件都不滿則時,則執行 otherwise中的sql。相似於Java 的switch 語句,choose爲switch,when爲case,otherwise則爲default。
if是與(and)的關係,而choose是或(or)的關係。
例以下面例子,一樣把全部能夠限制的條件都寫上,方面使用。選擇條件順序,when標籤的從上到下的書寫順序:
Xml代碼
<!-- 查詢學生list,like姓名、或=性別、或=生日、或=班級,使用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
對於動態SQL 很是必須的,主是要迭代一個集合,一般是用於IN 條件。
List 實例將使用「list」作爲鍵,數組實例以「array」 作爲鍵。
3.4.1參數爲list實例的寫法:
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>
接口的方法聲明:
Java代碼
public List<StudentEntity> getStudentListByClassIDs(List<String> classList);
public List<StudentEntity> getStudentListByClassIDs(List<String> classList); 測試代碼,查詢學生中,在2000000二、20000003這兩個班級的學生:
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參數爲Array實例的寫法:
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>
接口的方法聲明:
Java代碼
public List<StudentEntity> getStudentListByClassIDs(String[] ids);
public List<StudentEntity> getStudentListByClassIDs(String[] ids);測試代碼,查詢學生中,在2000000二、20000003這兩個班級的學生:
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()); }