MyBatis聯(lián)表CRUD
I.多表連接查詢(xún)方式 :
<!-- 結(jié)果的映射及關(guān)聯(lián)關(guān)系的映射-->
<resultMap type="City" id="cityMapper">
<id column="ctid" property="ctid"/>
<result column="ctname" property="ctname"/>
<!-- <association>表示多對(duì)一關(guān)聯(lián)關(guān)系的說(shuō)明,property:在多方實(shí)體類(lèi)中包含的一方關(guān)聯(lián)屬性的名稱(chēng),javaType:一方關(guān)聯(lián)屬性的類(lèi)型 -->
<association property="country" javaType="Country">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
</association>
</resultMap>
<!-- 多表連接查詢(xún)方式 -->
<select id="selectCityByCtid" resultMap="cityMapper">
select * from city ct,country c where ct.country_id=c.cid and ct.ctid=#{xxx}
</select>
II.多表單獨(dú)查詢(xún)方式:
<!-- 多表單獨(dú)查詢(xún)方式 -->
<select id="selectCountryByCountryId" resultType="Country">
select * from country where cid=#{xxx}
</select>
<resultMap type="City" id="cityMapperSingle">
<id column="ctid" property="ctid"/>
<result column="ctname" property="ctname"/>
<association property="country" javaType="Country" select="selectCountryByCountryId" column="cid"></association>
</resultMap>
<select id="selectCityByCtidSingle" resultMap="cityMapperSingle">
select ctid,ctname,country_id cid from city where ctid=#{xxx}
</select>
2.自關(guān)聯(lián):反應(yīng)到數(shù)據(jù)庫(kù)查詢(xún)中用自連接來(lái)表示,即一張當(dāng)成多張表來(lái)進(jìn)行連接查詢(xún)。
自關(guān)聯(lián)可以以一對(duì)多來(lái)處理(如通過(guò)主菜單找到該主菜單下面所有的子菜單)对雪,也可以以多對(duì)一來(lái)處理(如給出一個(gè)文件位置缆巧,找出這個(gè)文件的所有上級(jí)目錄的路徑)司抱。
(1)自關(guān)聯(lián)以一對(duì)多的方式處理
開(kāi)發(fā)步驟:
I.創(chuàng)建菜單表,設(shè)置關(guān)系绪励,指明外鍵,由于自連接是連接同一張表,所以多方的外鍵是在同一張表中澳眷,外鍵指向父元素的id胡嘿,一級(jí)元素的外鍵為null,而其他子元素的外鍵不為空,為它的上一級(jí)元素的id钳踊。
-- 菜單表
create table menu
(
mid int primary key auto_increment, -- 主菜單id
mname varchar(20), -- 菜單的名字
pid int, -- 父菜單的id
foreign key(pid) references menu(mid)
);
II.創(chuàng)建實(shí)體類(lèi)
//一方
public class Menu {
private int mid;
private String mname;
// 以一對(duì)多方式處理衷敌,此時(shí)的Menu看到的子菜單Menu是多個(gè)
// 關(guān)聯(lián)屬性:表示子菜單
private Set<Menu> menus;// 多方
...
}
III.創(chuàng)建MenuDao
IV.創(chuàng)建映射文件
A.以一對(duì)多的方式:使用遞歸調(diào)用,將本次查詢(xún)的id作為下一次查詢(xún)父id進(jìn)行遞歸搜索拓瞪。比如菜單缴罗。
只找父菜單下面的子菜單:
<resultMap type="Menu" id="menuMapper">
<id column="mid" property="mid"/>
<result column="mname" property="mname"/>
<!-- 通過(guò)select屬性遞歸調(diào)用selectMenuByParentId查詢(xún),并將上一次找到的mid作為本次的pid動(dòng)態(tài)參數(shù)祭埂,這樣遞歸后就可以遍歷所有的子菜單 -->
<collection property="menus" ofType="Menu" select="selectMenuByParentId" column="mid"></collection>
</resultMap>
<select id="selectMenuByParentId" resultMap="menuMapper">
select mid,mname,pid from menu where pid=#{xxx}
</select>
找出父菜單及其子菜單:
<select id="selectMenuByParentId" resultMap="menuMapper">
select mid,mname,pid from menu where pid=#{xxx}
</select>
<resultMap type="Menu" id="menuMapperByMid">
<id column="mid" property="mid"/>
<result column="mname" property="mname"/>
<!-- 通過(guò)select屬性遞歸調(diào)用selectMenuByParentId查詢(xún)面氓,并將上一次找到的mid作為本次的pid動(dòng)態(tài)參數(shù),這樣遞歸后就可以遍歷所有的子菜單 -->
<collection property="menus" ofType="Menu" select="selectMenuByParentId" column="mid"></collection>
</resultMap>
<select id="selectMenuByMId" resultMap="menuMapperByMid">
select mid,mname,pid from menu where mid=#{xxx}
</select>
B.以多對(duì)一的方式處理:使用遞歸處理蛆橡,將找到的當(dāng)前菜單的pid作為下一次查詢(xún)的mid的,比如確定某一個(gè)文件的位置舌界。