Grain College - project construction

Posted by kb9yjg on Sat, 12 Feb 2022 00:08:57 +0100

1, Project module introduction and database design

1. Engineering structure

2. Module introduction

  • Guli parent: online teaching root directory (parent project), managing four sub modules
    • Canal client: canal database table synchronization module (statistical synchronization data)
    • Common: parent node of common module
      • Common util: tool class module. All modules can depend on it
      • Service base: the base package of the service, which contains the public configuration class of the service. All service modules depend on it
      • Spring security: authentication and authorization module. The service that needs authentication and authorization depends on it
    • infrastructure: parent node of basic service module
      • api gateway: api gateway service
    • Service: api interface service parent node
      • Service ACL: User Authority Management api interface service (user management, role management, authority management, etc.)
      • Service CMS: cms api interface service
      • Service edu: teaching related api connection service
      • Service MSM: short message api interface service
      • Service order: order related api interface service
      • Service OSS: alicloud oss api interface service
      • Service statistics: statistical report api interface service
      • Service UCenter: member api interface service
      • Service VOD: VOD api interface service

3. Create instructor database

guli_edu.sql

2, Create parent project

1. Create sprigboot project Guli parent

In the idea development tool, use Spring Initializr to quickly initialize a Spring Boot module. Version: 2.2.1 RELEASE

to configure:

groupId: com.rg

artifactId: guli-parent

2. Delete src directory

3. Configure POM xml

  • Revised version: 2.2.1 Release and modify the packaging method to POM

  • Add < Properties > to determine the dependent version
<properties>
    <java.version>1.8</java.version>
    <guli.version>0.0.1-SNAPSHOT</guli.version>
    <mybatis-plus.version>3.0.5</mybatis-plus.version>
    <velocity.version>2.0</velocity.version>
    <swagger.version>2.7.0</swagger.version>
    <aliyun.oss.version>2.8.3</aliyun.oss.version>
    <jodatime.version>2.10.1</jodatime.version>
    <poi.version>3.17</poi.version>
    <commons-fileupload.version>1.3.1</commons-fileupload.version>
    <commons-io.version>2.6</commons-io.version>
    <httpclient.version>4.5.1</httpclient.version>
    <jwt.version>0.7.0</jwt.version>
    <aliyun-java-sdk-core.version>4.3.3</aliyun-java-sdk-core.version>
    <aliyun-sdk-oss.version>3.1.0</aliyun-sdk-oss.version>
    <aliyun-java-sdk-vod.version>2.15.2</aliyun-java-sdk-vod.version>
    <aliyun-java-vod-upload.version>1.4.11</aliyun-java-vod-upload.version>
    <aliyun-sdk-vod-upload.version>1.4.11</aliyun-sdk-vod-upload.version>
    <fastjson.version>1.2.28</fastjson.version>
    <gson.version>2.8.2</gson.version>
    <json.version>20170516</json.version>
    <commons-dbutils.version>1.7</commons-dbutils.version>
    <canal.client.version>1.1.0</canal.client.version>
    <docker.image.prefix>zx</docker.image.prefix>
    <cloud-alibaba.version>0.2.2.RELEASE</cloud-alibaba.version>
    <mysql.version>5.1.49</mysql.version>
</properties>
  • Add project dependency
<dependencyManagement>
    <dependencies>
        <!--Spring Cloud-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--mybatis-plus Persistent layer-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>

        <!-- velocity template engine, Mybatis Plus Code generator needs -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>${velocity.version}</version>
        </dependency>

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

        <!--aliyunOSS-->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>${aliyun.oss.version}</version>
        </dependency>

        <!--Date time tool-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${jodatime.version}</version>
        </dependency>

        <!--xls-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <!--xlsx-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>

        <!--File upload-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${commons-fileupload.version}</version>
        </dependency>

        <!--commons-io-->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>

        <!--httpclient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient.version}</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson.version}</version>
        </dependency>

        <!-- JWT -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jwt.version}</version>
        </dependency>

        <!--aliyun-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>${aliyun-java-sdk-core.version}</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>${aliyun-sdk-oss.version}</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-vod</artifactId>
            <version>${aliyun-java-sdk-vod.version}</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-vod-upload</artifactId>
            <version>${aliyun-java-vod-upload.version}</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-sdk-vod-upload</artifactId>
            <version>${aliyun-sdk-vod-upload.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>${json.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>${commons-dbutils.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.otter</groupId>
            <artifactId>canal.client</artifactId>
            <version>${canal.client.version}</version>
        </dependency>
        <!--use mysql5.5-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

3, Build service module

1. Create a module service under the parent project Guli parent

2. Add dependencies required by the project

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>

    <!--hystrix Dependence, mainly with  @HystrixCommand -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>

    <!--Service registration-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--Service call-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>

    <!--mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- velocity template engine, Mybatis Plus Code generator needs -->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
    </dependency>

    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
    </dependency>

    <!--lombok Used to simplify entity classes: need to install lombok plug-in unit-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <!--xls-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
    </dependency>

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
    </dependency>

    <!--httpclient-->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>
    <!--commons-io-->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
    </dependency>
    <!--gson-->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

4, Build service edu module

1. Create a sub module service edu under the parent project service module

2. Create application YML profile

# Service port
server:
  port: 8001

#Service name
spring:
  application:
    name: service-edu

#Environment settings: dev,test,prod
  profiles:
    active: dev

# mysql database connection
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///guli_edu
    username: root
    password: 186259

# mybatis log
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3. Write the content of entity, service and mapper code

  • Create the package com. In the test/java directory rg. Eduservice, create code generator: codegenerator java
public class CodeGenerator {

    @Test
    public void run() {

        // 1. Create code generator
        AutoGenerator mpg = new AutoGenerator();

        // 2. Global configuration
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//Gets the absolute path of the project

        gc.setOutputDir("F:\\Code\\Workspace_JavaEE\\Projects\\guli-parent\\service\\service-edu" + "/src/main/java");
        gc.setAuthor("lxy");
        gc.setOpen(false); //Whether to open the Explorer after generation or not
        gc.setFileOverride(false); //Whether the file is overwritten during regeneration
        gc.setServiceName("%sService");	//Remove the initial I of the Service interface
        gc.setIdType(IdType.ID_WORKER_STR); //The primary key policy determines the primary key policy according to the primary key type of the database. If it is long, it is ID_WORKER, ID if string_ WORKER_ STR
        gc.setDateType(DateType.ONLY_DATE);//Defines the date type in the generated entity class
        gc.setSwagger2(true);//Turn on Swagger2 mode

        mpg.setGlobalConfig(gc);

        // 3. Data source configuration
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/guli_edu");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("186259");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4. Package configuration
        PackageConfig pc = new PackageConfig();
        //com.rg.eduservice
        pc.setModuleName("eduservice"); //Module name
        pc.setParent("com.rg");

        //Subpackages in eduservice
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5. Policy configuration
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("edu_teacher");//Reverse generate code according to the structure of tables in the database
        strategy.setNaming(NamingStrategy.underline_to_camel);//Named policy mapping entities to database tables
        strategy.setTablePrefix(pc.getModuleName() + "_"); //Remove the table prefix when generating entities

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//Naming policy for mapping database table fields to entities
        strategy.setEntityLombokModel(true); // lombok model @ accessories (chain = true) setter chain operation

        strategy.setRestControllerStyle(true); //restful api style controller
        strategy.setControllerMappingHyphenStyle(true); //Hump to hyphen in url

        mpg.setStrategy(strategy);


        // 6. Execute
        mpg.execute();
    }
}
  • After execution, generate renderings:

5, Database design specification

The following regulations are only for this module. For more comprehensive documents, please refer to Alibaba Java Development Manual: v. MySQL database

1. The library name should be consistent with the application name as much as possible

2. Table names and field names must use lowercase letters or numbers, and the beginning of numbers is prohibited.

3. Plural nouns are not used in table names

4. It is better to add "business name table function" to the name of the table. For example, edu_teacher

5. Three required fields in the table: id, gmt_create,gmt_modified
explain:
The id must be the primary key, the type is bigint unsigned, and the step size is 1.
(if the cluster deployment by database and table is used, the id type is verchar, which is not self increment, and the distributed id generator is used in the business)
gmt_create,gmt_ The type of modified is datetime. The former current tense indicates active creation, and the latter past participle indicates passive update.

6. Only when the number of rows in a single table exceeds 5 million or the capacity of a single table exceeds 2GB, it is recommended to divide the database and table. Note: if the estimated data volume in three years can not reach this level at all, please do not divide databases and tables when creating tables.

7. The field expressing the concept of yes or no must use is_xxx. The data type is unsigned tinyint (1 means yes, 0 means no).
Note: if any field is non negative, it must be unsigned.
Note: any boolean variable in POJO class should not be prefixed with is. The database indicates the value of yes or no, using tinyint type and adhering to is_ The naming method of XXX is to clarify its value meaning and value range.
Positive example: field name is expressing logical deletion_ Deleted, 1 means deleted, 0 means not deleted.

8. The decimal type is decimal, and float and double are prohibited. Note: during the storage of float and double, there is a problem of accuracy loss. It is likely that incorrect results will be obtained during the comparison of values. If the stored data range exceeds the decimal range, it is recommended to split the data into integers and decimals and store them separately.

9. If the stored strings are almost equal in length, use the char fixed length string type.

10. varchar is A variable length string. It does not pre allocate storage space, and the length should not exceed 5000. If the storage length is greater than this value, define the field type as text, and separate A table to correspond with the primary key, so as to avoid the impact of scene A on the index efficiency of other fields.

11. The unique index name is uk_ Field name; The normal index name is idx_ Field name.
Description: uk_ unique key; idx_ The abbreviation of index

12. Foreign keys and cascading are not allowed. All foreign key concepts must be solved at the application layer. Foreign keys and cascaded updates are suitable for single machine low concurrency, not for distributed and high concurrency clusters; Cascade update is A strong blocking, and there is A risk of database update storm; The insertion speed of foreign key scene A into the database.
nyint type, insist is_ The naming method of XXX is to clarify its value meaning and value range.
Positive example: field name is expressing logical deletion_ Deleted, 1 means deleted, 0 means not deleted.

8. The decimal type is decimal, and float and double are prohibited. Note: during the storage of float and double, there is a problem of accuracy loss. It is likely that incorrect results will be obtained during the comparison of values. If the stored data range exceeds the decimal range, it is recommended to split the data into integers and decimals and store them separately.

9. If the stored strings are almost equal in length, use the char fixed length string type.

10. varchar is A variable length string. It does not pre allocate storage space, and the length should not exceed 5000. If the storage length is greater than this value, define the field type as text, and separate A table to correspond with the primary key, so as to avoid the impact of scene A on the index efficiency of other fields.

11. The unique index name is uk_ Field name; The normal index name is idx_ Field name.
Description: uk_ unique key; idx_ The abbreviation of index

12. Foreign keys and cascading are not allowed. All foreign key concepts must be solved at the application layer. Foreign keys and cascaded updates are suitable for single machine low concurrency, not for distributed and high concurrency clusters; Cascade update is A strong blocking, and there is A risk of database update storm; The insertion speed of foreign key scene A into the database.

Topics: JavaEE Spring Boot Microservices