一、用户与角色之间的关系
一个用户对应一个角色,一个角色可以对应多个用户,所以用户对角色的关系是多对一(多个用户对应一个角色),角色对用户的关系是一对多(一个角色对应多个用户)。外键应该创建在多的一方表中,即用户表。
1、创建表,用户表sys_user、角色表sys_role
create table sys_user(
uid int not null primary key auto_increment,
userName varchar(20) not null,
password varchar(20) not null,
rid int
);
create table sys_role(
rid int not null primary key auto_increment,
role varchar(20) not null
);
2、插入数据
insert into sys_user values(null,"zhangsan","123456",1);
insert into sys_user values(null,"lisi","123456",1);
insert into sys_role values(null,"admin");
insert into sys_role values(null,"ordinary");
3、创建项目配置maven相关依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tongchenggo</groupId>
<artifactId>tongchenggo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tongchenggo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--导入连接MySQL的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.tongchenggo.TongchenggoApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
application.properties
# 应用名称
spring.application.name=tongchenggo
# 应用服务 WEB 访问端口
server.port=8080
#下面这些内容是为了让MyBatis映射
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.tongchenggo.entity
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/tongchenggo?serverTimezone=Asia/Shanghai&useunicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.max-idle=10
#spring.datasource.max-wait=10000
#spring.datasource.min-idle=5
#spring.datasource.initial-size=5
4、编写User和Role实体类
User.java
package com.tongchenggo.entity;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
public class User implements Serializable {
private Integer ID;
private String userName;
private String password;
private Integer rid;
private Role role;
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Role.java
package com.tongchenggo.entity;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.List;
@Component
public class Role implements Serializable {
private Integer ID;
private String role;
private List<User> userList;
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
5、编写UserMapper类和RoleMapper类
UserMapper.java
package com.tongchenggo.mapper;
import com.tongchenggo.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
@Select("select * from sys_user where sys_user.uid=#{ID}")
@Results(id="userInfoResultMap" , value = {
@Result(column = "uid",property = "ID" ,id = true),
@Result(column = "userName" ,property = "userName"),
@Result(column = "password",property = "password"),
@Result(column = "rid",property = "rid"),
@Result(column = "rid",property ="role", one=@One(select = "com.tongchenggo.mapper.RoleMapper.getRoleByrid") )
})
//通过ID获取用户账号、角色
User getUserInfoByID(Integer ID);
@Results(id = "userResultMap",value = {
@Result(column = "uid",property = "ID" ,id = true),
@Result(column = "userName" ,property = "userName"),
@Result(column = "password",property = "password"),
})
//通过角色ID获取用户集合
@Select("select * from sys_user where sys_user.rid=#{rid}")
List<User> getUserList(Integer rid);
}
RoleMapper.java
package com.tongchenggo.mapper;
import com.tongchenggo.entity.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
@Repository
public interface RoleMapper {
@Results(id="roleResultMap" , value = {
@Result(column = "rid",property = "ID" ,id = true),
@Result(column = "role" ,property = "role")
})
//角色ID查询用户角色
@Select("select * from sys_role where sys_role.rid=#{rid} ")
Role getRoleByrid(Integer rid);
@Results(id="roleUserResultMap" , value = {
@Result(column = "rid",property = "ID" ,id = true),
@Result(column = "role" ,property = "role"),
@Result(column = "rid",property = "userList",many = @Many(select = "com.tongchenggo.mapper.UserMapper.getUserList"))
})
//通过角色id查询所有拥有该角色的用户
@Select("select * from sys_role where sys_role.rid=#{rid}")
Role getRoleWithUserByrid(Integer rid);
}
6、编写UserController类和RoleController类
UserController.java
package com.tongchenggo.controller;
import com.tongchenggo.entity.Role;
import com.tongchenggo.entity.User;
import com.tongchenggo.mapper.RoleMapper;
import com.tongchenggo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/get/{id}")
public User getUserRole(@PathVariable Integer id){
User user = userMapper.getUserInfoByID(id);
return user;
}
}
RoleController.java
package com.tongchenggo.controller;
import com.tongchenggo.entity.Role;
import com.tongchenggo.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleMapper roleMapper;
@RequestMapping("/get/{rid}")
public Role getRole(@PathVariable Integer rid){
Role role = roleMapper.getRoleWithUserByrid(rid);
return role;
}
}
7、测试
通过用户ID获取用户及角色
通过角色ID获取角色及拥有该角色的所有用户
二、角色与权限的关系
一个角色有多个权限,一个权限可以被多个角色拥有,所以角色与权限是双向多对一的关系,即多对多的关系,需要建立三张表,分别为角色表、权限表、及中间表关联。外键创建在中间表中
1、创建权限表及中间表
create table sys_permission(
pid int not null primary key auto_increment,
permission varchar(10) not null
);
create table role_permission(
rpid int not null primary key auto_increment,
rid int,
pid int
);
2、插入数据
insert into sys_permission values(null,"add");
insert into sys_permission values(null,"delete");
insert into sys_permission values(null,"change");
insert into sys_permission values(null,"find");
insert into role_permission values(null,1,1);
insert into role_permission values(null,1,2);
insert into role_permission values(null,1,3);
insert into role_permission values(null,1,4);
insert into role_permission values(null,2,3);
insert into role_permission values(null,2,4);
3、编写Permission类,修改Role类
Permission.java
package com.tongchenggo.entity;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.List;
@Component
public class Permission implements Serializable {
private Integer ID;
private String permission;
private List<Role> roleList;
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public List<Role> getRoleList() {
return roleList;
}
public void setRoleList(List<Role> roleList) {
this.roleList = roleList;
}
}
Role.java新增 List<Permission> permissionList;
package com.tongchenggo.entity;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.List;
@Component
public class Role implements Serializable {
private Integer ID;
private String role;
private List<User> userList;
private List<Permission> permissionList;
public List<Permission> getPermissionList() {
return permissionList;
}
public void setPermissionList(List<Permission> permissionList) {
this.permissionList = permissionList;
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
4、编写PermissionMapper类,修改RoleMapper类
RoleMapper.java
package com.tongchenggo.mapper;
import com.tongchenggo.entity.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface RoleMapper {
@Results(id="roleResultMap" , value = {
@Result(column = "rid",property = "ID" ,id = true),
@Result(column = "role" ,property = "role")
})
//角色ID查询用户角色
@Select("select * from sys_role where sys_role.rid=#{rid} ")
Role getRoleByrid(Integer rid);
@Results(id="roleUserResultMap" , value = {
@Result(column = "rid",property = "ID" ,id = true),
@Result(column = "role" ,property = "role"),
@Result(column = "rid",property = "userList",many = @Many(select = "com.tongchenggo.mapper.UserMapper.getUserList"))
})
//通过角色id查询所有拥有该角色的用户
@Select("select * from sys_role where sys_role.rid=#{rid}")
Role getRoleWithUserByrid(Integer rid);
//引用ID为roleResultMap的结果映射器
@ResultMap(value = "roleResultMap")
@Select("select * from sys_role r where r.rid in(select rp.rid from role_permission rp where rp.pid=#{pid});")
//通过权限ID查询拥有该权限的所有角色
List<Role> getAllRoleBypid(Integer pid);
//通过该结果映射器将所有属性查询出来
@Results(id="rolePermissionUserResultMap" , value = {
@Result(column = "rid",property = "ID" ,id = true),
@Result(column = "role" ,property = "role"),
@Result(column = "rid",property = "userList",many = @Many(select = "com.tongchenggo.mapper.UserMapper.getUserList")),
@Result(column = "rid",property = "permissionList",many = @Many(select = "com.tongchenggo.mapper.PermissionMapper.getPermission"))
})
//通过角色ID查询角色、角色所有的权限及拥有该角色的用户
@Select("select * from sys_role r where r.rid=#{rid}")
Role getUserRoleWithPermission(Integer rid);
}
PermissionMapper.java
package com.tongchenggo.mapper;
import com.tongchenggo.entity.Permission;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PermissionMapper {
@Results(id = "permissionRoleResultMap",value = {
@Result(column = "pid",property = "ID",id = true),
@Result(column = "permission",property = "permission"),
@Result(column = "pid",property = "roleList",many = @Many(select = "com.tongchenggo.mapper.RoleMapper.getAllRoleBypid"))
})
//通过权限ID查询权限及拥有该权限的角色
@Select("select * from sys_permission where sys_permission.pid=#{pid}")
Permission getPermissionWithRoleBypid(Integer pid);
@Results(id = "permissionResultMap",value = {
@Result(column = "pid",property = "ID",id = true),
@Result(column = "permission",property = "permission")
})
//通过角色ID查询该角色拥有的权限
@Select("select * from sys_permission p where p.pid in(select rp.pid from role_permission rp where rp.rid=#{rid})")
List<Permission> getPermission(Integer rid);
}
5、编写PermissionController类,修改RoleController类
RoleController.java
package com.tongchenggo.controller;
import com.tongchenggo.entity.Role;
import com.tongchenggo.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleMapper roleMapper;
@RequestMapping("/get/{rid}")
public Role getRole(@PathVariable Integer rid){
Role role = roleMapper.getRoleWithUserByrid(rid);
return role;
}
@RequestMapping("/getPermission/{rid}")
public Role getRoleWithPermission(@PathVariable Integer rid){
Role role = roleMapper.getUserRoleWithPermission(rid);
return role;
}
}
PermissionController.java
package com.tongchenggo.controller;
import com.tongchenggo.entity.Permission;
import com.tongchenggo.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/permission")
public class PermissionController {
@Autowired
private PermissionMapper permissionMapper;
@RequestMapping("/get/{pid}")
public Permission getPermission(@PathVariable Integer pid){
Permission permission=permissionMapper.getPermissionWithRoleBypid(pid);
return permission;
}
}
7、测试
通过角色ID查询角色、拥有该角色的用户及该角色所拥有的权限
通过权限ID查询权限及拥有该权限的角色