Mybatis的多表操作2

一、Mybatis的多对多操作

例: 一个用户可以有多个角色
一个角色可以赋予多个用户
方法:
1、建立两张表:用户表,角色表
让用户表和角色表具有多对多的关系。需要使用中间表,中间表中包含各自的主键,在中间表中是外键。
在这里插入图片描述
2、建立两个实体类:用户实体类和角色实体类
让用户和角色的实体类能体现出来多对多的关系
各自包含对方一个集合引用

//role角色表
    private Integer roleId;
    private String roleName;
    private String roleDesc;
    //多对多的关系映射 一个角色可以赋予多个用户
    private List<User> users;
//用户表实体
        private Integer id;
        private String username;
        private String address;
        private String sex;
        private Date birthday;
//多对多的关系映射,一个用户可以具备多个角色
        private List<Role> roles;

3、建立两个配置文件
用户的配置文件
角色的配置文件
在这里插入图片描述
4、实现配置
当我们查询用户时,可以同时得到用户所包含的角色信息

	<mapper namespace="com.rpf.dao.UserDao">
    <!--定义User的ResuleMap-->
    <resultMap id="userMap" type="user">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
   <collection property="roles" ofType="role">
       <id property="roleId" column="rid"></id>
       <result property="roleName" column="role_name"></result>
       <result property="roleDesc" column="role_desc"></result>
   </collection>
    </resultMap>
    <!--配置查询所有-->
    <select id="findAll" resultMap="userMap"><!--resultType="com.rpf.domain.User"-->
        select u.*,r.id as rid,r.role_name,r.role_desc from user u
        left outer join user_role ur on u.id =ur.uid
        left outer join role r on r.id=ur.rid
    </select>

</mapper>
	当我们查询角色时,可以同时得到角色的所赋予的用户信息
  <mapper namespace="com.rpf.dao.RoleDao">
    <!--查询所有-->
    <resultMap id="roleMap" type="Role">
        <id property="roleId" column="rid"></id>
        <result property="roleName" column="role_name"></result>
        <result property="roleDesc" column="role_desc"></result>
        <collection property="users" ofType="user">
            <id column="id" property="id"></id>
            <result column="username" property="username"></result>
            <result column="address" property="address"></result>
            <result column="sex" property="sex"></result>
            <result column="birthday" property="birthday"></result>
        </collection>
    </resultMap>
   <select id="findAll" resultMap="roleMap">
              select u.*,r.id as rid,r.role_name,r.role_desc from role r left outer join user_role ur on r.id =ur.rid
              left outer join user u on u.id=ur.uid
   </select>

5、测试 查询用户时同时显示用户的角色信息

 @Test
    public void findAll(){
        List<User> users=userDao.findAll();
        for (User user:users){
            System.out.println(user);
            System.out.println(user.getRoles());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值