mybatis--表关系之多对多
表关系--多对多
示例:每个角色可以拥有多个权限,每个权限为多个校色拥有。
角色表m_role和权限表m_astrict是一个多对多的关系
步骤:
1.建立两张表:权限表和角色表
让用两个表具有多对多的关系,需要使用中间表,中间表中的主键是各自的外键
2.建立两个实体类:权限实体类和角色实体类
让两个类能体现出多对多的关系,在各自的类中建一个集合引用对方类
3.建立两个配置文件
权限的配置文件
角色的配置文件
4.实现配置
当我们查询权限时,可以得到权限所包含的角色信息
当我们查询角色时,可以得到角色所包含的权限信息
代码示例:
1.建两张表和中间表
角色表
权限表
中间表
中间表的外键关系
2.建立两个实体类
public class Astrict { private Integer aId; private String aName; private String aType; //多对多,在本类中依赖对方类的集合 private Set<Role> roles; }
public class Role { private Integer rId; private String rName; private String rType; //一对多,一的一方,一个角色至少对应一个类 private List<User> user; //多对多,每个角色包含多个权限 private Set<Astrict> astricts; }
3.建立配置两个类的映射文件
<resultMap id="astrictRole" type="astrict"> <id column="a_id" property="aId"></id> <result column="a_name" property="aName"></result> <result column="a_type" property="aType"></result> <collection property="roles" resultMap="com.dao.RoleMapper.roleMap"></collection> </resultMap> <select id="findAllAR" resultMap="astrictRole"> SELECT *FROM m_astrict a,m_role r,m_role_astrict ra WHERE r.r_id=ra.r_id AND a.a_id=ra.a_id </select>
<!--对Role和Astrict用ResultMap进行封装--> <resultMap id="roleAstrict" type="role"> <id column="r_id" property="rId"></id> <result column="r_name" property="rName"></result> <result column="r_type" property="rType"></result> <collection property="astricts" resultMap="com.dao.AstrictMapper.astrictMap"/> </resultMap> <select id="findAllRA" resultMap="roleAstrict"> SELECT *FROM m_astrict a,m_role r,m_role_astrict ra WHERE r.r_id=ra.r_id AND a.a_id=ra.a_id; </select>
4.测试
//查询单个权限表 @Test public void m1(){ SqlSession session = MybatisUtils.getSession(); AstrictMapper mapper = session.getMapper(AstrictMapper.class); Set<Astrict> all = mapper.findAll(); for (Astrict astrict : all) { System.out.println(astrict.getaName()); } session.commit(); session.close(); } //多对多查询,查询权限方 @Test public void m2(){ SqlSession session = MybatisUtils.getSession(); AstrictMapper mapper = session.getMapper(AstrictMapper.class); Set<Astrict> all = mapper.findAllAR(); for (Astrict astrict : all) { System.out.println(astrict); } session.commit(); session.close(); } //多对多查询,查询角色方 @Test public void m3(){ SqlSession session = MybatisUtils.getSession(); RoleMapper mapper = session.getMapper(RoleMapper.class); Set<Role> allRA = mapper.findAllRA(); for (Role role : allRA) { System.out.println(role); System.out.println(role.getAstricts()); } session.commit(); session.close(); }
多对多--扩展
需求分析:查询所有用户的角色一级校色对应的权限
1.修改映射配置文件
<!--封装user,role,astrict--> <resultMap id="userRoleAstrictMap" type="user"> <id column="u_id" property="uId"></id> <result column="u_name" property="uName"></result> <result column="u_lovel" property="uLovel"></result> <collection property="role" resultMap="com.dao.RoleMapper.roleAstrict"/> </resultMap> <select id="findAllUAR" resultMap="userRoleAstrictMap"> SELECT *FROM m_astrict a,m_role r,m_role_astrict ra,m_user u WHERE r.r_id=ra.r_id AND a.a_id=ra.a_id AND u.u_r_id=r.r_id </select>
2.测试
//多对多查询 @Test public void m4(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); //查询到所有的用户 List<User> allUAR = mapper.findAllUAR(); for (User user : allUAR) { System.out.println("用户:"+user.getuName()); System.out.println("角色:"+user.getRole()); //遍历用户下面的权限权限 for (Astrict astrict : user.getRole().getAstricts()) { System.out.println("权限:"+astrict.getaName()); } } session.commit(); session.close(); }
输出结果
DEBUG [main] - ==> Preparing: SELECT *FROM m_astrict a,m_role r,m_role_astrict ra,m_user u WHERE r.r_id=ra.r_id AND a.a_id=ra.a_id AND u.u_r_id=r.r_id DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 11 用户:金角 角色:Role{rId=3, rName='主公', rType='A', user=null} 权限:献计 权限:带兵 权限:上战场 用户:银角 角色:Role{rId=1, rName='将军', rType='B', user=null} 权限:带兵 权限:上战场 用户:铁脚 角色:Role{rId=2, rName='士兵', rType='C', user=null} 权限:上战场 用户:吕布 角色:Role{rId=3, rName='主公', rType='A', user=null} 权限:带兵 权限:上战场 权限:献计 用户:周瑜 角色:Role{rId=1, rName='将军', rType='B', user=null} 权限:带兵 权限:上战场
public class Astrict {
private Integer aId;
private String aName;
private String aType;
//多对多,在本类中依赖对方类的集合
private Set<Role> roles;