美文网首页晴空万里
不设外键:用程序来实现表之间的关联

不设外键:用程序来实现表之间的关联

作者: Mr_Elliot | 来源:发表于2018-08-08 20:09 被阅读163次
Association

相信有很多同学都有使用过Hibernate框架的开发经验,尽管现在开发人员对Hibernate的褒贬不一,我们暂且不谈这个话题。
图中的三个注解,正好解析了表与表之间的关联关系。我也就不这这方面进行过多的赘述了。所以...
今天的主题是:我想在不涉及表与表之间的关联关系的情况下对表进行关联。
换成对应的表的意思就是,不设外键,实现多对多关联。话不多说 先上一张我自己的设计手稿:


手稿

数据库:(数据库的编码(utf8)与数据库引擎(InnonDB)已经设置好了)

CREATE table t_user(
    uid int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(100) not null,
    password VARCHAR(100) not null,
    createtime date not null
)default CHARSET=utf8;

-- 创建角色表
CREATE TABLE t_role(
    rid int(10) not null PRIMARY KEY AUTO_INCREMENT,
    rname VARCHAR(100) not null
)

-- 权限表db_auth
CREATE TABLE t_authority(
    aid int(10) not null PRIMARY KEY AUTO_INCREMENT,
    aname VARCHAR(100) not NULL,
    url VARCHAR(100) not NULL
)
-- 意义上的中间表 
CREATE TABLE t_role_authority(
    rid int(10) not null,
    aid int(10) not NULL,
    KEY index_rid(rid),
    KEY index_aid(aid)
)

CREATE TABLE t_user_role(
    uid int(10) not null,
    rid int(10) not null,
    KEY index_uid(uid),
    KEY index_rid(rid)
)
没有任何关联关系的5张表

项目框架选型我用的是SpringBoot+MyBatis


项目结构

Vo类:

package cn.calista.sbm.pojo.Vo;

import cn.calista.sbm.pojo.Authority;

import java.util.HashSet;

/**
 *  权限和角色也是多对多
 */
public class AuthorityVo extends Authority {
    private HashSet<RoleVo> roleVos = new HashSet<>();

    public HashSet<RoleVo> getRoleVos() {
        return roleVos;
    }

    public void setRoleVos(HashSet<RoleVo> roleVos) {
        this.roleVos = roleVos;
    }

}
package cn.calista.sbm.pojo.Vo;

import cn.calista.sbm.pojo.Role;

import java.util.HashSet;

/**
 * Role 和 User 多对对
 * Role 和 authority  多对多
 */
public class RoleVo extends Role {
    private HashSet<AuthorityVo> authorityVos = new HashSet<>();

    private HashSet<UserVo> userVos = new HashSet<>();

    public HashSet<AuthorityVo> getAuthorityVos() {
        return authorityVos;
    }

    public void setAuthorityVos(HashSet<AuthorityVo> authorityVos) {
        this.authorityVos = authorityVos;
    }

    public HashSet<UserVo> getUserVos() {
        return userVos;
    }

    public void setUserVos(HashSet<UserVo> userVos) {
        this.userVos = userVos;
    }

}
package cn.calista.sbm.pojo.Vo;

import cn.calista.sbm.pojo.User;

import java.util.HashSet;

/**
 * 用户和角色 多对多
 */
public class UserVo extends User {
    private HashSet<RoleVo> roleVos = new HashSet<>();

    public HashSet<RoleVo> getRoleVos() {
        return roleVos;
    }

    public void setRoleVos(HashSet<RoleVo> roleVos) {
        this.roleVos = roleVos;
    }
}

这里说一句,虽然用户和角色是多对多的关系,但是我们在实际开发中并不会去关注一个角色被多少用户所拥有,我们关心的更多的是一个用户拥有哪些角色然后拥有多少权限。所以我们实际理解就可以理解为 用户和角色是 一对多的关系就OK

至于XML Mapper 的代码我就不在粘了 直接上核心代码

package cn.calista.sbm;

import cn.calista.sbm.mapper.*;
import cn.calista.sbm.pojo.*;
import cn.calista.sbm.pojo.Vo.AuthorityVo;
import cn.calista.sbm.pojo.Vo.RoleVo;
import cn.calista.sbm.pojo.Vo.UserVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SbmApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleAuthorityMapper roleAuthorityMapper;

    @Autowired
    private AuthorityMapper authorityMapper;

    @Test
    public void testMethod() {
        UserVo userVo = new UserVo();
        RoleVo roleVo = null;
        AuthorityVo authorityVo = null;
        //根据uid 查询到 user
        User user = userMapper.getUser(1);
        //拿到User 装配给 UserVo
        BeanUtils.copyProperties(user, userVo);
        //根据user的uid  查询 user_role 用户角色表  得到该 user所拥有的角色集合
        List<UserRole> userRoleList = userRoleMapper.getUserRoleList(user.getUid());
        //遍历 操作每一个 用户角色对象
        for (UserRole userRole : userRoleList
        ) { // 拿到角色
            Role role = roleMapper.getRoleById(userRole.getRid());
            roleVo = new RoleVo();
            // 拿到role  装配给 roleVo
            BeanUtils.copyProperties(role, roleVo);
            //通过每一个role的rid 查询中间表roleAuthority 的到该角色的权限集合
            List<RoleAuthority> roleAuthorites = roleAuthorityMapper.getRoleAuthorites(role.getRid());
            //遍历权限集合
            for (RoleAuthority roleAuthority : roleAuthorites
            ) {
                //通过aid查询到权限
                Integer aid = roleAuthority.getAid();
                Authority authority = authorityMapper.getAuthorityById(aid);
                authorityVo = new AuthorityVo();
                BeanUtils.copyProperties(authority, authorityVo);
                authorityVo.getRoleVos().add(roleVo);
                //装配roleVo中的AuthorityVos
                roleVo.getAuthorityVos().add(authorityVo);
            }
            //装配userVo属性roleVos
            userVo.getRoleVos().add(roleVo);
        }
        
    }
}

最后测试一下:
我想知道id是 1 的用户拥有哪些权限?

HashSet<AuthorityVo> authorityVos = roleVo.getAuthorityVos();
        Iterator<AuthorityVo> it = authorityVos.iterator();
        while (it.hasNext()){
            AuthorityVo next = it.next();
            System.out.println(next.getAname());
        }
测试结果

再查看一下数据库


user
role
authority
role_Authority
user_role

最后,实现了关联。(并没有使用连接查询)

image.png
image.png
image.png
image.png
image.png

相关文章

  • 不设外键:用程序来实现表之间的关联

    相信有很多同学都有使用过Hibernate框架的开发经验,尽管现在开发人员对Hibernate的褒贬不一,我们暂且...

  • Spring Data JPA 多个实体类表联合视图查询

    Spring Data JPA 查询数据库时,如果两个表有关联,那么就设个外键,在查询的时候用Specificat...

  • Spring Data JPA 多个实体类表联合视图查询

    Spring Data JPA 查询数据库时,如果两个表有关联,那么就设个外键,在查询的时候用Specificat...

  • ORM 关联关系

    参看手册文档 关联关系 可以 创建外键或不创建外键 假设存在 users 表 id(主键) -> phones 表...

  • 2018-01-19

    1对n关系:外键关联与逻辑外键外键关联缺陷:外键字段的值必须依赖于另一张表的唯一性约束字段逻辑关联缺陷:逻辑外键所...

  • Postgresql外键约束级联删除时容易忽略的问题

    外键约束用来实现表与表之间的参照完整性(referential integrity)。外键约束是指一个引用表(re...

  • Hibernate_6 一对一

    外键一对一 通俗上讲就是在一张表中设置一个外键对应另一张表的主键 实现方法 对于基于外键的1-1关联,其外键可以存...

  • LitePal

    关于LitePal(github地址) 数据库相关(表关联):一对一:数据表根据外键进行关联(哪个表添加外键都可以...

  • MYSQL数据表建立外键

    MYSQL数据表建立外键 MySQL创建关联表可以理解为是两个表之间有个外键关系,但这两个表必须满足三个条件 1....

  • Flask-关系映射

    关系映射 一对多 在"多"表中增加外键关联,引用自"一"表中主键 在"一"表实现关联属性以及反向引用 一对一 A表...

网友评论

    本文标题:不设外键:用程序来实现表之间的关联

    本文链接:https://www.haomeiwen.com/subject/cqaxbftx.html