前言
mybatis 是一款優秀的持久層框架,它支持定制化 sql、存儲過程以及高級映射。mybatis 避免了幾乎所有的 jdbc 代碼和手動設置參數以及獲取結果集。mybatis 可以使用簡單的 xml 或注解來配置和映射原生信息,將接口和 java 的 pojos(plain old java objects,普通的 java對象)映射成數據庫中的記錄。
用來循環容器的標簽foreach,查看例子
foreach元素的屬性主要有item,index,collection,open,separator,close。
● item:集合中元素迭代時的別名,
● index:集合中元素迭代時的索引
● open:常用語where語句中,表示以什么開始,比如以'('開始
● separator:表示在每次進行迭代時的分隔符,
● close 常用語where語句中,表示以什么結束,
在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的,主要有一下3種情況:
● 如果傳入的是單參數且參數類型是一個list的時候,collection屬性值為list .
● 如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值為array .
● 如果傳入的參數是多個的時候,我們就需要把它們封裝成一個map了,當然單參數也可以封裝成map,實際上如果你在傳入參數的時候,在mybatis里面也是會把它封裝成一個map的,map的key就是參數名,所以這個時候collection屬性值就是傳入的list或array對象在自己封裝的map里面的key.
針對最后一條,我們來看一下官方說法:
注意 你可以將一個 list 實例或者數組作為參數對象傳給 mybatis,當你這么做的時候,mybatis 會自動將它包裝在一個 map 中并以名稱為鍵。list 實例將會以“list”作為鍵,而數組實例的鍵將是“array”。
所以,不管是多參數還是單參數的list,array類型,都可以封裝為map進行傳遞。如果傳遞的是一個list,則mybatis會封裝為一個list為key,list值為object的map,如果是array,則封裝成一個array為key,array的值為object的map,如果自己封裝呢,則colloection里放的是自己封裝的map里的key值
1
2
3
4
5
6
7
8
9
10
11
12
|
//mapper中我們要為這個方法傳遞的是一個容器,將容器中的元素一個一個的 //拼接到xml的方法中就要使用這個foreach這個標簽了 public list<entity> querybyid(list<string> userids); //對應的xml中如下 <select id= "querybyid" resultmap= "basereslutmap" > select * from entity where id in <foreach collection= "userids" item= "userid" index= "index" open= "(" separator= "," close= ")" > #{userid} </foreach> </select> |
concat模糊查詢
1
2
3
4
5
6
7
8
9
10
|
//比如說我們想要進行條件查詢,但是幾個條件不是每次都要使用,那么我們就可以 //通過判斷是否拼接到sql中 <select id= "querybyid" resultmap= "bascresultmap" parametertype= "entity" > select * from entity <where> < if test= "name!=null" > name like concat( '%' ,concat(#{name}, '%' )) </ if > </where> </select> |
choose (when, otherwise)標簽
choose標簽是按順序判斷其內部when標簽中的test條件出否成立,如果有一個成立,則 choose 結束。當 choose 中所有 when 的條件都不滿則時,則執行 otherwise 中的sql。類似于java 的 switch 語句,choose 為 switch,when 為 case,otherwise 則為 default。
例如下面例子,同樣把所有可以限制的條件都寫上,方面使用。choose會從上到下選擇一個when標簽的test為true的sql執行。安全考慮,我們使用where將choose包起來,放置關鍵字多于錯誤。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!-- choose(判斷參數) - 按順序將實體類 user 第一個不為空的屬性作為:where條件 --> <select id= "getuserlist_choose" resultmap= "resultmap_user" parametertype= "com.yiibai.pojo.user" > select * from user u <where> <choose> <when test= "username !=null " > u.username like concat(concat( '%' , #{username, jdbctype=varchar}), '%' ) </when > <when test= "sex != null and sex != '' " > and u.sex = #{sex, jdbctype=integer} </when > <when test= "birthday != null " > and u.birthday = #{birthday, jdbctype=date} </when > <otherwise> </otherwise> </choose> </where> </select> |
selectkey 標簽
在insert語句中,在oracle經常使用序列、在mysql中使用函數來自動生成插入表的主鍵,而且需要方法能返回這個生成主鍵。使用mybatis的selectkey標簽可以實現這個效果。
下面例子,使用mysql數據庫自定義函數nextval('student'),用來生成一個key,并把他設置到傳入的實體類中的studentid屬性上。所以在執行完此方法后,邊可以通過這個實體類獲取生成的key。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!-- 插入學生 自動主鍵--> <insert id= "createstudentautokey" parametertype= "liming.student.manager.data.model.studententity" keyproperty= "studentid" > <selectkey keyproperty= "studentid" resulttype= "string" order= "before" > select nextval( 'student' ) </selectkey> insert into student_tbl(student_id, student_name, student_sex, student_birthday, student_photo, class_id, place_id) values (#{studentid}, #{studentname}, #{studentsex}, #{studentbirthday}, #{studentphoto, javatype= byte [], jdbctype=blob, typehandler=org.apache.ibatis.type.blobtypehandler}, #{classid}, #{placeid}) </insert> |
調用接口方法,和獲取自動生成key
1
2
3
4
5
6
7
8
|
studententity entity = new studententity(); entity.setstudentname( "黎明你好" ); entity.setstudentsex( 1 ); entity.setstudentbirthday(dateutil.parse( "1985-05-28" )); entity.setclassid( "20000001" ); entity.setplaceid( "70000001" ); this .dynamicsqlmapper.createstudentautokey(entity); system.out.println( "新增學生id: " + entity.getstudentid()); |
if標簽
if標簽可用在許多類型的sql語句中,我們以查詢為例。首先看一個很普通的查詢:
1
2
3
4
5
|
<!-- 查詢學生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或等于空字符串,我們就不進行此條件的判斷,增加靈活性。
參數為實體類studententity。將實體類中所有的屬性均進行判斷,如果不為空則執行判斷條件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<!-- 2 if (判斷參數) - 將實體類不為空的屬性作為where條件 --> <select id= "getstudentlist_if" resultmap= "resultmap_studententity" parametertype= "liming.student.manager.data.model.studententity" > select st.student_id, st.student_name, st.student_sex, st.student_birthday, st.student_photo, st.class_id, st.place_id from student_tbl st where < if test= "studentname !=null " > st.student_name like concat(concat( '%' , #{studentname, jdbctype=varchar}), '%' ) </ if > < if test= "studentsex != null and studentsex != '' " > and st.student_sex = #{studentsex, jdbctype=integer} </ if > < if test= "studentbirthday != null " > and st.student_birthday = #{studentbirthday, jdbctype=date} </ if > < if test= "classid != null and classid!= '' " > and st.class_id = #{classid, jdbctype=varchar} </ if > < if test= "classentity != null and classentity.classid !=null and classentity.classid !=' ' " > and st.class_id = #{classentity.classid, jdbctype=varchar} </ if > < if test= "placeid != null and placeid != '' " > and st.place_id = #{placeid, jdbctype=varchar} </ if > < if test= "placeentity != null and placeentity.placeid != null and placeentity.placeid != '' " > and st.place_id = #{placeentity.placeid, jdbctype=varchar} </ if > < if test= "studentid != null and studentid != '' " > and st.student_id = #{studentid, jdbctype=varchar} </ if > </select> |
使用時比較靈活, new一個這樣的實體類,我們需要限制那個條件,只需要附上相應的值就會where這個條件,相反不去賦值就可以不在where中判斷。
1
2
3
4
5
6
7
8
9
10
11
12
|
public void select_test_2_1() { studententity entity = new studententity(); entity.setstudentname( "" ); entity.setstudentsex( 1 ); entity.setstudentbirthday(dateutil.parse( "1985-05-28" )); entity.setclassid( "20000001" ); //entity.setplaceid("70000001"); list<studententity> list = this .dynamicsqlmapper.getstudentlist_if(entity); for (studententity e : list) { system.out.println(e.tostring()); } } |
if + where 的條件判斷
當where中的條件使用的if標簽較多時,這樣的組合可能會導致錯誤。我們以在3.1中的查詢語句為例子,當java代碼按如下方法調用時:
1
2
3
4
5
6
7
8
9
10
|
@test public void select_test_2_1() { studententity entity = new studententity(); entity.setstudentname( null ); entity.setstudentsex( 1 ); list<studententity> list = this .dynamicsqlmapper.getstudentlist_if(entity); for (studententity e : list) { system.out.println(e.tostring()); } } |
如果上面例子,參數studentname為null,將不會進行student_name列的判斷,則會直接導“where and”關鍵字多余的錯誤sql。
這時我們可以使用where動態語句來解決。這個“where”標簽會知道如果它包含的標簽中有返回值的話,它就插入一個‘where'。此外,如果標簽返回的內容是以and 或or 開頭的,則它會剔除掉。
上面例子修改為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<!-- 3 select - where/ if (判斷參數) - 將實體類不為空的屬性作為where條件 --> <select id= "getstudentlist_whereif" resultmap= "resultmap_studententity" parametertype= "liming.student.manager.data.model.studententity" > select st.student_id, st.student_name, st.student_sex, st.student_birthday, st.student_photo, st.class_id, st.place_id from student_tbl st <where> < if test= "studentname !=null " > st.student_name like concat(concat( '%' , #{studentname, jdbctype=varchar}), '%' ) </ if > < if test= "studentsex != null and studentsex != '' " > and st.student_sex = #{studentsex, jdbctype=integer} </ if > < if test= "studentbirthday != null " > and st.student_birthday = #{studentbirthday, jdbctype=date} </ if > < if test= "classid != null and classid!= '' " > and st.class_id = #{classid, jdbctype=varchar} </ if > < if test= "classentity != null and classentity.classid !=null and classentity.classid !=' ' " > and st.class_id = #{classentity.classid, jdbctype=varchar} </ if > < if test= "placeid != null and placeid != '' " > and st.place_id = #{placeid, jdbctype=varchar} </ if > < if test= "placeentity != null and placeentity.placeid != null and placeentity.placeid != '' " > and st.place_id = #{placeentity.placeid, jdbctype=varchar} </ if > < if test= "studentid != null and studentid != '' " > and st.student_id = #{studentid, jdbctype=varchar} </ if > </where> </select> |
if + set實現修改語句
當update語句中沒有使用if標簽時,如果有一個參數為null,都會導致錯誤。
當在update語句中使用if標簽時,如果前面的if沒有執行,則或導致逗號多余錯誤。使用set標簽可以將動態的配置set 關鍵字,和剔除追加到條件末尾的任何不相關的逗號。使用if+set標簽修改后,如果某項為null則不進行更新,而是保持數據庫原值。如下示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<!-- 4 if /set(判斷參數) - 將實體類不為空的屬性更新 --> <update id= "updatestudent_if_set" parametertype= "liming.student.manager.data.model.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= "studentphoto != null " > student_tbl.student_photo = #{studentphoto, javatype= byte [], jdbctype=blob, typehandler=org.apache.ibatis.type.blobtypehandler}, </ if > < if test= "classid != '' " > student_tbl.class_id = #{classid} </ if > < if test= "placeid != '' " > student_tbl.place_id = #{placeid} </ if > </set> where student_tbl.student_id = #{studentid}; </update> |
if + trim代替where/set標簽
trim是更靈活的去處多余關鍵字的標簽,他可以實踐where和set的效果。
trim代替where
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<!-- 5 .1if/trim代替where(判斷參數) -將實體類不為空的屬性作為where條件--> <select id= "getstudentlist_if_trim" resultmap= "resultmap_studententity" > select st.student_id, st.student_name, st.student_sex, st.student_birthday, st.student_photo, st.class_id, st.place_id from student_tbl st <trim prefix= "where" prefixoverrides= "and|or" > < if test= "studentname !=null " > st.student_name like concat(concat( '%' , #{studentname, jdbctype=varchar}), '%' ) </ if > < if test= "studentsex != null and studentsex != '' " > and st.student_sex = #{studentsex, jdbctype=integer} </ if > < if test= "studentbirthday != null " > and st.student_birthday = #{studentbirthday, jdbctype=date} </ if > < if test= "classid != null and classid!= '' " > and st.class_id = #{classid, jdbctype=varchar} </ if > < if test= "classentity != null and classentity.classid !=null and classentity.classid !=' ' " > and st.class_id = #{classentity.classid, jdbctype=varchar} </ if > < if test= "placeid != null and placeid != '' " > and st.place_id = #{placeid, jdbctype=varchar} </ if > < if test= "placeentity != null and placeentity.placeid != null and placeentity.placeid != '' " > and st.place_id = #{placeentity.placeid, jdbctype=varchar} </ if > < if test= "studentid != null and studentid != '' " > and st.student_id = #{studentid, jdbctype=varchar} </ if > </trim> </select> |
trim代替set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<!-- 5.2 if /trim代替set(判斷參數) - 將實體類不為空的屬性更新 --> <update id= "updatestudent_if_trim" parametertype= "liming.student.manager.data.model.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= "studentphoto != null " > student_tbl.student_photo = #{studentphoto, javatype= byte [], jdbctype=blob, typehandler=org.apache.ibatis.type.blobtypehandler}, </ if > < if test= "classid != '' " > student_tbl.class_id = #{classid}, </ if > < if test= "placeid != '' " > student_tbl.place_id = #{placeid} </ if > </trim> where student_tbl.student_id = #{studentid} </update> |
foreach
對于動態sql 非常必須的,主是要迭代一個集合,通常是用于in 條件。list 實例將使用“list”做為鍵,數組實例以“array” 做為鍵。
foreach元素是非常強大的,它允許你指定一個集合,聲明集合項和索引變量,它們可以用在元素體內。它也允許你指定開放和關閉的字符串,在迭代之間放置分隔符。這個元素是很智能的,它不會偶然地附加多余的分隔符。
注意:你可以傳遞一個list實例或者數組作為參數對象傳給mybatis。當你這么做的時候,mybatis會自動將它包裝在一個map中,用名稱在作為鍵。list實例將會以“list”作為鍵,而數組實例將會以“array”作為鍵。
這個部分是對關于xml配置文件和xml映射文件的而討論的。下一部分將詳細討論java api,所以你可以得到你已經創建的最有效的映射。
1參數為array示例的寫法
接口的方法聲明:
1
|
public list<studententity> getstudentlistbyclassids_foreach_array(string[] classids); |
動態sql語句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!— 7.1 foreach(循環array參數) - 作為where中in的條件 --> <select id= "getstudentlistbyclassids_foreach_array" resultmap= "resultmap_studententity" > select st.student_id, st.student_name, st.student_sex, st.student_birthday, st.student_photo, st.class_id, st.place_id from student_tbl st where st.class_id in <foreach collection= "array" item= "classids" open= "(" separator= "," close= ")" > #{classids} </foreach> </select> |
測試代碼,查詢學生中,在20000001、20000002這兩個班級的學生:
1
2
3
4
5
6
7
8
|
@test public void test7_foreach() { string[] classids = { "20000001" , "20000002" }; list<studententity> list = this .dynamicsqlmapper.getstudentlistbyclassids_foreach_array(classids); for (studententity e : list) { system.out.println(e.tostring()); } } |
2參數為list示例的寫法
接口的方法聲明:
1
|
public list<studententity> getstudentlistbyclassids_foreach_list(list<string> classidlist); |
動態sql語句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- 7.2 foreach(循環list<string>參數) - 作為where中in的條件 --> <select id= "getstudentlistbyclassids_foreach_list" resultmap= "resultmap_studententity" > select st.student_id, st.student_name, st.student_sex, st.student_birthday, st.student_photo, st.class_id, st.place_id from student_tbl st where st.class_id in <foreach collection= "list" item= "classidlist" open= "(" separator= "," close= ")" > #{classidlist} </foreach> </select> |
測試代碼,查詢學生中,在20000001、20000002這兩個班級的學生:
1
2
3
4
5
6
7
8
9
|
@test public void test7_2_foreach() { arraylist<string> classidlist = new arraylist<string>(); classidlist.add( "20000001" ); classidlist.add( "20000002" ); list<studententity> list = this .dynamicsqlmapper.getstudentlistbyclassids_foreach_list(classidlist); for (studententity e : list) { system.out.println(e.tostring()); } |
sql片段標簽:通過該標簽可定義能復用的sql語句片段,在執行sql語句標簽中直接引用即可。這樣既可以提高編碼效率,還能有效簡化代碼,提高可讀性
需要配置的屬性:id="" >>>表示需要改sql語句片段的唯一標識
引用:通過標簽引用,refid="" 中的值指向需要引用的中的id=“”屬性
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!--定義sql片段--> <sql id= "orderanditem" > o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count </sql> <select id= "findorderanditemsbyoid" parametertype= "java.lang.string" resultmap= "baseresultmap" > select <!--引用sql片段--> <include refid= "orderanditem" /> from ordertable o join orderitem i on o.orderitem_id = i.orderitem_id where o.order_id = #{orderid} </select> |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://yq.aliyun.com/articles/668121