mybatis動態SQL語句
一 if標簽
<select 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>
二 where標簽
<select 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>
如果它包含的標簽中有返回值的話就插入一個where。此外如果標簽返回的內容是以AND或OR開頭的,則它會剔除掉。
三 set 標簽
使用set+if標簽修改後,如果某項為null則不進行更新,而是保持數據庫原值
<update 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>
四 trim標簽
trim是更靈活的去處多餘關鍵字的標簽,他可以實踐where和set的效果。
where例子的等效trim語句
<select 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語句
<update 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>
五 choose (when, otherwise)
有時候並不想應用所有的條件,而隻是想從多個選項中選擇一個。MyBatis提供了choose 元素,按順序判斷when中的條件出否成立,如果有一個成立,則choose結束。當choose中所有when的條件都不滿則時,則執行otherwise中的sql。類似於Java 的switch語句,choose為switch,when為case,otherwise則為default。
<select 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>
六 foreach
對於動態SQL 非常必須的,主是要迭代一個集合,通常是用於IN 條件。List實例將使用“list”做為鍵,數組實例以“array”做為鍵。
1 參數為list實例的寫法
SqlMapper.xml
<select resultMap="studentResultMap">
SELECT * FROM STUDENT_TBL ST
WHERE ST.CLASS_ID IN
<foreach collection="list" item="classList" open="(" separator="," close=")">
#{classList}
</foreach>
</select>
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());
}
2 參數為Array實例的寫法
SqlMapper.xml
<select resultMap="studentResultMap">
SELECT * FROM STUDENT_TBL ST
WHERE ST.CLASS_ID IN
<foreach collection="array" item="ids" open="(" separator="," close=")">
#{ids}
</foreach>
</select>
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());
}
原帖地址:https://blog.csdn.net/ask_rent/article/details/6320326
最後更新:2017-04-03 05:40:29
上一篇:
java線程學習5——線程同步之同步方法
下一篇:
vmware安裝係統注意點
鍾義信:分享“BRF”的現場實感
什麼是新零售?阿裏巴巴最新的報告終於說清楚了!(附報告)
《數據驅動安全:數據安全分析、可視化和儀表盤》一1.2 獲取數據分析技能
Incompatible namespaceIDs或連接被對端重置異常的解決
淺談Windos Azure架構與存儲
穀歌員工吐槽:人生不如意十之八九 在穀歌工作也不容易
雅虎天氣API調用
StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1) 中pack的理解
keras中最常用深度學習的API
masm中list文件和宏的一些常用編譯調試查看方法