`
独善其身008
  • 浏览: 168562 次
  • 性别: Icon_minigender_2
  • 来自: 苏州
文章分类
社区版块
存档分类
最新评论

ibatis 多表之间的运用。

 
阅读更多

<sqlMap namespace="user"><--命名空间,好像2.0默认是开通的-->

字串9

<cacheModel type="LRU" id="oneDayCategory"><!--缓存配置,详细的资料请察看官方提供的用户手册,好像有中文版本的了 -->

字串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"><!-- 这就是不定义本地变量的结果-->

字串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"的含义已经很对象化了,group指的User对象里的数据类型-Group的别名;这里的column=”group_id”为group表里的PK:group_id而非user表的FK:group_id,不要弄混淆了!-->

字串7

<!--为user的role信息(属性roleid,rolename)配置,这里同样是User中的子对象,采取的处理却是不同的,通过group来避免使用子查询带来的性能问题,大家可以看到 字串8

第一个配置的粒度比较细微,配置子对象的属性,而role只是到对象一级,group中没有使用select="XXX"这样的子查询,从而避免了因为数据量大而引起二次查询带来的性能问题,而采用了一个role作为一个反面教材,大家比较一下可以发现其中的不同!--> 字串5

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

字串3

<!—这里采用了子查询(两次查询),注意这里的role_id是role表的PK:role_id,而非user表的FK:role_id,大家不要搞错了>

字串6

</resultMap> 字串1

字串7

<select id="get_role" parameterClass="int" resultMap="get_role_result"><!--这里的parameterClass="int"因为父查询传过来的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<!--因为这里的group没有子查询,所以要使用嵌套!这样在执行的效率上比子查询有优势哦,大家应该很熟悉吧!--> 字串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,<!—因为数据库里用了序列--> 字串7

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

字串9

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

字串8

</insert> 字串5

<!—这里红色标注的:group.groupid同样的意思是User对象的数据成员:group,而groupid为子对象Group的数据成员(在UML里把带有setter和getter的数据成员称为属性。) -->

字串3

业务逻辑: 字串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

请看操作部分的代码: 字串9

实现效果:察看user_id为2的用户在那个组。 字串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

增加用户:

字串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("无锡.江苏");

字串1

user.setNationality("中国"); 字串8

user.setTel("1390000000");

字串5

user.setRealname("欧阳静茹");

字串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

以上所有操作都是通过的!

addthis_pub= 'wolfit'; addthis_logo= 'http://download.rainbowsoft.org/image/common/zbloglogo.gif'; addthis_brand= 'Z-Blog'; addthis_options= 'favorites, delicious, twitter, digg, myspace, facebook, google, live, email, more';

 


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics