一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - MyBatis通用的10種寫法總結大全

MyBatis通用的10種寫法總結大全

2021-06-14 13:51琪琪 Java教程

這篇文章主要給大家介紹了關于MyBatis通用的10種寫法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

mybatis 是一款優(yōu)秀的持久層框架,它支持定制化 sql、存儲過程以及高級映射。mybatis 避免了幾乎所有的 jdbc 代碼和手動設置參數(shù)以及獲取結果集。mybatis 可以使用簡單的 xml 或注解來配置和映射原生信息,將接口和 java 的 pojos(plain old java objects,普通的 java對象)映射成數(shù)據(jù)庫中的記錄。

用來循環(huán)容器的標簽foreach,查看例子

foreach元素的屬性主要有item,index,collection,open,separator,close。

● item:集合中元素迭代時的別名,

● index:集合中元素迭代時的索引

● open:常用語where語句中,表示以什么開始,比如以'('開始

● separator:表示在每次進行迭代時的分隔符,

● close 常用語where語句中,表示以什么結束,

在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的,主要有一下3種情況:

● 如果傳入的是單參數(shù)且參數(shù)類型是一個list的時候,collection屬性值為list .

● 如果傳入的是單參數(shù)且參數(shù)類型是一個array數(shù)組的時候,collection的屬性值為array .

● 如果傳入的參數(shù)是多個的時候,我們就需要把它們封裝成一個map了,當然單參數(shù)也可以封裝成map,實際上如果你在傳入?yún)?shù)的時候,在mybatis里面也是會把它封裝成一個map的,map的key就是參數(shù)名,所以這個時候collection屬性值就是傳入的list或array對象在自己封裝的map里面的key.

針對最后一條,我們來看一下官方說法:

注意 你可以將一個 list 實例或者數(shù)組作為參數(shù)對象傳給 mybatis,當你這么做的時候,mybatis 會自動將它包裝在一個 map 中并以名稱為鍵。list 實例將會以“list”作為鍵,而數(shù)組實例的鍵將是“array”。

所以,不管是多參數(shù)還是單參數(shù)的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標簽是按順序判斷其內(nèi)部when標簽中的test條件出否成立,如果有一個成立,則 choose 結束。當 choose 中所有 when 的條件都不滿則時,則執(zhí)行 otherwise 中的sql。類似于java 的 switch 語句,choose 為 switch,when 為 case,otherwise 則為 default。

例如下面例子,同樣把所有可以限制的條件都寫上,方面使用。choose會從上到下選擇一個when標簽的test為true的sql執(zhí)行。安全考慮,我們使用where將choose包起來,放置關鍵字多于錯誤。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- choose(判斷參數(shù)) - 按順序?qū)嶓w類 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經(jīng)常使用序列、在mysql中使用函數(shù)來自動生成插入表的主鍵,而且需要方法能返回這個生成主鍵。使用mybatis的selectkey標簽可以實現(xiàn)這個效果。

下面例子,使用mysql數(shù)據(jù)庫自定義函數(shù)nextval('student'),用來生成一個key,并把他設置到傳入的實體類中的studentid屬性上。所以在執(zhí)行完此方法后,邊可以通過這個實體類獲取生成的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>

調(diào)用接口方法,和獲取自動生成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語句中,我們以查詢?yōu)槔J紫瓤匆粋€很普通的查詢:

?
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動態(tài)sql語句先進行判斷,如果值為null或等于空字符串,我們就不進行此條件的判斷,增加靈活性。

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

?
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(判斷參數(shù)) - 將實體類不為空的屬性作為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代碼按如下方法調(diào)用時:

?
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());
}
}

如果上面例子,參數(shù)studentname為null,將不會進行student_name列的判斷,則會直接導“where and”關鍵字多余的錯誤sql。

這時我們可以使用where動態(tài)語句來解決。這個“where”標簽會知道如果它包含的標簽中有返回值的話,它就插入一個‘where'。此外,如果標簽返回的內(nèi)容是以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(判斷參數(shù)) - 將實體類不為空的屬性作為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實現(xiàn)修改語句

當update語句中沒有使用if標簽時,如果有一個參數(shù)為null,都會導致錯誤。

當在update語句中使用if標簽時,如果前面的if沒有執(zhí)行,則或?qū)е露禾柖嘤噱e誤。使用set標簽可以將動態(tài)的配置set 關鍵字,和剔除追加到條件末尾的任何不相關的逗號。使用if+set標簽修改后,如果某項為null則不進行更新,而是保持數(shù)據(jù)庫原值。如下示例:

?
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(判斷參數(shù)) - 將實體類不為空的屬性更新 -->
<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(判斷參數(shù)) -將實體類不為空的屬性作為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(判斷參數(shù)) - 將實體類不為空的屬性更新 -->
<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

對于動態(tài)sql 非常必須的,主是要迭代一個集合,通常是用于in 條件。list 實例將使用“list”做為鍵,數(shù)組實例以“array” 做為鍵。

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

注意:你可以傳遞一個list實例或者數(shù)組作為參數(shù)對象傳給mybatis。當你這么做的時候,mybatis會自動將它包裝在一個map中,用名稱在作為鍵。list實例將會以“list”作為鍵,而數(shù)組實例將會以“array”作為鍵。

這個部分是對關于xml配置文件和xml映射文件的而討論的。下一部分將詳細討論java api,所以你可以得到你已經(jīng)創(chuàng)建的最有效的映射。

1參數(shù)為array示例的寫法

接口的方法聲明:

?
1
public list<studententity> getstudentlistbyclassids_foreach_array(string[] classids);

動態(tài)sql語句:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!— 7.1 foreach(循環(huán)array參數(shù)) - 作為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參數(shù)為list示例的寫法

接口的方法聲明:

?
1
public list<studententity> getstudentlistbyclassids_foreach_list(list<string> classidlist);

動態(tài)sql語句:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 7.2 foreach(循環(huán)list<string>參數(shù)) - 作為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語句片段,在執(zhí)行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>

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://yq.aliyun.com/articles/668121

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 九九精品国产兔费观看久久 | 97爱干 | 欧美一区a | 福利一区三区 | 91精品国产美女福到在线不卡 | 爽爽影院免费观看 | 歪歪漫画a漫入口 | 国产美女亚洲精品久久久综合91 | 四虎影视在线永久免费观看 | 色婷婷网 | 国产精品福利在线观看免费不卡 | 国产成人精品高清不卡在线 | 青青青青青国产费线在线观看 | 青青青国产手机在线播放 | 美女69xx | 成人性爱视频在线观看 | 青草热久精品视频在线观看 | 久久国产视频网 | 免费在线影院 | 成人国产精品视频频 | 动漫美女被吸乳羞羞小说 | 亚洲九九九 | gay男强壮军人chinese | 毛片视频网站在线观看 | 日本xxxⅹ69xxxx护士 | 四虎影院在线免费观看视频 | 日韩精品 欧美 | 国产亚洲精aa在线观看香蕉 | 999精品视频在线观看 | 久久中文字幕乱码免费 | 从后面撕开老师的丝袜动态图 | 成人香蕉xxxxxxx | 轻轻色在线视频中文字幕 | 精品区2区3区4区产品乱码9 | 国产专区一va亚洲v天堂 | 日本高清在线看免费观看 | 国产性视频 | 95视频免费看片 | 四虎在线网址 | 白丝捆绑vk | 精品一区二区三区高清免费观看 |