1.项目介绍

1.1介绍

权限管理是所有后台系统都会涉及的一个重要组成部分,而权限管理的核心流程是相似的,如果每个后台单独开发一套权限管理系统,就是重复造轮子,是人力的极大浪费,本项目就是针对这个问题,提供了一套通用的权限解决方案。

  • 项目服务器端架构:SpringBoot + MyBatisPlus + SpringSecurity
  • 前端架构:Node.js + Npm + Vue + ElementUI + Axios
  • 数据缓存:Redis
  • 数据库:Mysql
  • 全局日志记录:AOP

1.2模块划分

最终服务器端架构模块
guigu-auth-parent:根目录,管理子模块:

  • common:公共类父模块
    • common-log:系统操作日志模块
    • common-util:核心工具类
    • service-util:service模块工具类
    • spring-security:spring-security业务模块
  • model:实体类模块
  • service-system:系统权限模块

1.3数据库

code

2.环境搭建

guigu-auth-parent模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.atguigu</groupId>
<artifactId>guigu-auth-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>service-system</module>
<module>model</module>
</modules>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
</parent>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis-plus.version>3.4.1</mybatis-plus.version>
<mysql.version>8.0.23</mysql.version>
<knife4j.version>2.0.8</knife4j.version>
<jwt.version>0.7.0</jwt.version>
<fastjson.version>1.2.29</fastjson.version>
</properties>

<!--配置dependencyManagement锁定依赖的版本-->
<dependencyManagement>
<dependencies>
<!--mybatis-plus 持久层-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--knife4j-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<!--jjwt-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
common模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>guigu-auth-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>common</artifactId>
<packaging>pom</packaging>
<modules>
<module>common-util</module>
<module>service-util</module>
</modules>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
common-util模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>common-util</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>

</project>
service-util模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>service-util</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>common-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
</dependencies>

</project>
model模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>guigu-auth-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>model</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!--lombok用来简化实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
service-system模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>guigu-auth-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>service-system</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

3.定义统一返回结果对象

列表:

1
2
3
4
5
6
7
8
9
10
11
{
"code": 200,
"message": "成功",
"data": [
{
"id": 2,
"roleName": "系统管理员"
}
],
"ok": true
}

分页:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"code": 200,
"message": "成功",
"data": {
"records": [
{
"id": 2,
"roleName": "系统管理员"
},
{
"id": 3,
"name": "普通管理员"
}
],
"total": 10,
"size": 3,
"current": 1,
"orders": [],
"hitCount": false,
"searchCount": true,
"pages": 2
},
"ok": true
}

没有返回数据:

1
2
3
4
5
6
{
"code": 200,
"message": "成功",
"data": null,
"ok": true
}

失败:

1
2
3
4
5
6
{
"code": 201,
"message": "失败",
"data": null,
"ok": false
}
  • 操作模块:common-util
  1. 统一返回结果状态信息类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package com.atguigu.result;

    import lombok.Getter;

    @Getter
    public enum ResultCodeEnum {
    SUCCESS(200,"成功"),
    FAIL(201, "失败"),
    SERVICE_ERROR(2012, "服务异常"),
    DATA_ERROR(204, "数据异常"),
    ILLEGAL_REQUEST(205, "非法请求"),
    REPEAT_SUBMIT(206, "重复提交"),
    ARGUMENT_VALID_ERROR(210, "参数校验异常"),

    LOGIN_AUTH(208, "未登陆"),
    PERMISSION(209, "没有权限"),
    ACCOUNT_ERROR(214, "账号不正确"),
    PASSWORD_ERROR(215, "密码不正确"),
    LOGIN_MOBLE_ERROR( 216, "账号不正确"),
    ACCOUNT_STOP( 217, "账号已停用"),
    NODE_ERROR( 218, "该节点下有子节点,不可以删除")
    ;

    private Integer code;

    private String message;

    private ResultCodeEnum(Integer code, String message) {
    this.code = code;
    this.message = message;
    }
    }

  2. 全局统一返回结果类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    package com.atguigu.result;

    import lombok.Data;

    @Data
    public class Result<T> {
    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    public Result(){}

    // 返回数据
    protected static <T> Result<T> build(T data) {
    Result<T> result = new Result<T>();
    if (data != null)
    result.setData(data);
    return result;
    }

    public static <T> Result<T> build(T body, Integer code, String message) {
    Result<T> result = build(body);
    result.setCode(code);
    result.setMessage(message);
    return result;
    }

    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
    Result<T> result = build(body);
    result.setCode(resultCodeEnum.getCode());
    result.setMessage(resultCodeEnum.getMessage());
    return result;
    }

    public static<T> Result<T> ok(){
    return Result.ok(null);
    }

    /**
    * 操作成功
    * @param data baseCategory1List
    * @param <T>
    * @return
    */
    public static<T> Result<T> ok(T data){
    return build(data, ResultCodeEnum.SUCCESS);
    }

    public static<T> Result<T> fail(){
    return Result.fail(null);
    }

    /**
    * 操作失败
    * @param data
    * @param <T>
    * @return
    */
    public static<T> Result<T> fail(T data){
    return build(data, ResultCodeEnum.FAIL);
    }

    public Result<T> message(String msg){
    this.setMessage(msg);
    return this;
    }

    public Result<T> code(Integer code){
    this.setCode(code);
    return this;
    }
    }

4.knife4j

  • 操作模块:service-uitl
  1. 添加依赖
    1
    2
    3
    4
    <dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    </dependency>
  2. 添加knife4j配置类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    package com.atguigu.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

    import java.util.ArrayList;
    import java.util.List;

    /**
    * knife4j配置信息
    */
    @Configuration
    @EnableSwagger2WebMvc
    public class Knife4jConfig {

    @Bean
    public Docket adminApiConfig(){
    List<Parameter> pars = new ArrayList<>();
    ParameterBuilder tokenPar = new ParameterBuilder();
    tokenPar.name("token")
    .description("用户token")
    .defaultValue("")
    .modelRef(new ModelRef("string"))
    .parameterType("header")
    .required(false)
    .build();
    pars.add(tokenPar.build());
    //添加head参数end

    Docket adminApi = new Docket(DocumentationType.SWAGGER_2)
    .groupName("adminApi")
    .apiInfo(adminApiInfo())
    .select()
    //只显示admin路径下的页面
    .apis(RequestHandlerSelectors.basePackage("com.atguigu"))
    .paths(PathSelectors.regex("/admin/.*"))
    .build()
    .globalOperationParameters(pars);
    return adminApi;
    }

    private ApiInfo adminApiInfo(){

    return new ApiInfoBuilder()
    .title("后台管理系统-API文档")
    .description("本文档描述了后台管理系统微服务接口定义")
    .version("1.0")
    .contact(new Contact("qy", "http://atguigu.com", "493290402@qq.com"))
    .build();
    }


    }
  3. 测试地址:http://localhost:8800/doc.html

5.统一异常处理

  • 操作模块:service-util
  1. 全局异常处理类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    package com.atguigu.handler;

    import com.atguigu.exception.GuiguException;
    import com.atguigu.result.Result;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;


    @ControllerAdvice
    public class GlobalExceptionHandler {

    /**
    * 全局异常处理类
    */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
    e.printStackTrace();
    return Result.fail();
    }
    /**
    * 特定异常处理类
    */
    @ExceptionHandler
    @ResponseBody
    public Result error(ArithmeticException e){
    e.printStackTrace();
    return Result.fail().message("执行了特定异常处理");
    }
    /**
    * 自定义异常处理类
    */
    @ExceptionHandler(GuiguException.class)
    @ResponseBody
    public Result error(GuiguException e){
    e.printStackTrace();
    return Result.fail().message(e.getMessage()).code(e.getCode());
    }

    }

  2. 自定义异常处理类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    package com.atguigu.exception;

    import com.atguigu.result.ResultCodeEnum;
    import lombok.Data;

    /**
    * 自定义全局异常类
    *
    */
    @Data
    public class GuiguException extends RuntimeException {

    private Integer code;

    private String message;

    /**
    * 通过状态码和错误消息创建异常对象
    * @param code
    * @param message
    */
    public GuiguException(Integer code, String message) {
    super(message);
    this.code = code;
    this.message = message;
    }

    /**
    * 接收枚举类型对象
    * @param resultCodeEnum
    */
    public GuiguException(ResultCodeEnum resultCodeEnum) {
    super(resultCodeEnum.getMessage());
    this.code = resultCodeEnum.getCode();
    this.message = resultCodeEnum.getMessage();
    }

    @Override
    public String toString() {
    return "GuliException{" +
    "code=" + code +
    ", message=" + this.getMessage() +
    '}';
    }
    }

6.分页查询

mybatis-plus搭建
  1. 配置yaml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    server:
    port: 8800
    mybatis-plus:
    configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 查看日志
    spring:
    datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/guigu-auth?characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
  2. 启动类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.atguigu.system;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @MapperScan(basePackages = "com.atguigu.system.mapper")
    @SpringBootApplication
    public class ServiceAuthApplication {

    public static void main(String[] args) {
    SpringApplication.run(ServiceAuthApplication.class, args);
    }

    }
  3. 实体类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    package com.atguigu.model.system;

    import com.baomidou.mybatisplus.annotation.TableField;
    import com.baomidou.mybatisplus.annotation.TableName;
    import com.atguigu.model.base.BaseEntity;
    import lombok.Data;

    @Data
    @TableName("sys_role")
    public class SysRole extends BaseEntity {

    private static final long serialVersionUID = 1L;

    //角色名称
    @TableField("role_name")
    private String roleName;

    //角色编码
    @TableField("role_code")
    private String roleCode;

    //描述
    @TableField("description")
    private String description;

    }
  4. 添加Mapper类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package com.atguigu.system.mapper;

    import com.atguigu.model.auth.SysRole;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.apache.ibatis.annotations.Mapper;

    @Repository
    public interface SysRoleMapper extends BaseMapper<SysRole> {

    }
  5. 添加Service类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package com.atguigu.system.service;

    import com.atguigu.model.auth.SysRole;
    import com.baomidou.mybatisplus.extension.service.IService;

    import java.util.List;

    public interface SysRoleService extends IService<SysRole> {

    }
  6. 添加serviceImpl类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package com.atguigu.system.service.impl;

    import com.atguigu.auth.mapper.SysRoleMapper;
    import com.atguigu.auth.service.SysRoleService;
    import com.atguigu.model.auth.SysRole;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.stereotype.Service;
    import org.springframework.beans.factory.annotation.Autowired;

    import java.util.List;

    @Transactional
    @Service
    public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    }
  1. 配置分页插件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    package com.atguigu.config;

    import com.baomidou.mybatisplus.annotation.DbType;
    import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.transaction.annotation.EnableTransactionManagement;

    /**
    * MybatisPlus配置类
    *
    */
    @EnableTransactionManagement
    @Configuration
    public class MybatisPlusConfig {

    /**
    *
    * @return
    */
    @Bean
    public MybatisPlusInterceptor addPaginationInnerInterceptor(){
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    //向Mybatis过滤器链中添加分页拦截器
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
    }

    }
  2. SysRoleController
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    @Api(tags = "角色管理")
    @RestController
    @RequestMapping("/admin/system/sysRole")
    public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    //分页查询
    @ApiOperation(value = "角色分页查询")
    @GetMapping("/{pageNum}/{pageSize}")
    public Result index(
    @ApiParam(name = "pageNum", value = "当前页码", required = true)
    @PathVariable Long pageNum,

    @ApiParam(name = "pageSize", value = "每页记录数", required = true)
    @PathVariable Long pageSize,

    @ApiParam(name = "roleQueryVo", value = "查询对象", required = false)
    SysRoleQueryVo roleQueryVo) {
    //1.使用mybatis 默认的分页方法
    // Page page = new Page(pageNum,pageSize);
    // QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
    // String roleName = roleQueryVo.getRoleName();
    // queryWrapper.like(roleName == null ? false : true,"role_name",roleName);
    // Page pageRoleList = sysRoleService.page(page, queryWrapper);
    // return Result.ok(pageRoleList);

    //2.自定义分页方法
    Page page = new Page(pageNum,pageSize);
    Page<SysRole> Page = sysRoleService.findPage(page,roleQueryVo);
    return Result.ok(Page);
    }
  3. SysRoleService —-自定义方法才需要
    1
    2
    3
    4
    public interface SysRoleService extends IService<SysRole> {

    Page<SysRole> findPage(Page page, SysRoleQueryVo roleQueryVo);
    }
  4. SysRoleServiceImpl
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @Service
    public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper,SysRole>implements SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;

    //分页查询
    @Override
    public Page<SysRole> findPage(Page page, SysRoleQueryVo roleQueryVo) {
    return sysRoleMapper.findPage(page,roleQueryVo);
    }
    }
  5. SysRoleMapper —-在resource/mapper目录下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    <!-- resultType 自动映射-->
    <mapper namespace="com.atguigu.mapper.SysRoleMapper">
    <select id="findPage" resultType="com.atguigu.system.SysRole">
    select * from sys_role
    <where>
    <if test="vo.roleName != null and vo.roleName != ''">
    role_name like concat('%',#{vo.roleName},'%')
    </if>
    and is_deleted=0
    </where>
    </select>
    </mapper>

7.简单的增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

@Autowired
private SysRoleService sysRoleService;
//根据id查询角色
@ApiOperation("根据id查询角色")
@GetMapping("/get/{id}")
public Result<SysRole> get(
@ApiParam(name = "id",value = "角色主键",required = true)
@PathVariable Long id){
SysRole role = sysRoleService.getById(id);
return Result.ok(role);
}

//添加角色
@ApiOperation("添加角色")
@PostMapping("/save")
public Result<SysRole> save(@RequestBody SysRole sysRole){
sysRoleService.save(sysRole);
return Result.ok();
}

//修改角色
@ApiOperation("修改角色")
@PutMapping("/update")
public Result<SysRole> update(@RequestBody SysRole sysRole){
sysRoleService.updateById(sysRole);
return Result.ok();
}

//删除角色
@ApiOperation(value = "删除角色")
@DeleteMapping("/remove/{id}")
public Result remove(@PathVariable Long id) {
sysRoleService.removeById(id);
return Result.ok();
}

//根据id删除角色
@ApiOperation(value = "根据id列表删除")
@DeleteMapping("/batchRemove")
public Result batchRemove(@RequestBody List<Long> idList) {
sysRoleService.removeByIds(idList);
return Result.ok();
}
}