如何構建Springboot多模塊項目

文章目錄

1、工程簡介

下圖是我們最終需要構建出來的單應用多模塊項目的完整結構

主要包括:

  • boss-bes-exam (父工程)
    • boss-bes-exam-config
    • boss-bes-exam-controller
    • boss-bes-exam-dao
    • boss-bes-exam-pojo
    • boss-bes-exam-service
    • boss-bes-exam-util
      在這裏插入圖片描述

需要實現的功能

1、完成用戶和角色的基本增加和查詢操作;
2、使用Swagger2生成接口文檔便於測試;
3、使用Mybatis插件:通用Mapper輔助開發;
4、使用阿里Druid數據庫連接池,並配置監控頁面。

2、項目構建

注:基於idea進行項目構建與開發

2.1 創建父工程

2.1.1 new Project (由於本人是在空工程上構建,所以是new model)

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

2.1.2 由於當前是一個父工程,除了保留pom.xml之外,可以刪除其他無用部分(src 以及 其他)

在這裏插入圖片描述

2.1.3 修改pom.xml文件

父工程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>
    <!--添加打包類型爲pom-->
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bosssoft.hr.bes</groupId>
    <artifactId>boss-bes-exam</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam</name>
    <description>boss 考試系統</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!--定義版本-->
        <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
        <java.version>1.8</java.version>
        <mybatis.version>2.1.1</mybatis.version>
        <swagger2.version>2.9.1</swagger2.version>
        <lombok.version>1.18.10</lombok.version>
        <druid.version>1.1.10</druid.version>
        <pagehelper.version>4.2.1</pagehelper.version>
        <tkmybatis.version>2.0.3-beta1</tkmybatis.version>
        <mysql.version>8.0.18</mysql.version>
        <springboot.version>2.2.1.RELEASE</springboot.version>
    </properties>

    <!--管理庫-->
    <dependencyManagement>
        <dependencies>

        </dependencies>
    </dependencyManagement>

    <!--公共庫,所有子模塊都會自動繼承該標籤內定義的依賴-->
    <!--定義依賴-->
    <dependencies>
        <!-- WEB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${springboot.version}</version>
        </dependency>

        <!-- TEST -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${springboot.version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>

        <!-- devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>${springboot.version}</version>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- JPA -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${springboot.version}</version>
        </dependency>


        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <!-- Mybatis plugin -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>${tkmybatis.version}</version>
        </dependency>

        <!--分頁插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>

        <!--Druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!--Swagger2 依賴 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger2.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger2.version}</version>
        </dependency>

    </dependencies>

    <!--<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>-->


    <build>
        <plugins>
            <!-- 注意: 因爲我們不需要demo-parent是一個可運行的項目,所以修改原來的打包方式爲maven打包方式 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <!--執行測試用例的插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!--跳過項目運行測試用例-->
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2.2 創建其餘子模塊工程

2.2.1 創建boss-bes-exam-pojo

2.2.1.1 創建子模塊

在這裏插入圖片描述
選擇父工程,new module
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

2.2.1.2 刪除其他無用文件

注:只需要在controller模塊保留啓動類其他模塊的啓動類都可以刪除
在這裏插入圖片描述

2.2.1.3 boss-bes-exam-pojo 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>
    <!--設置打包方式爲jar-->
    <packaging>jar</packaging>
    <!--設置父工程-->
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>

    <groupId>com.bosssoft.hr.bes.exam.center.pojo</groupId>
    <artifactId>boss-bes-exam-pojo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-pojo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

    </dependencies>

</project>

2.2.2 創建 boss-bes-exam-config

2.2.2.1 創建子模塊

創建方式同2.2.1.1
在這裏插入圖片描述
在這裏插入圖片描述

2.2.2.2 刪除無用文件

同2.2.1.2
在這裏插入圖片描述

2.2.2.3 boss-bes-exam-config 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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>com.bosssoft.hr.bes.exam.center.config</groupId>
    <artifactId>boss-bes-exam-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

    </dependencies>
</project>

2.2.3 創建 boss-bes-exam-util

2.2.3.1 創建子模塊

同上
在這裏插入圖片描述
在這裏插入圖片描述

2.2.3.2 刪除無用文件

在這裏插入圖片描述

2.2.3.3 boss-bes-exam-util 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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>

    <groupId>com.bosssoft.hr.bes.exam.center.util</groupId>
    <artifactId>boss-bes-exam-util</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-util</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

    </dependencies>

</project>

2.2.4 創建 boss-bes-exam-dao

2.2.4.1 創建子模塊

同上
在這裏插入圖片描述
在這裏插入圖片描述

2.2.4.2 刪除無用文件

在這裏插入圖片描述

2.2.4.3 boss-bes-exam-dao 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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>
    <groupId>com.bosssoft.hr.bes.exam.center.dao</groupId>
    <artifactId>boss-bes-exam-dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-dao</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

    </dependencies>
</project>

2.2.5 創建 boss-bes-exam-service

2.2.5.1 創建子模塊

在這裏插入圖片描述
在這裏插入圖片描述

2.2.5.2 刪除無用文件

在這裏插入圖片描述

2.2.5.3 boss-bes-exam-service 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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>
    <groupId>com.bosssoft.hr.bes.exam.center.service</groupId>
    <artifactId>boss-bes-exam-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

    </dependencies>
</project>

2.2.6 創建 boss-bes-exam-controller

2.2.6.1 創建子模塊

在這裏插入圖片描述
在這裏插入圖片描述

2.2.6.2 刪除無用文件

Controller 保存類啓動文件
在這裏插入圖片描述

2.2.6.3 boss-bes-exam-controller 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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>

    <groupId>com.bosssoft.hr.bes.exam.center.controller</groupId>
    <artifactId>boss-bes-exam-controller</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-controller</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

    </dependencies>

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

2.2.7 修改模塊pom.xml文件

2.2.7.1 父工程 pom.xml

添加子模塊moduls

<?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>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bosssoft.hr.bes</groupId>
    <artifactId>boss-bes-exam</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam</name>
    <description>bossoft 考試系統</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
        <java.version>1.8</java.version>
        <mybatis.version>2.1.1</mybatis.version>
        <swagger2.version>2.9.1</swagger2.version>
        <lombok.version>1.18.10</lombok.version>
        <druid.version>1.1.10</druid.version>
        <pagehelper.version>4.2.1</pagehelper.version>
        <tkmybatis.version>2.0.3-beta1</tkmybatis.version>
        <mysql.version>8.0.18</mysql.version>
        <springboot.version>2.2.1.RELEASE</springboot.version>
    </properties>

    <!--依賴配置-->
    <dependencies>
        <!-- WEB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${springboot.version}</version>
        </dependency>

        <!-- TEST -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${springboot.version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>

        <!-- devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>${springboot.version}</version>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- JPA -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${springboot.version}</version>
        </dependency>


        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <!-- Mybatis plugin -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>${tkmybatis.version}</version>
        </dependency>

        <!--分頁插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>

        <!--Druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!--Swagger2 依賴 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger2.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger2.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!-- 注意: 因爲我們不需要demo-parent是一個可運行的項目,所以修改原來的打包方式爲maven打包方式 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <!--執行測試用例的插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!--跳過項目運行測試用例-->
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!--模塊-->
    <modules>
        <module>boss-bes-exam-pojo</module>
        <module>boss-bes-exam-config</module>
        <module>boss-bes-exam-controller</module>
        <module>boss-bes-exam-dao</module>
        <module>boss-bes-exam-util</module>
        <module>boss-bes-exam-service</module>
    </modules>
</project>

2.2.7.2 boss-bes-exam-util 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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>

    <groupId>com.bosssoft.hr.bes.exam.center.util</groupId>
    <artifactId>boss-bes-exam-util</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-util</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

    </dependencies>

</project>

2.2.7.3 boss-bes-exam-pojo pom.xml

依賴util模塊

<?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>
    <!--設置打包方式爲jar-->
    <packaging>jar</packaging>
    <!--設置父工程-->
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>

    <groupId>com.bosssoft.hr.bes.exam.center.pojo</groupId>
    <artifactId>boss-bes-exam-pojo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-pojo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--引入模塊依賴-->
        <!-- util -->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.util</groupId>
            <artifactId>boss-bes-exam-util</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2.2.7.4 boss-bes-exam-config pom.xml

依賴pojo模塊

<?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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>com.bosssoft.hr.bes.exam.center.config</groupId>
    <artifactId>boss-bes-exam-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--引入模塊依賴-->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.pojo</groupId>
            <artifactId>boss-bes-exam-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

2.2.7.5 boss-bes-exam-dao pom.xml

依賴pojo、util

<?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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>
    <groupId>com.bosssoft.hr.bes.exam.center.dao</groupId>
    <artifactId>boss-bes-exam-dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-dao</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--引入模塊依賴-->
        <!-- pojo -->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.pojo</groupId>
            <artifactId>boss-bes-exam-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- util -->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.util</groupId>
            <artifactId>boss-bes-exam-util</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
</project>

2.2.7.6 boss-bes-exam-service pom.xml

依賴pojo、util、dao

<?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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>
    <groupId>com.bosssoft.hr.bes.exam.center.service</groupId>
    <artifactId>boss-bes-exam-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>


        <!--引入模塊依賴-->
        <!-- pojo -->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.pojo</groupId>
            <artifactId>boss-bes-exam-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- dao -->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.dao</groupId>
            <artifactId>boss-bes-exam-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- util -->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.util</groupId>
            <artifactId>boss-bes-exam-util</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

2.2.7.7 boss-bes-exam-controller pom.xml

依賴pojo、config、service

<?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>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.bosssoft.hr.bes</groupId>
        <artifactId>boss-bes-exam</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath><!-- lookup parent from repository -->
    </parent>

    <groupId>com.bosssoft.hr.bes.exam.center.controller</groupId>
    <artifactId>boss-bes-exam-controller</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam-controller</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--引入模塊依賴-->
        <!-- pojo -->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.pojo</groupId>
            <artifactId>boss-bes-exam-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- config -->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.config</groupId>
            <artifactId>boss-bes-exam-config</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- service -->
        <dependency>
            <groupId>com.bosssoft.hr.bes.exam.center.service</groupId>
            <artifactId>boss-bes-exam-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.3 測試環境

2.3.1 修改父工程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>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bosssoft.hr.bes</groupId>
    <artifactId>boss-bes-exam</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam</name>
    <description>bossoft 考試系統</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
        <java.version>1.8</java.version>
        <mybatis.version>2.1.1</mybatis.version>
        <swagger2.version>2.9.1</swagger2.version>
        <lombok.version>1.18.10</lombok.version>
        <druid.version>1.1.10</druid.version>
        <pagehelper.version>4.2.1</pagehelper.version>
        <tkmybatis.version>2.0.3-beta1</tkmybatis.version>
        <mysql.version>8.0.18</mysql.version>
        <springboot.version>2.2.1.RELEASE</springboot.version>
    </properties>

    <!--依賴配置-->
    <dependencies>
        <!-- WEB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${springboot.version}</version>
        </dependency>

        <!-- TEST -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${springboot.version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>

        <!-- devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>${springboot.version}</version>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- MySQL -->
        <!--<dependency>-->
            <!--<groupId>mysql</groupId>-->
            <!--<artifactId>mysql-connector-java</artifactId>-->
            <!--<version>${mysql.version}</version>-->
            <!--<scope>runtime</scope>-->
        <!--</dependency>-->

        <!-- JPA -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-data-jpa</artifactId>-->
            <!--<version>${springboot.version}</version>-->
        <!--</dependency>-->


        <!-- Mybatis -->
        <!--<dependency>-->
            <!--<groupId>org.mybatis.spring.boot</groupId>-->
            <!--<artifactId>mybatis-spring-boot-starter</artifactId>-->
            <!--<version>${mybatis.version}</version>-->
        <!--</dependency>-->

        <!-- Mybatis plugin -->
        <!--<dependency>-->
            <!--<groupId>tk.mybatis</groupId>-->
            <!--<artifactId>mapper-spring-boot-starter</artifactId>-->
            <!--<version>${tkmybatis.version}</version>-->
        <!--</dependency>-->

        <!--分頁插件 -->
        <!--<dependency>-->
            <!--<groupId>com.github.pagehelper</groupId>-->
            <!--<artifactId>pagehelper</artifactId>-->
            <!--<version>${pagehelper.version}</version>-->
        <!--</dependency>-->

        <!--Druid-->
        <!--<dependency>-->
            <!--<groupId>com.alibaba</groupId>-->
            <!--<artifactId>druid-spring-boot-starter</artifactId>-->
            <!--<version>${druid.version}</version>-->
        <!--</dependency>-->

        <!--Swagger2 依賴 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger2.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger2.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!-- 注意: 因爲我們不需要demo-parent是一個可運行的項目,所以修改原來的打包方式爲maven打包方式 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <!--執行測試用例的插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!--跳過項目運行測試用例-->
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!--模塊-->
    <modules>
        <module>boss-bes-exam-pojo</module>
        <module>boss-bes-exam-config</module>
        <module>boss-bes-exam-controller</module>
        <module>boss-bes-exam-dao</module>
        <module>boss-bes-exam-util</module>
        <module>boss-bes-exam-service</module>
    </modules>
</project>

2.3.2 編寫測試代碼

2.3.2.1 基本結構

添加HelloController、IHelloService、HelloService(實現IHelloService)、IHelloDao、HelloDao(實現IHelloDao)
在這裏插入圖片描述

2.3.2.1 基本代碼

boss-bes-exam-controller模塊下

啓動類:
BossBesExamControllerApplication.java

package com.bosssoft.hr.bes.exam.center;

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

//設置基礎包掃描路徑,默認掃描路徑爲啓動類路徑及其子包
@SpringBootApplication(scanBasePackages = {"com.bosssoft.hr.bes"})
public class BossBesExamControllerApplication {

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

}

HelloController .java

package com.bosssoft.hr.bes.exam.center.controller;

import com.bosssoft.hr.bes.exam.center.service.IHelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author LGX_TvT
 * @date 2019-11-27 11:37
 */
@RestController
public class HelloController {

    @Autowired
    IHelloService helloService;

    @RequestMapping("/hello")
    public String hello(){
        return helloService.sayHello();
    }
}

boss-bes-exam-service模塊下

IHelloService .java

package com.bosssoft.hr.bes.exam.center.service;

/**
 * @author LGX_TvT
 * @date 2019-11-27 11:38
 */
public interface IHelloService {

    String sayHello();
}

HelloService .java

package com.bosssoft.hr.bes.exam.center.service;

import com.bosssoft.hr.bes.exam.center.dao.IHelloDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author LGX_TvT
 * @date 2019-11-27 11:38
 */
@Service
public class HelloService implements IHelloService {

    @Autowired
    IHelloDao helloDao;

    @Override
    public String sayHello() {
        return helloDao.sayHello();
    }
}

boss-bes-exam-dao模塊下

IHelloDao.java

/**
 * @author LGX_TvT
 * @date 2019-11-27 11:39
 */
public interface IHelloDao {
    String sayHello();
}

HelloDao.java

/**
 * @author LGX_TvT
 * @date 2019-11-27 11:40
 */
@Repository
public class HelloDao implements IHelloDao {
    @Override
    public String sayHello() {
        return "Hello World";
    }
}

2.3.3 測試結果

在這裏插入圖片描述

3、項目功能實現

把測試中父工程POM.XML註釋掉的依賴還原
父工程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>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bosssoft.hr.bes</groupId>
    <artifactId>boss-bes-exam</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boss-bes-exam</name>
    <description>bossoft 考試系統</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
        <java.version>1.8</java.version>
        <mybatis.version>2.1.1</mybatis.version>
        <swagger2.version>2.9.1</swagger2.version>
        <lombok.version>1.18.10</lombok.version>
        <druid.version>1.1.10</druid.version>
        <pagehelper.version>4.2.1</pagehelper.version>
        <tkmybatis.version>2.0.3-beta1</tkmybatis.version>
        <mysql.version>8.0.18</mysql.version>
        <springboot.version>2.2.1.RELEASE</springboot.version>
    </properties>

    <!--依賴配置-->
    <dependencies>
        <!-- WEB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${springboot.version}</version>
        </dependency>

        <!-- TEST -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${springboot.version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>

        <!-- devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>${springboot.version}</version>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- JPA -->
        <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${springboot.version}</version>
        </dependency>


        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <!-- Mybatis plugin -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>${tkmybatis.version}</version>
        </dependency>

        <!--分頁插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>

        <!--Druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!--Swagger2 依賴 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger2.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger2.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!-- 注意: 因爲我們不需要demo-parent是一個可運行的項目,所以修改原來的打包方式爲maven打包方式 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <!--執行測試用例的插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!--跳過項目運行測試用例-->
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!--模塊-->
    <modules>
        <module>boss-bes-exam-pojo</module>
        <module>boss-bes-exam-config</module>
        <module>boss-bes-exam-controller</module>
        <module>boss-bes-exam-dao</module>
        <module>boss-bes-exam-util</module>
        <module>boss-bes-exam-service</module>
    </modules>
</project>

3.0 Util添加ID自動生成工具類

在這裏插入圖片描述

package com.bosssoft.hr.bes.exam.center.util;

/**
 * @author LGX_TvT
 * @date 2019-11-26 13:46
 */

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * id自增器(雪花算法)
 *
 * @author renjie
 * @version 1.0.0
 */
public class SnowFlake {
    private final static long twepoch = 12888349746579L;
    // 機器標識位數
    private final static long workerIdBits = 5L;
    // 數據中心標識位數
    private final static long datacenterIdBits = 5L;

    // 毫秒內自增位數
    private final static long sequenceBits = 12L;
    // 機器ID偏左移12位
    private final static long workerIdShift = sequenceBits;
    // 數據中心ID左移17位
    private final static long datacenterIdShift = sequenceBits + workerIdBits;
    // 時間毫秒左移22位
    private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
    //sequence掩碼,確保sequnce不會超出上限
    private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
    //上次時間戳
    private static long lastTimestamp = -1L;
    //序列
    private long sequence = 0L;
    //服務器ID
    private long workerId = 1L;
    private static long workerMask = -1L ^ (-1L << workerIdBits);
    //進程編碼
    private long processId = 1L;
    private static long processMask = -1L ^ (-1L << datacenterIdBits);

    private static SnowFlake snowFlake = null;

    static{
        snowFlake = new SnowFlake();
    }
    public static synchronized long nextId(){
        return snowFlake.getNextId();
    }

    private SnowFlake() {

        //獲取機器編碼
        this.workerId=this.getMachineNum();
        //獲取進程編碼
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        this.processId=Long.valueOf(runtimeMXBean.getName().split("@")[0]).longValue();

        //避免編碼超出最大值
        this.workerId=workerId & workerMask;
        this.processId=processId & processMask;
    }

    public synchronized long getNextId() {
        //獲取時間戳
        long timestamp = timeGen();
        //如果時間戳小於上次時間戳則報錯
        if (timestamp < lastTimestamp) {
            try {
                throw new Exception("Clock moved backwards.  Refusing to generate id for " + (lastTimestamp - timestamp) + " milliseconds");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //如果時間戳與上次時間戳相同
        if (lastTimestamp == timestamp) {
            // 當前毫秒內,則+1,與sequenceMask確保sequence不會超出上限
            sequence = (sequence + 1) & sequenceMask;
            if (sequence == 0) {
                // 當前毫秒內計數滿了,則等待下一秒
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0;
        }
        lastTimestamp = timestamp;
        // ID偏移組合生成最終的ID,並返回ID
        long nextId = ((timestamp - twepoch) << timestampLeftShift) | (processId << datacenterIdShift) | (workerId << workerIdShift) | sequence;
        return nextId;
    }

    /**
     * 再次獲取時間戳直到獲取的時間戳與現有的不同
     * @param lastTimestamp
     * @return 下一個時間戳
     */
    private long tilNextMillis(final long lastTimestamp) {
        long timestamp = this.timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = this.timeGen();
        }
        return timestamp;
    }

    private long timeGen() {
        return System.currentTimeMillis();
    }

    /**
     * 獲取機器編碼
     * @return
     */
    private long getMachineNum(){
        long machinePiece;
        StringBuilder sb = new StringBuilder();
        Enumeration<NetworkInterface> e = null;
        try {
            e = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e1) {
            e1.printStackTrace();
        }
        while (e.hasMoreElements()) {
            NetworkInterface ni = e.nextElement();
            sb.append(ni.toString());
        }
        machinePiece = sb.toString().hashCode();
        return machinePiece;
    }
}

3.1 配置Druid數據源

3.1.1 在controller模塊中添加yml配置

在這裏插入圖片描述
具體配置如下:

spring:
  application:
    name: upfs-provider
  datasource:
    name: dataSource1                               #如果存在多個數據源,監控的時候可以通過名字來區分開來。如果沒有配置,將會生成一個名字,格式是:"DataSource-" + System.identityHashCode(this)
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/bossoft_exam?serverTimezone=UTC&characterEncoding=utf-8
    username: root
    password: root
    # 下面爲連接池的補充設置
    druid:
      initialSize: 10                               #初始化連接個數
      minIdle: 10                                   #最小空閒連接個數
      maxActive: 100                                #最大連接個數
      maxWait: 60000                                #獲取連接時最大等待時間,單位毫秒。
      timeBetweenEvictionRunsMillis: 60000          #配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
      minEvictableIdleTimeMillis: 30000             #配置一個連接在池中最小生存的時間,單位是毫秒
      validationQuery: select 'x'                   #用來檢測連接是否有效的sql,要求是一個查詢語句。
      testWhileIdle: true                           #建議配置爲true,不影響性能,並且保證安全性。如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。
      testOnBorrow: true                            #申請連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能
      testOnReturn: false                           #歸還連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能
      poolPreparedStatements: false                 #是否緩存preparedStatement,也就是PSCache。PSCache對支持遊標的數據庫性能提升巨大,比如說oracle。在mysql下建議關閉。
      maxPoolPreparedStatementPerConnectionSize: -1 #要啓用PSCache,必須配置大於0,當大於0時,poolPreparedStatements自動觸發修改爲true。在Druid中,不會存在Oracle下PSCache佔用內存過多的問題,可以把這個數值配置大一些,比如說100
      filters: stat,wall                            #通過別名的方式配置擴展插件,常用的插件有:監控統計用的filter:stat,日誌用的filter:log4j,防禦sql注入的filter:wall
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
      useGlobalDataSourceStat: false                # 合併多個DruidDataSource的監控數據

# Mybatis配置
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml # 指定全局配置文件的位置
  mapper-locations: classpath:mybatis/mapper/*.xml      # 指定sql映射文件的位置

# 服務器配置
server:
  port: 8080

3.1.2 在Config模塊中添加Druid配置類

在這裏插入圖片描述
具體代碼如下

package com.bosssoft.hr.bes.exam.center.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


/**
 * druid監控控制檯配置
 * http://localhost:port/contextpath/druid
 */
@Configuration
public class DruidConfig {

    /**
     * // 主要實現web監控的配置處理
     * @return
     */
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");//表示進行druid監控的配置處理操作
        servletRegistrationBean.addInitParameter("loginUsername", "root");//用戶名
        servletRegistrationBean.addInitParameter("loginPassword", "root");//密碼
        servletRegistrationBean.addInitParameter("resetEnable", "false");//是否可以重置數據源
        return servletRegistrationBean;

    }

    /**
     * //監控
     * @return
     */
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");//所有請求進行監控處理
        filterRegistrationBean.addInitParameter("exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");//排除
        return filterRegistrationBean;
    }

    /**
     * 配置數據源
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }
}

3.2 配置Swagger2

3.2.1 在Config模塊中添加Swagger2配置類

在這裏插入圖片描述
具體代碼

package com.bosssoft.hr.bes.exam.center.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * 頂層的接口
 * @author LGX_TvT
 * @date 2019-11-25 19:25
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //掃描包路徑
                .apis(RequestHandlerSelectors.basePackage("com.bosssoft.hr.bes.exam.center"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //標題
                .title("springboot利用swagger構建api文檔")
                //描述
                .description("簡單優雅的restful風格")
                //(不可見)條款地址
                .termsOfServiceUrl("https://blog.csdn.net/l1336037686")
                //版本號
                .version("1.0")
                .build();
    }
}

3.3 在POJO模塊添加逆行工程生成的實體類與自定義的領域模型

在這裏插入圖片描述

具體代碼(合併在一起):

/**
 * 用戶增加DTO
 * @author LGX_TvT
 * @date 2019-11-26 14:49
 */
@Data
public class UserInsertDTO {

    private User user;
    private List<Long> roles;

    public UserInsertDTO(User user, List<Long> roles) {
        this.user = user;
        this.roles = roles;
    }

    public UserInsertDTO() {
    }
}


package com.bosssoft.hr.bes.exam.center.pojo.dto;

import com.bosssoft.hr.bes.exam.center.pojo.entity.Role;
import com.bosssoft.hr.bes.exam.center.pojo.entity.User;
import lombok.Data;

import java.util.List;

/**
 * 用戶查詢操作結果DTO
 * @author LGX_TvT
 * @date 2019-11-26 9:28
 */
@Data
public class UserSelectDTO {
    private User user;
    private List<Role> roles;

    public UserSelectDTO(User user, List<Role> roles) {
        this.user = user;
        this.roles = roles;
    }

    public UserSelectDTO() {
    }
}


package com.bosssoft.hr.bes.exam.center.pojo.entity;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Table;
import java.util.Date;

@Getter
@Setter
@Table(name="t_resource")
public class Resource {
    private Long id;

    private String name;

    private String code;

    private Integer orderIndex;

    private Long parentId;

    private String url;

    private String openImg;

    private String closeImg;

    private Byte resourceType;

    private Byte leaf;

    private String remark;

    private Byte status;

    private Long createdBy;

    private Date createdTime;

    private Long updatedBy;

    private Date updatedTime;

    private Long version;

    public Resource(Long id, String name, String code, Integer orderIndex, Long parentId, String url, String openImg, String closeImg, Byte resourceType, Byte leaf, String remark, Byte status, Long createdBy, Date createdTime, Long updatedBy, Date updatedTime, Long version) {
        this.id = id;
        this.name = name;
        this.code = code;
        this.orderIndex = orderIndex;
        this.parentId = parentId;
        this.url = url;
        this.openImg = openImg;
        this.closeImg = closeImg;
        this.resourceType = resourceType;
        this.leaf = leaf;
        this.remark = remark;
        this.status = status;
        this.createdBy = createdBy;
        this.createdTime = createdTime;
        this.updatedBy = updatedBy;
        this.updatedTime = updatedTime;
        this.version = version;
    }

    public Resource() {
    }
}

package com.bosssoft.hr.bes.exam.center.pojo.entity;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.Table;
import java.util.Date;

@Getter
@Setter
@Data
@Table(name="t_role")
public class Role {

    private Long id;

    private String name;

    private String code;

    private String remark;

    private Byte status;

    private Long createdBy;

    private Date createdTime;

    private Long updatedBy;

    private Date updatedTime;

    private Long version;

    public Role(Long id, String name, String code, String remark, Byte status, Long createdBy, Date createdTime, Long updatedBy, Date updatedTime, Long version) {
        this.id = id;
        this.name = name;
        this.code = code;
        this.remark = remark;
        this.status = status;
        this.createdBy = createdBy;
        this.createdTime = createdTime;
        this.updatedBy = updatedBy;
        this.updatedTime = updatedTime;
        this.version = version;
    }

    public Role() {
    }
}

package com.bosssoft.hr.bes.exam.center.pojo.entity;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Table;

@Getter
@Setter
@Table(name="t_role_resource")
public class RoleResource {
    private Long id;

    private Long role;

    private Long resource;

    public RoleResource() {
    }

    public RoleResource(Long id, Long role, Long resource) {
        this.id = id;
        this.role = role;
        this.resource = resource;
    }
}

package com.bosssoft.hr.bes.exam.center.pojo.entity;

import io.swagger.annotations.ApiModel;
import lombok.Data;

import javax.persistence.Table;
import java.util.Date;

@ApiModel("User")
@Data
@Table(name="t_user")
public class User {
    private Long id;

    private String code;

    private String password;

    private String name;

    private String profilePicture;

    private Byte sex;

    private Date birthday;

    private String tel;

    private String email;

    private String other;

    private String remark;

    private Byte status;

    private Long createdBy;

    private Date createdTime;

    private Long updatedBy;

    private Date updatedTime;

    private Long version;

    public User(Long id, String code, String password, String name, String profilePicture, Byte sex, Date birthday, String tel, String email, String other, String remark, Byte status, Long createdBy, Date createdTime, Long updatedBy, Date updatedTime, Long version) {
        this.id = id;
        this.code = code;
        this.password = password;
        this.name = name;
        this.profilePicture = profilePicture;
        this.sex = sex;
        this.birthday = birthday;
        this.tel = tel;
        this.email = email;
        this.other = other;
        this.remark = remark;
        this.status = status;
        this.createdBy = createdBy;
        this.createdTime = createdTime;
        this.updatedBy = updatedBy;
        this.updatedTime = updatedTime;
        this.version = version;
    }

    public User() {
    }
}

package com.bosssoft.hr.bes.exam.center.pojo.entity;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Table;

@Getter
@Setter
@Table(name="t_user_role")
public class UserRole {
    private Long id;

    private Long user;

    private Long role;

    public UserRole() {

    }

    public UserRole(Long id, Long user, Long role) {
        this.id = id;
        this.user = user;
        this.role = role;
    }
}

package com.bosssoft.hr.bes.exam.center.pojo.vo;

import lombok.Data;

/**
 * @author LGX_TvT
 * @date 2019-11-26 11:56
 */
@Data
public class MessageVO {
    private int code;
    private Object msg;

    public MessageVO(int code, Object msg) {
        this.code = code;
        this.msg = msg;
    }

}

package com.bosssoft.hr.bes.exam.center.pojo.vo;

import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * 用戶增加VO
 * @author LGX_TvT
 * @date 2019-11-26 11:20
 */
@Data
public class UserInsertVO {
    private String code;
    private String password;

    private String name;
    private Byte sex;
    private Date birthday;

    private String tel;
    private String email;
    private Byte status;

    private Long createdBy;
    List<Long> roles;

    public UserInsertVO() {
    }

    public UserInsertVO(String code, String password, String name, Byte sex, Date birthday, String tel, String email, Byte status, Long createdBy, List<Long> roles) {
        this.code = code;
        this.password = password;
        this.name = name;
        this.sex = sex;
        this.birthday = birthday;
        this.tel = tel;
        this.email = email;
        this.status = status;
        this.createdBy = createdBy;
        this.roles = roles;
    }
}

package com.bosssoft.hr.bes.exam.center.pojo.vo;

import com.bosssoft.hr.bes.exam.center.pojo.entity.Role;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * 用戶查詢VO
 * @author LGX_TvT
 * @date 2019-11-26 13:36
 */
@Data
public class UserSelectVO {

    //user
    private Long id;
    private String code;
    private String name;

    private String profilePicture;
    private Byte sex;
    private Date birthday;

    private String tel;
    private String email;
    private String remark;

    private Byte status;
    private Long createdBy;
    private Date createdTime;

    private Long updatedBy;
    private Date updatedTime;
    private Long version;

    List<Role> roles;

    public UserSelectVO() {
    }

    public UserSelectVO(Long id, String code, String name, String profilePicture, Byte sex, Date birthday, String tel,
                        String email, String remark, Byte status, Long createdBy, Date createdTime, Long updatedBy,
                        Date updatedTime, Long version, List<Role> roles) {
        this.id = id;
        this.code = code;
        this.name = name;
        this.profilePicture = profilePicture;
        this.sex = sex;
        this.birthday = birthday;
        this.tel = tel;
        this.email = email;
        this.remark = remark;
        this.status = status;
        this.createdBy = createdBy;
        this.createdTime = createdTime;
        this.updatedBy = updatedBy;
        this.updatedTime = updatedTime;
        this.version = version;
        this.roles = roles;
    }
}

3.4 配置通用Mapper,與Mapper.xml實現基本的增刪改查

3.4.1 添加BaseDao使用通用Mapper接口

在這裏插入圖片描述
具體代碼如下(合併)

package com.bosssoft.hr.bes.exam.center.dao.base;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * 頂層的接口
 * @author LGX_TvT
 * @date 2019-11-25 19:25
 */
public interface BaseDao<T> extends Mapper<T>, MySqlMapper<T> {
}


package com.bosssoft.hr.bes.exam.center.dao.mapper;
import com.bosssoft.hr.bes.exam.center.dao.base.BaseDao;
import com.bosssoft.hr.bes.exam.center.pojo.entity.Resource;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author LGX_TvT
 * @date 2019-11-25 19:39
 */
@Mapper
public interface ResourceDao extends BaseDao<Resource> {
}

package com.bosssoft.hr.bes.exam.center.dao.mapper;
import com.bosssoft.hr.bes.exam.center.dao.base.BaseDao;
import com.bosssoft.hr.bes.exam.center.pojo.entity.Role;
import org.apache.ibatis.annotations.Mapper;

/**
 * 角色Dao
 * @author LGX_TvT
 * @date 2019-11-25 19:31
 */
@Mapper
public interface RoleDao extends BaseDao<Role> {
}

package com.bosssoft.hr.bes.exam.center.dao.mapper;
import com.bosssoft.hr.bes.exam.center.dao.base.BaseDao;
import com.bosssoft.hr.bes.exam.center.pojo.entity.RoleResource;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author LGX_TvT
 * @date 2019-11-25 19:39
 */
@Mapper
public interface RoleResourceDao extends BaseDao<RoleResource> {
}

package com.bosssoft.hr.bes.exam.center.dao.mapper;
import com.bosssoft.hr.bes.exam.center.dao.base.BaseDao;
import com.bosssoft.hr.bes.exam.center.pojo.dto.UserSelectDTO;
import com.bosssoft.hr.bes.exam.center.pojo.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * @author LGX_TvT
 * @date 2019-11-25 19:38
 */
@Mapper
public interface UserDao extends BaseDao<User> {

    /**
     * 查詢所有用戶
     * @return
     */
    public List<UserSelectDTO> selectAllUser();

}

package com.bosssoft.hr.bes.exam.center.dao.mapper;
import com.bosssoft.hr.bes.exam.center.dao.base.BaseDao;
import com.bosssoft.hr.bes.exam.center.pojo.entity.UserRole;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author LGX_TvT
 * @date 2019-11-25 19:38
 */
@Mapper
public interface UserRoleDao extends BaseDao<UserRole> {
}

3.4.2 添加Mapper.xml映射文件

在這裏插入圖片描述
UserDao.xml

<?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">
<mapper namespace="com.bosssoft.hr.bes.exam.center.dao.mapper.UserDao">
    <select id="selectAllUser" resultMap="userSelectDTO">select * from t_user</select>

    <select id="selectRoles"  resultType="Role">
        SELECT t_role.id, t_role.name, t_role.code, t_role.remark, t_role.status, t_role.created_by as createdBy, t_role.created_time as createdTime, t_role.updated_by as updatedBy,t_role.updated_time as updatedTime, t_role.version from t_role,t_user_role where t_role.id = t_user_role.role and t_user_role.user = #{userId,jdbcType = BIGINT}
    </select>

    <!--ResultMap-->
    <resultMap id="userSelectDTO" type="UserSelectDTO">
        <id column="id" jdbcType="BIGINT" property="user.id" />
        <result column="code" jdbcType="VARCHAR" property="user.code" />
        <result column="password" jdbcType="VARCHAR" property="user.password" />
        <result column="name" jdbcType="VARCHAR" property="user.name" />
        <result column="profile_picture" jdbcType="VARCHAR" property="user.profilePicture" />
        <result column="sex" jdbcType="TINYINT" property="user.sex" />
        <result column="birthday" jdbcType="DATE" property="user.birthday" />
        <result column="tel" jdbcType="VARCHAR" property="user.tel" />
        <result column="email" jdbcType="VARCHAR" property="user.email" />
        <result column="other" jdbcType="VARCHAR" property="user.other" />
        <result column="remark" jdbcType="VARCHAR" property="user.remark" />
        <result column="status" jdbcType="TINYINT" property="user.status" />
        <result column="created_by" jdbcType="BIGINT" property="user.createdBy" />
        <result column="created_time" jdbcType="DATE" property="user.createdTime" />
        <result column="updated_by" jdbcType="BIGINT" property="user.updatedBy" />
        <result column="updated_time" jdbcType="DATE" property="user.updatedTime" />
        <result column="version" jdbcType="BIGINT" property="user.version" />
        <!--關聯查詢-->
        <collection property="roles" ofType="Role" javaType="java.util.List" column="{userId=id}" select="selectRoles" />
    </resultMap>
</mapper>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--類型別名-->
    <typeAliases>
        <typeAlias alias="UserInsertDTO" type="com.bosssoft.hr.bes.exam.center.pojo.dto.UserInsertDTO"/>
        <typeAlias alias="UserSelectDTO" type="com.bosssoft.hr.bes.exam.center.pojo.dto.UserSelectDTO"/>
        <typeAlias alias="User" type="com.bosssoft.hr.bes.exam.center.pojo.entity.User" />
        <typeAlias alias="Role" type="com.bosssoft.hr.bes.exam.center.pojo.entity.Role" />
    </typeAliases>
</configuration>

3.4 添加Controller

3.4.1 添加UserController,使用Swagger2做接口文檔

在這裏插入圖片描述

package com.bosssoft.hr.bes.exam.center.controller;

import com.bosssoft.hr.bes.exam.center.pojo.dto.UserInsertDTO;
import com.bosssoft.hr.bes.exam.center.pojo.dto.UserSelectDTO;
import com.bosssoft.hr.bes.exam.center.pojo.entity.User;
import com.bosssoft.hr.bes.exam.center.pojo.vo.MessageVO;
import com.bosssoft.hr.bes.exam.center.pojo.vo.UserInsertVO;
import com.bosssoft.hr.bes.exam.center.pojo.vo.UserSelectVO;
import com.bosssoft.hr.bes.exam.center.service.IUserService;
import com.bosssoft.hr.bes.exam.center.util.SnowFlake;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 用戶控制層
 * @author LGX_TvT
 * @date 2019-11-26 11:32
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用戶管理")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value="添加用戶信息", notes="包括用戶信息與用戶角色")
    @PostMapping("/insert")
    public MessageVO insertUser(UserInsertVO userVO){
        try {
            //補充數據
            if(userService.insertUser(new UserInsertDTO(
                            new User(SnowFlake.nextId(), userVO.getCode(), userVO.getPassword(), userVO.getName(), null, userVO.getSex(), userVO.getBirthday(), userVO.getTel(),
                            userVO.getEmail(), null, null, userVO.getStatus(), userVO.getCreatedBy(), new Date(), null, null, 1L),
                            userVO.getRoles())))
            {
                return new MessageVO(200, "添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MessageVO(400, "添加失敗");
    }

    @ApiOperation(value="獲取所有用戶信息", notes="包括用戶信息與用戶角色")
    @GetMapping("/select/all")
    public MessageVO selectAllUser(){
        List<UserSelectDTO> users =  userService.selectAllUser();
        List<UserSelectVO> userList = new ArrayList<>();
        if(users != null) {
            for (UserSelectDTO u : users) {
                User user = u.getUser();
                userList.add(new UserSelectVO(user.getId(), user.getCode(), user.getName(), user.getProfilePicture(), user.getSex(),
                        user.getBirthday(), user.getTel(), user.getEmail(), user.getRemark(), user.getStatus(), user.getCreatedBy(),
                        user.getCreatedTime(), user.getUpdatedBy(), user.getUpdatedTime(), user.getVersion(), u.getRoles()));
            }
            return new MessageVO(200, userList);
        }
        return new MessageVO(200, userList);
    }
}

3.5 修改啓動類

設置包掃描路勁,與Mapper掃描路徑(使用TK包)

package com.bosssoft.hr.bes.exam.center;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication(scanBasePackages = {"com.bosssoft"})
@MapperScan(basePackages = {"com.bosssoft.hr.bes.exam.center.dao.mapper"})
public class BossBesExamControllerApplication {

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

    }

}

3.6 測試

接口正常
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章