mybatis的高級(jí)應(yīng)用_第1頁
mybatis的高級(jí)應(yīng)用_第2頁
mybatis的高級(jí)應(yīng)用_第3頁
mybatis的高級(jí)應(yīng)用_第4頁
mybatis的高級(jí)應(yīng)用_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、mybatis的高級(jí)應(yīng)用9 : resultmapresultmap 屬性id : resultmap 的唯一標(biāo)識(shí)type : java實(shí)體類resultmap子元素id : 一般對(duì)應(yīng)數(shù)據(jù)庫中該行的主鍵id ”設(shè)置此項(xiàng)可提高mybatis性能 result :映射到j(luò)avabean的某個(gè)簡(jiǎn)單類型"屬性property :映射數(shù)據(jù)庫列的實(shí)體對(duì)象的屬性column :數(shù)據(jù)庫列名或者別名association :映射到j(luò)avabean的某個(gè)"復(fù)雜類型"屬性,比如javabean 類復(fù)雜的類型關(guān)聯(lián),一對(duì)內(nèi)部嵌套映射一個(gè)嵌套javabean屬性association 屬性:

2、property :映射數(shù)據(jù)庫列的實(shí)體對(duì)象的屬性javatype :完整java類名或者別名resultmap :引用外部 resultmapassociatio n 子元素idresultproperty :映射數(shù)據(jù)庫列的實(shí)體對(duì)象的屬性 column :數(shù)據(jù)庫列名或者別名collection :映射到j(luò)avabean的某個(gè)復(fù)雜類型屬性,比如集合復(fù)雜類型集合,一對(duì)多, 內(nèi)部嵌套,映射一個(gè)嵌套結(jié)果集到一個(gè)列表collection :屬性property :映射數(shù)據(jù)庫列的實(shí)體對(duì)象的屬性oftype :完整java類名或者別名(集合所包括的類型)resultmap :引用外部 resultmapco

3、llection :子元素idresultproperty :映射數(shù)據(jù)庫列的實(shí)體對(duì)象的屬性column :數(shù)據(jù)庫列名或者別名<!-根據(jù) roleld 獲取用戶列表 association start -><!1 :在用戶表中添加private role role;屬性2:<resultmap>必須*讓查的字段,全部映射,不然查不到數(shù) 據(jù)(除非將mybatis全局配置文件中設(shè)置自動(dòng)映射級(jí) 別為full)3 : association :映射單個(gè)屬性(多對(duì)一的關(guān)系)id="userroleresult"><resultmap type=

4、”cn.zx.pojo.user”<id property=”id” column=”id”/><result property="username"colum n=”username”/>colu mn=”userpassword”/>column="address7>javatype=nc n.zx.pojo.role”>column="rolename7><result property="userpasswordn<result property=haddressh<as

5、sociation property=nrole"<id property=”id” column=nr_id7><result property=”rolename”</associati on></resultmap><resultmap type=”cn.zx.pojo.user” id=”userroleresult”a<id property=hidm column=nid7><result property=,'username" column=”username”/><res

6、ult property="userpasswordn column=',userpassword,7><result property="address" column=naddress7><association property=mrole" javatype="cn.zx.pojo.roleu resultmap=nroleresult7></resultmap><resultmap type=”cn.zx.pojo.role” id=”roleresult”><id

7、property=”id” column=,r_id,7><result property=nrolenamen column=,rolename'7></resultmap><select id=,getuserlistbyroleld,' parametertype="lnteger,' resultmap="userroleresulth>select u.*,r.id r_id ,r.rolename from smbms_user u,smbms_role rwhere u.userrole = #

8、userrole and u.userrole = r.id</select><!- association end -><!-獲取指定角色的用戶列表(collection)獲取指定用戶的地址列表(user表-address表:1對(duì)多關(guān) 系)在角色表中添加屬性:private list<user>userlist;<resultmap type=hcn.zx.pojo.role" id="roleuserresulth><id property="idh column="r_id'7&g

9、t;<result property="rolename" colu mn=nrolename'7>collection property=,userlist" oftype=hcn.zx.pojo.user" resultmap="userroleresult'7></resultmap><select id="finduserlistbyroleldk parametertype=”lnteger*' resultmap=,roleuserresult">

10、select u.*,r.id rjd, r.rolename from smbms_user u,smbms_role rwhere u.userrole = #id and u.userrole = r.id</select>v! collection end ->* 10 :自動(dòng)映射級(jí)別<settings>v!設(shè)置resultmap的自動(dòng)映射級(jí)別為:none :禁止自動(dòng)匹配partial(默認(rèn))自動(dòng)匹配所有屬性(association ,collection 除夕卜)full :自動(dòng)匹配所有><setting name=hautomappingb

11、ehavior" value=hpartialh /> </settings>*二mybatis工作流程(1) 加載配置并初始化(觸發(fā)條件:加載配置文件)配置文件,將sql的配置信息加載成為一個(gè)個(gè)mappedstatement對(duì)象(包括了傳入?yún)?shù)映射配置、執(zhí)行的sql語句、結(jié)果映射配置),存儲(chǔ)在內(nèi)存中。(2) 接收調(diào)用請(qǐng)求(觸發(fā)條件:調(diào)用mybatis提供的api )傳入?yún)?shù):為sql的id和傳入?yún)?shù)對(duì)象處理過程:將請(qǐng)求傳遞給下層的請(qǐng)求處理層(3)進(jìn)行處理。(3) 處理操作請(qǐng)求(觸發(fā)條件:api接口層傳遞請(qǐng)求過來)傳入?yún)?shù):為sql的id和傳入?yún)?shù)對(duì)象處理過程:(a) 根據(jù)sql的id查找對(duì)應(yīng)的mappedstatement對(duì)象。(b) 根據(jù)傳入?yún)?shù)對(duì)象解析mappedstatement對(duì)象z得到最終要執(zhí)行的sql和執(zhí)行傳入?yún)?shù)。(c) 獲取數(shù)據(jù)庫連接,根據(jù)得到的最終sql語句和執(zhí)行傳入?yún)?shù)到數(shù)據(jù)庫執(zhí)行,并得到執(zhí)行結(jié)果。(d) 根據(jù)mappedstatement對(duì)象中的結(jié)果映射配置對(duì)得到的執(zhí)行結(jié)果進(jìn)行轉(zhuǎn)換處理,并得到最終的處理結(jié)果。(e) 釋放連接資源。(4) 返回處理結(jié)果將最終的處理結(jié)果返回。三、orm工具的基本

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論