[java] [Refactoring 1] Practical combat of module development and design

Table of Contents

1. Create a project

1. First create an empty project

2. Set up project SDK, etc.

2. Create the parent module and select springboot

1. Create the parent module parent

2. Delete redundant files and keep only pom.xml

3. Modify pom.xml

4. Add some public dependencies to pom

3. Create entity class sub-module entity

1. Create entity class sub-module entity

2. Delete irrelevant files

3. Modify pom

4. Copy the entity class file entity to the directory (create it if it doesn’t exist)

4. Create tool sub-module utils

1. Create tool submodule utils

2. Delete redundant files and keep only pom.xml

3. Modify pom.xml

4. Copy the entity class file utils to the directory (create it if it doesn’t exist)

5. Create the web project sub-module web-management

1. Create the web project sub-module web-management

2. Modify pom.xml

3. Copy the web project files

4. Create new resources

5. Create a new directory of resources com/bocai/mapper

6. Copy relevant files

7. Copy the startup program

6. Start the service

1. Start the backend AppApplication

2. Start ngnix

3. Verify data


Foreword: Following “[JavaWeb] [Fourteen] Web Backend Development-MAVEN Advanced”, start designing Java projects in modules from scratch

1. Create project

1. Create an empty project first

2. Set up project SDK, etc.

Select 1.8 for SDK and 8 for Language Level.

2. Create the parent module and select springboot

1. Create parent module parent

2. Delete redundant files and keep only pom.xml

3. Modify 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 1. Modified to 2.7.5 -->
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
\t
<groupId>com.bocai</groupId>
<artifactId>bocai-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 2. Newly set the packaging mode to pom -->
<packaging>pom</packaging>
\t
\t
<!-- 3. Comment name and description -->
<!--<name>bocai-parent</name>
<description>bocai-parent</description>-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
\t\t
\t\t
<!-- 4. Comment the following dependencies -->
<!--
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

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



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

</project>

4. Add some public dependencies to pom

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <!-- 1. Modified to 2.7.5 -->
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.bocai</groupId>
    <artifactId>bocai-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 2. Newly set the packaging mode to pom -->
    <packaging>pom</packaging>


    <!-- 3. Comment name and description -->
    <!--<name>bocai-parent</name>
    <description>bocai-parent</description>-->
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!-- 6. Added public dependencies -->
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- 4. Comment the following dependencies -->
        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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



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

</project>

3. Create entity class sub-module entity

1. Create entity class sub-module entity

2. Delete irrelevant files

3. Modify pom

<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.bocai</groupId>
        <artifactId>bocai-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../bocai-parent/pom.xml</relativePath>
    </parent>

    <artifactId>bocai-entity</artifactId>
    <!-- 1. Comment packaging method (unsure) -->
    <!--
    <packaging>jar</packaging> -->

    <name>bocai-entity</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <!-- 2. Comment junit -->
        <!--
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        -->
    </dependencies>
</project>

4. Copy the entity class file entity to the directory (create it if it does not exist)

Four. Create tool sub-module utils

1. Create tool sub-module utils

2. Delete redundant files and keep only pom.xml

slightly

3. Modify pom.xml

<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.bocai</groupId>
        <artifactId>bocai-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../bocai-parent/pom.xml</relativePath>
    </parent>

    <artifactId>bocai-utils</artifactId>
    <!-- 1. Comment packaging method (unsure) -->
    <!--
    <packaging>jar</packaging> -->

    <name>bocai-utils</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <!-- 2. Comment junit -->
        <!--
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        -->

        <!-- 3. Add dependencies -->
        <!-- web starting dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Alibaba Cloud OSS dependencies -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.15.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- no more than 2.3.3-->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.3</version>
        </dependency>



        <!--JWT token-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
    </dependencies>
</project>

4. Copy the entity class file utils to the directory (create it if it does not exist)

5. Create web project sub-module web-management

1. Create web project sub-module web-management

2. Modify pom.xml

The core is the sub-module entity and utils introduced in this article

<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.bocai</groupId>
        <artifactId>bocai-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../bocai-parent/pom.xml</relativePath>
    </parent>

    <artifactId>bocai-web-management</artifactId>
    <!-- 1. Comment packaging method (unsure) -->
    <!--
    <packaging>jar</packaging> -->

    <name>bocai-web-management</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- 2. Comment junit -->
        <!--
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        -->

        <!-- 3. Add submodule dependencies -->
        <dependency>
            <groupId>com.bocai</groupId>
            <artifactId>bocai-entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.bocai</groupId>
            <artifactId>bocai-utils</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- 4. Add dependencies -->

        <!-- web startup dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- mybatis starting dependency -->

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!-- mysql driver -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- springboot unit test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- PageHelper paging plug-in https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.6</version>
        </dependency>


        <!--fastJSON-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>

        <!--AOP-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

    </dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3. Copy web project files

Some files may report errors, mainly reflected in web startup dependencies. Please process each file separately.

4. New resources

5. Create a new directory for resources com/bocai/mapper

7. Copy startup program

package com.bocai;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;


@Slf4j
@ServletComponentScan //Enables support for servlet components Filter
@SpringBootApplication
public class AppApplication {

    public static void main(String[] args) {

        SpringApplication.run(AppApplication.class, args);
        log.info("============ Service startup completed!=====================");
    }


}

AppApplicationTests annotates a test code and reports an error when packaging.

package com.bocai;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class AppApplicationTests {

    @Test
    void contextLoads() {
    }

    /**
     * Generate Jwt token
     */
    @Test
    public void testGenJwt(){
        Map<String, Object> claims = new HashMap<>();
        claims.put("id",1);
        claims.put("name","bocai");
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, "bocai") // Signature algorithm. What are the algorithms? Go to the official website
                .setClaims(claims) //Customized content (load)
                .setExpiration(new Date(System.currentTimeMillis() + 3600* 1000)) //Set the validity period to 1h
                .compact();
        System.out.println(jwt);

    }

    /**
     * Parse Jwt, parseClaimsJws (replace with the jwt generated above)
     */
// @Test
// public void testPareJwt(){
// Claims claims = Jwts.parser()
// .setSigningKey("bocai") //runa must be consistent with the previous generation
// .parseClaimsJws("eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiYm9jYWkiLCJpZCI6MSwiZXhwIjoxNjk3NTM5MTQzfQ.kh_8USqITQHQvnOpWuF8Z7BB0x_BVhfZvOgF1JMcj8o")
// .getBody();
// System.out.println(claims);
//
// }

}

6. Start the service

1. Start the backend AppApplication

2. Start ngnix

3. Verification data

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 137861 people are learning the system