小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

ibatis 多表查詢(xún)操作詳解

 WindySky 2009-03-19

<sqlMap namespace="user"><--命名空間,好像2.0默認(rèn)是開(kāi)通的-->

字串9

<cacheModel type="LRU" id="oneDayCategory"><!--緩存配置,詳細(xì)的資料請(qǐng)察看官方提供的用戶(hù)手冊(cè),好像有中文版本的了 -->

字串8

<flushInterval hours="24" /> 字串7

</cacheModel> 字串5

<typeAlias type="com.infodeliver.pigeon.bean.User" alias="userbean" /><!--定義本地變量,為了方便使用 -->

字串4

<typeAlias type="com.infodeliver.pigeon.util.Condition" 字串6

alias="condition" /> 字串8

<typeAlias type="com.infodeliver.pigeon.bean.Role" alias="rolebean"/>

字串6

字串9

<resultMap id="get_group_result"

字串9

class="com.infodeliver.pigeon.bean.Group"><!-- 這就是不定義本地變量的結(jié)果-->

字串2

<result property="groupid" column="group_id" /> 字串7

<result property="groupname" column="group_name" /> 字串5

</resultMap> 字串7

字串8

<resultMap id="get_role_result" 

字串7

class="rolebean"> 字串2

<result property="role_id" column="role_id"/>

字串1

<result property="role_name" column="role_name"/>

字串6

</resultMap> 字串5

字串5

<resultMap id="get_user_result" 字串1

class="userbean"> 字串5

<result property="user_id" column="user_id" />

字串6

<result property="username" column="username" /> 字串4

<!--為user的group信息(屬性groupid,groupname)配置 --> 字串1

<result property="group.groupid" column="group_id"  />

字串3

<result property="group.groupname" column="group_name" />

字串2

<!—這里的property="group.groupid"的含義已經(jīng)很對(duì)象化了,group指的User對(duì)象里的數(shù)據(jù)類(lèi)型-Group的別名;這里的column=”group_id”為group表里的PK:group_id而非user表的FK:group_id,不要弄混淆了!-->

字串7

<!--為user的role信息(屬性roleid,rolename)配置,這里同樣是User中的子對(duì)象,采取的處理卻是不同的,通過(guò)group來(lái)避免使用子查詢(xún)帶來(lái)的性能問(wèn)題,大家可以看到 字串8

第一個(gè)配置的粒度比較細(xì)微,配置子對(duì)象的屬性,而role只是到對(duì)象一級(jí),group中沒(méi)有使用select="XXX"這樣的子查詢(xún),從而避免了因?yàn)閿?shù)據(jù)量大而引起二次查詢(xún)帶來(lái)的性能問(wèn)題,而采用了一個(gè)role作為一個(gè)反面教材,大家比較一下可以發(fā)現(xiàn)其中的不同!--> 字串5

<result property="role" column="role_id" select="get_role"/>

字串3

<!—這里采用了子查詢(xún)(兩次查詢(xún)),注意這里的role_id是role表的PK:role_id,而非user表的FK:role_id,大家不要搞錯(cuò)了>

字串6

</resultMap> 字串1

字串7

<select id="get_role" parameterClass="int" resultMap="get_role_result"><!--這里的parameterClass="int"因?yàn)楦覆樵?xún)傳過(guò)來(lái)的role_id為int型的-->

字串3

select * from sew_role where role_id=#value# 字串7

</select>

字串9

字串5

<select id="get_user" parameterClass="userbean" 字串1

resultMap="get_user_result">

字串6

select * from sew_user u,sew_group g<!--因?yàn)檫@里的group沒(méi)有子查詢(xún),所以要使用嵌套!這樣在執(zhí)行的效率上比子查詢(xún)有優(yōu)勢(shì)哦,大家應(yīng)該很熟悉吧!--> 字串1

where u.group_id=g.group_id 

字串5

and u.user_id=#user_id#

字串3

</select>

字串1

<select id="checkuser" parameterClass="userbean"

字串1

resultMap="get_user_result">

字串6

select * from sew_user u,sew_group g 字串5

where u.group_id=g.group_id  字串5

and u.username=#username# 字串1

and u.password=#password#

字串1

</select>

字串5

<select id="get_group" resultMap="get_group_result"

字串6

parameterClass="int"> 字串5

select * from sew_group where group_id=#value# 字串9

</select>

字串6

<insert id="insertuser" parameterClass="userbean"> 字串4

insert into sew_user

字串3

values(sew_user_id_seq.nextval,<!—因?yàn)閿?shù)據(jù)庫(kù)里用了序列--> 字串7

#username#,#password#,#email#,#group.groupid#,#role.role_id#,#nationality#,

字串9

#tel#,#address#,#language#,#isEvectioner#,#realname#)

字串8

</insert> 字串5

<!—這里紅色標(biāo)注的:group.groupid同樣的意思是User對(duì)象的數(shù)據(jù)成員:group,而groupid為子對(duì)象Group的數(shù)據(jù)成員(在UML里把帶有setter和getter的數(shù)據(jù)成員稱(chēng)為屬性。) -->

字串3

業(yè)務(wù)邏輯: 字串5

Public class UserService{ 字串4

public User getUser(User user){ 字串2

User user1=null; 字串1

try {

字串2

System.out.println(user.getUser_id()); 字串2

user1 =(User)getSqlMapExecutor().queryForObject("getuser",user); 字串5

} catch (SQLException e) {

字串2

e.printStackTrace(); 字串4

return null; 字串7

} 字串8

return user1; 字串1

}

字串2

}

字串9

字串7

字串8

字串4

字串2

字串7

請(qǐng)看操作部分的代碼: 字串9

實(shí)現(xiàn)效果:察看user_id為2的用戶(hù)在那個(gè)組。 字串4

UserService us=new UserService();

字串6

User ub=new User();

字串9

ub.setUser_id(2);

字串3

ub =(User)us.getUser_Group(ub);

字串5

System.out.println("ok"+"\n"+ub.getUsername()+"\n"+ub.getGroup().getGroupname()+"\n"+ub.getRole().getRole_name()); 字串7

System.out.println(ub.getGroup().getGroupname()); 字串5

字串7

增加用戶(hù):

字串6

User user=new User();

字串3

user.setUser_id(27); 字串2

user.setUsername("##梁靜茹!!"); 字串2

user.setPassword("1111"); 字串5

user.setEmail("ljr@sohu.com"); 字串7

user.setLanguage("CN");

字串5

user.setAddress("無(wú)錫.江蘇");

字串1

user.setNationality("中國(guó)"); 字串8

user.setTel("1390000000");

字串5

user.setRealname("歐陽(yáng)靜茹");

字串7

Group g=new Group();

字串6

g.setGroupid(1); 字串8

Role r =new Role(); 字串7

r.setRole_id(1); 字串1

user.setGroup(g); 字串2

user.setRole(r);

字串4

user=us.saveUser(user);

字串6

if(user!=null)

字串1

System.out.println(user.getGroup().getGroupid()); 字串5

字串7

以上所有操作都是通過(guò)的!

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多