日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

mybatis學(xué)習(xí)資料

系統(tǒng) 2695 0

3.1 selectKey?標(biāo)簽

???????在insert語(yǔ)句中,在Oracle經(jīng)常使用序列、在MySQL中使用函數(shù)來(lái)自動(dòng)生成插入表的主鍵,而且需要方法能返回這個(gè)生成主鍵。使用myBatis的selectKey標(biāo)簽可以實(shí)現(xiàn)這個(gè)效果。

???????下面例子,使用mysql數(shù)據(jù)庫(kù)自定義函數(shù)nextval('student'),用來(lái)生成一個(gè)key,并把他設(shè)置到傳入的實(shí)體類中的studentId屬性上。所以在執(zhí)行完此方法后,邊可以通過(guò)這個(gè)實(shí)體類獲取生成的key。

?

Xml代碼?? 收藏代碼
  1. <!--?插入學(xué)生?自動(dòng)主鍵--> ??
  2. < insert ? id = "createStudentAutoKey" ? parameterType = "liming.student.manager.data.model.StudentEntity" ? keyProperty = "studentId" > ??
  3. ???? < selectKey ? keyProperty = "studentId" ? resultType = "String" ? order = "BEFORE" > ??
  4. ????????select?nextval('student')??
  5. ???? </ selectKey > ??
  6. ????INSERT?INTO?STUDENT_TBL(STUDENT_ID,??
  7. ????????????????????????????STUDENT_NAME,??
  8. ????????????????????????????STUDENT_SEX,??
  9. ????????????????????????????STUDENT_BIRTHDAY,??
  10. ????????????????????????????STUDENT_PHOTO,??
  11. ????????????????????????????CLASS_ID,??
  12. ????????????????????????????PLACE_ID)??
  13. ????VALUES?(#{studentId},??
  14. ????????????#{studentName},??
  15. ????????????#{studentSex},??
  16. ????????????#{studentBirthday},??
  17. ????????????#{studentPhoto,? javaType = byte [],? jdbcType = BLOB ,? typeHandler = org .apache.ibatis.type.BlobTypeHandler},??
  18. ????????????#{classId},??
  19. ????????????#{placeId})??
  20. </ insert > ??

?調(diào)用接口方法,和獲取自動(dòng)生成key

?

?

Java代碼?? 收藏代碼
  1. StudentEntity?entity?=? new ?StudentEntity();??
  2. entity.setStudentName( "黎明你好" );??
  3. entity.setStudentSex( 1 );??
  4. entity.setStudentBirthday(DateUtil.parse( "1985-05-28" ));??
  5. entity.setClassId( "20000001" );??
  6. entity.setPlaceId( "70000001" );??
  7. this .dynamicSqlMapper.createStudentAutoKey(entity);??
  8. System.out.println( "新增學(xué)生ID:?" ?+?entity.getStudentId());??

?selectKey語(yǔ)句屬性配置細(xì)節(jié):

?

?

屬性 描述 取值
keyProperty selectKey?語(yǔ)句生成結(jié)果需要設(shè)置的屬性。 ?
resultType 生成結(jié)果類型,MyBatis?允許使用基本的數(shù)據(jù)類型,包括String?、int類型。 ?
order

1:BEFORE,會(huì)先選擇主鍵,然后設(shè)置keyProperty,再執(zhí)行insert語(yǔ)句;

2:AFTER,就先運(yùn)行insert?語(yǔ)句再運(yùn)行selectKey?語(yǔ)句。

BEFORE

AFTER
statementType MyBatis?支持STATEMENT,PREPARED和CALLABLE?的語(yǔ)句形式,?對(duì)應(yīng)Statement?,PreparedStatement?和CallableStatement?響應(yīng)

STATEMENT

PREPARED

CALLABLE

?

3.2 if標(biāo)簽

?

?if標(biāo)簽可用在許多類型的sql語(yǔ)句中,我們以查詢?yōu)槔J紫瓤匆粋€(gè)很普通的查詢:

?

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

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

?

參數(shù)為實(shí)體類StudentEntity。將實(shí)體類中所有的屬性均進(jìn)行判斷,如果不為空則執(zhí)行判斷條件。

?

Xml代碼?? 收藏代碼
  1. <!--?2?if(判斷參數(shù))?-?將實(shí)體類不為空的屬性作為where條件?--> ??
  2. < select ? id = "getStudentList_if" ? resultMap = "resultMap_studentEntity" ? parameterType = "liming.student.manager.data.model.StudentEntity" > ??
  3. ????SELECT?ST.STUDENT_ID,??
  4. ???????????ST.STUDENT_NAME,??
  5. ???????????ST.STUDENT_SEX,??
  6. ???????????ST.STUDENT_BIRTHDAY,??
  7. ???????????ST.STUDENT_PHOTO,??
  8. ???????????ST.CLASS_ID,??
  9. ???????????ST.PLACE_ID??
  10. ??????FROM?STUDENT_TBL?ST???
  11. ?????WHERE??
  12. ???? < if ? test = "studentName?!=null?" > ??
  13. ????????ST.STUDENT_NAME?LIKE?CONCAT(CONCAT('%',?#{studentName,? jdbcType = VARCHAR }),'%')??
  14. ???? </ if > ??
  15. ???? < if ? test = "studentSex?!=?null?and?studentSex?!=?''?" > ??
  16. ????????AND? ST.STUDENT_SEX ?=?#{studentSex,? jdbcType = INTEGER }??
  17. ???? </ if > ??
  18. ???? < if ? test = "studentBirthday?!=?null?" > ??
  19. ????????AND? ST.STUDENT_BIRTHDAY ?=?#{studentBirthday,? jdbcType = DATE }??
  20. ???? </ if > ??
  21. ???? < if ? test = "classId?!=?null?and?classId!=?''?" > ??
  22. ????????AND? ST.CLASS_ID ?=?#{classId,? jdbcType = VARCHAR }??
  23. ???? </ if > ??
  24. ???? < if ? test = "classEntity?!=?null?and?classEntity.classId?!=null?and?classEntity.classId?!='?'?" > ??
  25. ????????AND? ST.CLASS_ID ?=?#{classEntity.classId,? jdbcType = VARCHAR }??
  26. ???? </ if > ??
  27. ???? < if ? test = "placeId?!=?null?and?placeId?!=?''?" > ??
  28. ????????AND? ST.PLACE_ID ?=?#{placeId,? jdbcType = VARCHAR }??
  29. ???? </ if > ??
  30. ???? < if ? test = "placeEntity?!=?null?and?placeEntity.placeId?!=?null?and?placeEntity.placeId?!=?''?" > ??
  31. ????????AND? ST.PLACE_ID ?=?#{placeEntity.placeId,? jdbcType = VARCHAR }??
  32. ???? </ if > ??
  33. ???? < if ? test = "studentId?!=?null?and?studentId?!=?''?" > ??
  34. ????????AND? ST.STUDENT_ID ?=?#{studentId,? jdbcType = VARCHAR }??
  35. ???? </ if > ???
  36. </ select > ??

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

?

?

Java代碼?? 收藏代碼
  1. public ? void ?select_test_2_1()?{??
  2. ????StudentEntity?entity?=? new ?StudentEntity();??
  3. ????entity.setStudentName( "" );??
  4. ????entity.setStudentSex( 1 );??
  5. ????entity.setStudentBirthday(DateUtil.parse( "1985-05-28" ));??
  6. ????entity.setClassId( "20000001" );??
  7. ???? //entity.setPlaceId("70000001"); ??
  8. ????List<StudentEntity>?list?=? this .dynamicSqlMapper.getStudentList_if(entity);??
  9. ???? for ?(StudentEntity?e?:?list)?{??
  10. ????????System.out.println(e.toString());??
  11. ????}??
  12. }??

?

?

3.3 if + where?的條件判斷

???????當(dāng)where中的條件使用的if標(biāo)簽較多時(shí),這樣的組合可能會(huì)導(dǎo)致錯(cuò)誤。我們以在3.1中的查詢語(yǔ)句為例子,當(dāng)java代碼按如下方法調(diào)用時(shí):

?

Java代碼?? 收藏代碼
  1. @Test ??
  2. public ? void ?select_test_2_1()?{??
  3. ????StudentEntity?entity?=? new ?StudentEntity();??
  4. ????entity.setStudentName( null );??
  5. ????entity.setStudentSex( 1 );??
  6. ????List<StudentEntity>?list?=? this .dynamicSqlMapper.getStudentList_if(entity);??
  7. ???? for ?(StudentEntity?e?:?list)?{??
  8. ????????System.out.println(e.toString());??
  9. ????}??
  10. }??

?如果上面例子,參數(shù)studentName為null,將不會(huì)進(jìn)行STUDENT_NAME列的判斷,則會(huì)直接導(dǎo)“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ì)剔除掉。

上面例子修改為:

?

Xml代碼?? 收藏代碼
  1. <!--?3?select?-?where/if(判斷參數(shù))?-?將實(shí)體類不為空的屬性作為where條件?--> ??
  2. < select ? id = "getStudentList_whereIf" ? resultMap = "resultMap_studentEntity" ? parameterType = "liming.student.manager.data.model.StudentEntity" > ??
  3. ????SELECT?ST.STUDENT_ID,??
  4. ???????????ST.STUDENT_NAME,??
  5. ???????????ST.STUDENT_SEX,??
  6. ???????????ST.STUDENT_BIRTHDAY,??
  7. ???????????ST.STUDENT_PHOTO,??
  8. ???????????ST.CLASS_ID,??
  9. ???????????ST.PLACE_ID??
  10. ??????FROM?STUDENT_TBL?ST???
  11. ???? < where > ??
  12. ???????? < if ? test = "studentName?!=null?" > ??
  13. ????????????ST.STUDENT_NAME?LIKE?CONCAT(CONCAT('%',?#{studentName,? jdbcType = VARCHAR }),'%')??
  14. ???????? </ if > ??
  15. ???????? < if ? test = "studentSex?!=?null?and?studentSex?!=?''?" > ??
  16. ????????????AND? ST.STUDENT_SEX ?=?#{studentSex,? jdbcType = INTEGER }??
  17. ???????? </ if > ??
  18. ???????? < if ? test = "studentBirthday?!=?null?" > ??
  19. ????????????AND? ST.STUDENT_BIRTHDAY ?=?#{studentBirthday,? jdbcType = DATE }??
  20. ???????? </ if > ??
  21. ???????? < if ? test = "classId?!=?null?and?classId!=?''?" > ??
  22. ????????????AND? ST.CLASS_ID ?=?#{classId,? jdbcType = VARCHAR }??
  23. ???????? </ if > ??
  24. ???????? < if ? test = "classEntity?!=?null?and?classEntity.classId?!=null?and?classEntity.classId?!='?'?" > ??
  25. ????????????AND? ST.CLASS_ID ?=?#{classEntity.classId,? jdbcType = VARCHAR }??
  26. ???????? </ if > ??
  27. ???????? < if ? test = "placeId?!=?null?and?placeId?!=?''?" > ??
  28. ????????????AND? ST.PLACE_ID ?=?#{placeId,? jdbcType = VARCHAR }??
  29. ???????? </ if > ??
  30. ???????? < if ? test = "placeEntity?!=?null?and?placeEntity.placeId?!=?null?and?placeEntity.placeId?!=?''?" > ??
  31. ????????????AND? ST.PLACE_ID ?=?#{placeEntity.placeId,? jdbcType = VARCHAR }??
  32. ???????? </ if > ??
  33. ???????? < if ? test = "studentId?!=?null?and?studentId?!=?''?" > ??
  34. ????????????AND? ST.STUDENT_ID ?=?#{studentId,? jdbcType = VARCHAR }??
  35. ???????? </ if > ??
  36. ???? </ where > ????
  37. </ select > ??

?

?

3.4 if + set?的更新語(yǔ)句

當(dāng)update語(yǔ)句中沒(méi)有使用if標(biāo)簽時(shí),如果有一個(gè)參數(shù)為null,都會(huì)導(dǎo)致錯(cuò)誤。

當(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)。

?

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

?

Xml代碼?? 收藏代碼
  1. <!--?4?if/set(判斷參數(shù))?-?將實(shí)體類不為空的屬性更新?--> ??
  2. < update ? id = "updateStudent_if_set" ? parameterType = "liming.student.manager.data.model.StudentEntity" > ??
  3. ????UPDATE?STUDENT_TBL??
  4. ???? < set > ??
  5. ???????? < if ? test = "studentName?!=?null?and?studentName?!=?''?" > ??
  6. ???????????? STUDENT_TBL.STUDENT_NAME ?=?#{studentName},??
  7. ???????? </ if > ??
  8. ???????? < if ? test = "studentSex?!=?null?and?studentSex?!=?''?" > ??
  9. ???????????? STUDENT_TBL.STUDENT_SEX ?=?#{studentSex},??
  10. ???????? </ if > ??
  11. ???????? < if ? test = "studentBirthday?!=?null?" > ??
  12. ???????????? STUDENT_TBL.STUDENT_BIRTHDAY ?=?#{studentBirthday},??
  13. ???????? </ if > ??
  14. ???????? < if ? test = "studentPhoto?!=?null?" > ??
  15. ???????????? STUDENT_TBL.STUDENT_PHOTO ?=?#{studentPhoto,? javaType = byte [],? jdbcType = BLOB ,? typeHandler = org .apache.ibatis.type.BlobTypeHandler},??
  16. ???????? </ if > ??
  17. ???????? < if ? test = "classId?!=?''?" > ??
  18. ???????????? STUDENT_TBL.CLASS_ID ?=?#{classId}??
  19. ???????? </ if > ??
  20. ???????? < if ? test = "placeId?!=?''?" > ??
  21. ???????????? STUDENT_TBL.PLACE_ID ?=?#{placeId}??
  22. ???????? </ if > ??
  23. ???? </ set > ??
  24. ????WHERE? STUDENT_TBL.STUDENT_ID ?=?#{studentId};??????
  25. </ update > ??

?

?

3.5 if + trim代替where/set標(biāo)簽

???????trim是更靈活的去處多余關(guān)鍵字的標(biāo)簽,他可以實(shí)踐where和set的效果。

?

3.5.1 trim代替where

?

? ??

Xml代碼?? 收藏代碼
  1. <!--?5.1?if/trim代替where(判斷參數(shù))?-?將實(shí)體類不為空的屬性作為where條件?--> ??
  2. < select ? id = "getStudentList_if_trim" ? resultMap = "resultMap_studentEntity" > ??
  3. ????SELECT?ST.STUDENT_ID,??
  4. ???????????ST.STUDENT_NAME,??
  5. ???????????ST.STUDENT_SEX,??
  6. ???????????ST.STUDENT_BIRTHDAY,??
  7. ???????????ST.STUDENT_PHOTO,??
  8. ???????????ST.CLASS_ID,??
  9. ???????????ST.PLACE_ID??
  10. ??????FROM?STUDENT_TBL?ST???
  11. ???? < trim ? prefix = "WHERE" ? prefixOverrides = "AND|OR" > ??
  12. ???????? < if ? test = "studentName?!=null?" > ??
  13. ????????????ST.STUDENT_NAME?LIKE?CONCAT(CONCAT('%',?#{studentName,? jdbcType = VARCHAR }),'%')??
  14. ???????? </ if > ??
  15. ???????? < if ? test = "studentSex?!=?null?and?studentSex?!=?''?" > ??
  16. ????????????AND? ST.STUDENT_SEX ?=?#{studentSex,? jdbcType = INTEGER }??
  17. ???????? </ if > ??
  18. ???????? < if ? test = "studentBirthday?!=?null?" > ??
  19. ????????????AND? ST.STUDENT_BIRTHDAY ?=?#{studentBirthday,? jdbcType = DATE }??
  20. ???????? </ if > ??
  21. ???????? < if ? test = "classId?!=?null?and?classId!=?''?" > ??
  22. ????????????AND? ST.CLASS_ID ?=?#{classId,? jdbcType = VARCHAR }??
  23. ???????? </ if > ??
  24. ???????? < if ? test = "classEntity?!=?null?and?classEntity.classId?!=null?and?classEntity.classId?!='?'?" > ??
  25. ????????????AND? ST.CLASS_ID ?=?#{classEntity.classId,? jdbcType = VARCHAR }??
  26. ???????? </ if > ??
  27. ???????? < if ? test = "placeId?!=?null?and?placeId?!=?''?" > ??
  28. ????????????AND? ST.PLACE_ID ?=?#{placeId,? jdbcType = VARCHAR }??
  29. ???????? </ if > ??
  30. ???????? < if ? test = "placeEntity?!=?null?and?placeEntity.placeId?!=?null?and?placeEntity.placeId?!=?''?" > ??
  31. ????????????AND? ST.PLACE_ID ?=?#{placeEntity.placeId,? jdbcType = VARCHAR }??
  32. ???????? </ if > ??
  33. ???????? < if ? test = "studentId?!=?null?and?studentId?!=?''?" > ??
  34. ????????????AND? ST.STUDENT_ID ?=?#{studentId,? jdbcType = VARCHAR }??
  35. ???????? </ if > ??
  36. ???? </ trim > ?????
  37. </ select > ??

?

?

3.5.2 trim代替set

??

Xml代碼?? 收藏代碼
  1. <!--?5.2?if/trim代替set(判斷參數(shù))?-?將實(shí)體類不為空的屬性更新?--> ??
  2. < update ? id = "updateStudent_if_trim" ? parameterType = "liming.student.manager.data.model.StudentEntity" > ??
  3. ????UPDATE?STUDENT_TBL??
  4. ???? < trim ? prefix = "SET" ? suffixOverrides = "," > ??
  5. ???????? < if ? test = "studentName?!=?null?and?studentName?!=?''?" > ??
  6. ???????????? STUDENT_TBL.STUDENT_NAME ?=?#{studentName},??
  7. ???????? </ if > ??
  8. ???????? < if ? test = "studentSex?!=?null?and?studentSex?!=?''?" > ??
  9. ???????????? STUDENT_TBL.STUDENT_SEX ?=?#{studentSex},??
  10. ???????? </ if > ??
  11. ???????? < if ? test = "studentBirthday?!=?null?" > ??
  12. ???????????? STUDENT_TBL.STUDENT_BIRTHDAY ?=?#{studentBirthday},??
  13. ???????? </ if > ??
  14. ???????? < if ? test = "studentPhoto?!=?null?" > ??
  15. ???????????? STUDENT_TBL.STUDENT_PHOTO ?=?#{studentPhoto,? javaType = byte [],? jdbcType = BLOB ,? typeHandler = org .apache.ibatis.type.BlobTypeHandler},??
  16. ???????? </ if > ??
  17. ???????? < if ? test = "classId?!=?''?" > ??
  18. ???????????? STUDENT_TBL.CLASS_ID ?=?#{classId},??
  19. ???????? </ if > ??
  20. ???????? < if ? test = "placeId?!=?''?" > ??
  21. ???????????? STUDENT_TBL.PLACE_ID ?=?#{placeId}??
  22. ???????? </ if > ??
  23. ???? </ trim > ??
  24. ????WHERE? STUDENT_TBL.STUDENT_ID ?=?#{studentId}??
  25. </ update > ??

?

?

3.6 choose (when, otherwise)

?

? ? 有時(shí)候我們并不想應(yīng)用所有的條件,而只是想從多個(gè)選項(xiàng)中選擇一個(gè)。而使用if標(biāo)簽時(shí),只要test中的表達(dá)式為true,就會(huì)執(zhí)行if標(biāo)簽中的條件。MyBatis提供了choose 元素。if標(biāo)簽是與(and)的關(guān)系,而choose比傲天是或(or)的關(guān)系。

? ? choose標(biāo)簽是按順序判斷其內(nèi)部when標(biāo)簽中的test條件出否成立,如果有一個(gè)成立,則choose結(jié)束。當(dāng)choose中所有when的條件都不滿則時(shí),則執(zhí)行otherwise中的sql。類似于Java 的switch 語(yǔ)句,choose為switch,when為case,otherwise則為default。

? ? 例如下面例子,同樣把所有可以限制的條件都寫(xiě)上,方面使用。choose會(huì)從上到下選擇一個(gè)when標(biāo)簽的test為true的sql執(zhí)行。安全考慮,我們使用where將choose包起來(lái),放置關(guān)鍵字多于錯(cuò)誤。

?

?

Xml代碼?? 收藏代碼
  1. <!--?6?choose(判斷參數(shù))?-?按順序?qū)?shí)體類第一個(gè)不為空的屬性作為where條件?--> ??
  2. < select ? id = "getStudentList_choose" ? resultMap = "resultMap_studentEntity" ? parameterType = "liming.student.manager.data.model.StudentEntity" > ??
  3. ????SELECT?ST.STUDENT_ID,??
  4. ???????????ST.STUDENT_NAME,??
  5. ???????????ST.STUDENT_SEX,??
  6. ???????????ST.STUDENT_BIRTHDAY,??
  7. ???????????ST.STUDENT_PHOTO,??
  8. ???????????ST.CLASS_ID,??
  9. ???????????ST.PLACE_ID??
  10. ??????FROM?STUDENT_TBL?ST???
  11. ???? < where > ??
  12. ???????? < choose > ??
  13. ???????????? < when ? test = "studentName?!=null?" > ??
  14. ????????????????ST.STUDENT_NAME?LIKE?CONCAT(CONCAT('%',?#{studentName,? jdbcType = VARCHAR }),'%')??
  15. ???????????? </ when ? > ??
  16. ???????????? < when ? test = "studentSex?!=?null?and?studentSex?!=?''?" > ??
  17. ????????????????AND? ST.STUDENT_SEX ?=?#{studentSex,? jdbcType = INTEGER }??
  18. ???????????? </ when ? > ??
  19. ???????????? < when ? test = "studentBirthday?!=?null?" > ??
  20. ????????????????AND? ST.STUDENT_BIRTHDAY ?=?#{studentBirthday,? jdbcType = DATE }??
  21. ???????????? </ when ? > ??
  22. ???????????? < when ? test = "classId?!=?null?and?classId!=?''?" > ??
  23. ????????????????AND? ST.CLASS_ID ?=?#{classId,? jdbcType = VARCHAR }??
  24. ???????????? </ when ? > ??
  25. ???????????? < when ? test = "classEntity?!=?null?and?classEntity.classId?!=null?and?classEntity.classId?!='?'?" > ??
  26. ????????????????AND? ST.CLASS_ID ?=?#{classEntity.classId,? jdbcType = VARCHAR }??
  27. ???????????? </ when ? > ??
  28. ???????????? < when ? test = "placeId?!=?null?and?placeId?!=?''?" > ??
  29. ????????????????AND? ST.PLACE_ID ?=?#{placeId,? jdbcType = VARCHAR }??
  30. ???????????? </ when ? > ??
  31. ???????????? < when ? test = "placeEntity?!=?null?and?placeEntity.placeId?!=?null?and?placeEntity.placeId?!=?''?" > ??
  32. ????????????????AND? ST.PLACE_ID ?=?#{placeEntity.placeId,? jdbcType = VARCHAR }??
  33. ???????????? </ when ? > ??
  34. ???????????? < when ? test = "studentId?!=?null?and?studentId?!=?''?" > ??
  35. ????????????????AND? ST.STUDENT_ID ?=?#{studentId,? jdbcType = VARCHAR }??
  36. ???????????? </ when ? > ??
  37. ???????????? < otherwise > ??
  38. ???????????? </ otherwise > ??
  39. ???????? </ choose > ??
  40. ???? </ where > ????
  41. </ select > ??

?

?

3.7 foreach

對(duì)于動(dòng)態(tài)SQL?非常必須的,主是要迭代一個(gè)集合,通常是用于IN?條件。List?實(shí)例將使用“l(fā)ist”做為鍵,數(shù)組實(shí)例以“array”?做為鍵。

foreach元素是非常強(qiáng)大的,它允許你指定一個(gè)集合,聲明集合項(xiàng)和索引變量,它們可以用在元素體內(nèi)。它也允許你指定開(kāi)放和關(guān)閉的字符串,在迭代之間放置分隔符。這個(gè)元素是很智能的,它不會(huì)偶然地附加多余的分隔符。

注意:你可以傳遞一個(gè)List實(shí)例或者數(shù)組作為參數(shù)對(duì)象傳給MyBatis。當(dāng)你這么做的時(shí)候,MyBatis會(huì)自動(dòng)將它包裝在一個(gè)Map中,用名稱在作為鍵。List實(shí)例將會(huì)以“l(fā)ist”作為鍵,而數(shù)組實(shí)例將會(huì)以“array”作為鍵。

這個(gè)部分是對(duì)關(guān)于XML配置文件和XML映射文件的而討論的。下一部分將詳細(xì)討論Java API,所以你可以得到你已經(jīng)創(chuàng)建的最有效的映射。

?

?

3.7.1參數(shù)為array示例的寫(xiě)法

?

接口的方法聲明:

?

Java代碼?? 收藏代碼
  1. public ?List<StudentEntity>?getStudentListByClassIds_foreach_array(String[]?classIds);??

?動(dòng)態(tài)SQL語(yǔ)句:

?

?

Xml代碼?? 收藏代碼
  1. <!—?7.1?foreach(循環(huán)array參數(shù))?-?作為where中in的條件?-- > ??
  2. < select ? id = "getStudentListByClassIds_foreach_array" ? resultMap = "resultMap_studentEntity" > ??
  3. ????SELECT?ST.STUDENT_ID,??
  4. ???????????ST.STUDENT_NAME,??
  5. ???????????ST.STUDENT_SEX,??
  6. ???????????ST.STUDENT_BIRTHDAY,??
  7. ???????????ST.STUDENT_PHOTO,??
  8. ???????????ST.CLASS_ID,??
  9. ???????????ST.PLACE_ID??
  10. ??????FROM?STUDENT_TBL?ST??
  11. ??????WHERE?ST.CLASS_ID?IN???
  12. ????? < foreach ? collection = "array" ? item = "classIds" ?? open = "(" ? separator = "," ? close = ")" > ??
  13. ????????#{classIds}??
  14. ????? </ foreach > ??
  15. </ select > ??

?測(cè)試代碼,查詢學(xué)生中,在20000001、20000002這兩個(gè)班級(jí)的學(xué)生:

?

?

Java代碼?? 收藏代碼
  1. @Test ??
  2. public ? void ?test7_foreach()?{??
  3. ????String[]?classIds?=?{? "20000001" ,? "20000002" ?};??
  4. ????List<StudentEntity>?list?=? this .dynamicSqlMapper.getStudentListByClassIds_foreach_array(classIds);??
  5. ???? for ?(StudentEntity?e?:?list)?{??
  6. ????????System.out.println(e.toString());??
  7. ????}??
  8. }??

?

?

3.7.2參數(shù)為list示例的寫(xiě)法

接口的方法聲明:

Java代碼?? 收藏代碼
  1. public ?List<StudentEntity>?getStudentListByClassIds_foreach_list(List<String>?classIdList);??

? 動(dòng)態(tài) SQL 語(yǔ)句:

Xml代碼?? 收藏代碼
  1. <!--?7.2?foreach(循環(huán)List<String>參數(shù))?-?作為where中in的條件?--> ??
  2. < select ? id = "getStudentListByClassIds_foreach_list" ? resultMap = "resultMap_studentEntity" > ??
  3. ????SELECT?ST.STUDENT_ID,??
  4. ???????????ST.STUDENT_NAME,??
  5. ???????????ST.STUDENT_SEX,??
  6. ???????????ST.STUDENT_BIRTHDAY,??
  7. ???????????ST.STUDENT_PHOTO,??
  8. ???????????ST.CLASS_ID,??
  9. ???????????ST.PLACE_ID??
  10. ??????FROM?STUDENT_TBL?ST??
  11. ??????WHERE?ST.CLASS_ID?IN???
  12. ????? < foreach ? collection = "list" ? item = "classIdList" ?? open = "(" ? separator = "," ? close = ")" > ??
  13. ????????#{classIdList}??
  14. ????? </ foreach > ??
  15. </ select > ??

? 測(cè)試代碼,查詢學(xué)生中,在 20000001 20000002 這兩個(gè)班級(jí)的學(xué)生:

Java代碼?? 收藏代碼
  1. @Test ??
  2. public ? void ?test7_2_foreach()?{??
  3. ????ArrayList<String>?classIdList?=? new ?ArrayList<String>();??
  4. ????classIdList.add( "20000001" );??
  5. ????classIdList.add( "20000002" );??
  6. ????List<StudentEntity>?list?=? this .dynamicSqlMapper.getStudentListByClassIds_foreach_list(classIdList);??
  7. ???? for ?(StudentEntity?e?:?list)?{??
  8. ????????System.out.println(e.toString());??
  9. ????}??
  10. }??

?

mybatis學(xué)習(xí)資料


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 尚义县| 郎溪县| 于都县| 莱州市| 武义县| 潮州市| 酉阳| 琼海市| 宁陵县| 阳朔县| 云梦县| 鄂尔多斯市| 邢台市| 黔江区| 涞源县| 永泰县| 虎林市| 大悟县| 尤溪县| 观塘区| 仲巴县| 施甸县| 得荣县| 曲麻莱县| 彩票| 普安县| 漳平市| 安康市| 道真| 共和县| 呼玛县| 错那县| 枣强县| 扶风县| 盈江县| 常山县| 沙田区| 抚州市| 大荔县| 雷州市| 屯留县|