什么是 mybatis ?
mybatis 是一款優秀的持久層框架,它支持定制化 sql、存儲過程以及高級映射。mybatis 避免了幾乎所有的 jdbc 代碼和手動設置參數以及獲取結果集。mybatis 可以使用簡單的 xml 或注解來配置和映射原生信息,將接口和 java 的 pojos(plain old java objects,普通的 java對象)映射成數據庫中的記錄。
1、foreach 循環
foreach 元素的屬性主要有 item, idnex, collection, open, separator, close。
1.collection:傳入的 list 或 array 或自己封裝的 map。
2.item:集合中元素迭代時的別名。
3.idnex:集合中元素迭代是的索引。
4.open:where 后面表示以什么開始,如以‘('開始。
5.separator:表示在每次進行迭代是的分隔符。
6.close:where后面表示以什么結束,如以‘)'結束。
1
2
3
4
5
6
7
8
9
10
|
//mapper中需要傳遞一個容器 public list<user> querybyidlist(list<integer> useridlist); <select id= "querybyidlist" resultmap= "baseresultmap" parametertype= "map" > select * from user where userid in <foreach collection= "useridlist" item= "id" index= "index" open= "(" close= ")" separator= "," > #{id} </foreach> </select> |
2、concat 模糊查詢
1
2
3
4
5
6
7
8
9
|
//模糊查詢使用concat拼接sql <select id= "querybyname" resultmap= "baseresultmap" paramtertype "string" > select * from user <where> < if test= "name != null" > name like concat( '%' , concat(#{name}, '%' )) </ if > </where> </select> |
3、if + where 標簽
用 if 標簽判斷參數是否有效來進行條件查詢。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<select id= "getuserlist" resultmap= "baseresultmap" paramtertype= "com.demo.user" > select * from user <where> < if test= "userid !=null and userid!= ''" > userid= #{userid} </ if > < if test= "name !=null and name!= ''" > and name= #{name} </ if > < if phone= "userid !=null and phone!= ''" > and phone= #{phone} </ if > </where> </select> |
where 動態語句中,where 標簽會自動去掉 and 或 or。防止 where and 錯誤。
4、if + set
使用 set 標簽可以動態的配置 set 關鍵字,使用 if + set 標簽,如果某項為 null 則不進行更新。
<update id="updateuser" paramtertype="com.demo.user">
update user
<set>
<if test=" name != null and name != ''">
name = #{name},
</if>
<if test=" phone != null and phone != ''">
phone = #{phone},
</if>
</set>
where userid = #{userid}
</update>
5、if + trim 代替 where/set 標簽
trim 可以更靈活的去處多余關鍵字的標簽,可以實現 where 和 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
26
27
|
<select id= "getuserlist" resultmap= "baseresultmap" paramtertype= "com.demo.user" > select * from user <trim prefix= "where" prefixoverrides= "and|or" > < if test= "userid !=null and userid!= ''" > userid= #{userid} </ if > < if test= "name !=null and name!= ''" > and name= #{name} </ if > < if phone= "userid !=null and phone!= ''" > and phone= #{phone} </ if > </trim> </select> <update id= "updateuser" paramtertype= "com.demo.user" > update user <trim prefix= "set" suffixoverrides= "," > < if test= " name != null and name != ''" > name = #{name}, </ if > < if test= " phone != null and phone != ''" > phone = #{phone}, </ if > </trim> where userid = #{userid} </update> |
5、choose(when, otherwise)標簽
choose 標簽是按順序判斷其內部 when 標簽中的 test 條件是否成立,如果有一個成立,則 choose 結束。當 choose 中所有 when 的條件都不滿足,則執行 otherwise 中的 sql。類似 java 中的 switch 語句,choose 為 switch,when 為 case,otherwise 則為 default。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<select id= "selectcustomerbycustnameandtype" parametertype= "map" resultmap= "baseresultmap" > select * from user <choose> <when test= "utype == 'name'" > where name = #{name} </when> <when test= "utype == 'phone'" > where phone= #{phone} </when> <when test= "utype == 'email'" > where email= #{email} </when> <otherwise> where name = #{name} </otherwise> </choose> </select> |
總結
以上所述是小編給大家介紹的mybatis 常用寫法 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/zt19994/archive/2018/11/22/9999696.html