IDEA builds springboot multi-module project

First of all, let me talk about why we need to build multi-module projects. In fact, many projects are single-structure applications at the beginning. The common layers (web layer, service layer, dao layer) can be directly created by building different package names. , but as the business develops, the number of project participants increases and the business becomes more complex. All codes are under one structure, which becomes unintuitive, and the degree of coupling may be relatively high. Another problem is that in a multi-service scenario, it is necessary to provide interfaces for external services (for example, to provide external dubbo interfaces). If it is a single structure, the entire module can only be exported as a jar, which is not elegant, otherwise it has to be It is troublesome to re-do multi-module splitting. There is another problem. Some common classes may exist in several projects. The multi-module structure can put common classes together and package them for other services. Therefore, for medium and large-scale projects in the foreseeable future, it is best to build multiple modules directly at the beginning, and for small projects, a single structure is enough.

Here is a simple example to build a multi-module project in idea:

Let me talk about the structure of the example first:

app-info
└ app-info-commom
└ app-info-model
└ app-info-dao
└ app-info-service
└ app-info-web

Dependencies of each module:

app-info-commom

app-info-model

app-info-dao

app-info-service

app-info-web

Create a new project, packaging select jar

In the next step, do not select any dependencies here, because this is the outermost parent module

For the completed project, only the part with the red line is kept, and other files are deleted

This step starts to create a new sub-module, first build the bottom app-info-commom, select maven

Fill in groupId and artifactId

in pom.xml under app-info-commom should be the information of the parent module

↑↑↑↑↑↑ app-info-model reference app-info-commom operation ↑↑↑↑↑↑

Create a new app-info-dao below, because here you need to import packages related to mysql and mybatis, so choose spring initializr

After it is built, only the red part of the file is kept, and the others can be deleted

↑↑↑↑↑↑ app-info-service, app-info-web reference app-info-dao operation ↑↑↑↑↑↑

After the 5 sub-modules are built, the files that need to be processed:

1. Delete the initialization startup class under src in app-info-service and app-info-dao, and only keep the startup class of app-info-web

2. Only keep the resources under src in the app-info-dao and app-info-web modules, and delete other modules

3. According to the dependencies of each module, relevant dependencies need to be introduced in the pom.xml of the corresponding sub-module (the complete pom.xml will be posted below)

4. If the app-info-dao and app-info-service under construction introduce the dependency of redis or mysql, you need to add the relevant configuration of mysql or redis to the application.properties of app-info-web, otherwise the startup will not succeed

final project structure

Paste the pom.xml of each module below

pom.xml under app-info

<?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.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.app.info</groupId>
    <artifactId>app-info</artifactId>
    <version>${app.info.version}</version>
    <name>app-info</name>

    <modules>
        <module>app-info-commom</module>
        <module>app-info-model</module>
        <module>app-info-dao</module>
        <module>app-info-service</module>
        <module>app-info-web</module>
    </modules>

    <properties>
        <app.info.version>1.0.0</app.info.version>
    </properties>

    <dependencies>

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

    </dependencies>

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

</project>

pom.xml under app-info-commom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>app-info-commom</artifactId>
    <version>${app.info.version}</version>


</project>

pom.xml under app-info-model

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>app-info-model</artifactId>
    <version>${app.info.version}</version>

    <dependencies>

        <dependency>
            <groupId>com.app.info</groupId>
            <artifactId>app-info-commom</artifactId>
            <version>${app.info.version}</version>
        </dependency>

    </dependencies>

</project>

pom.xml under app-info-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>
    <parent>
        <artifactId>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>

    <artifactId>app-info-dao</artifactId>
    <version>${app.info.version}</version>

    <dependencies>
        <dependency>
            <groupId>com.app.info</groupId>
            <artifactId>app-info-model</artifactId>
            <version>${app.info.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

</project>

pom.xml under app-info-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>
    <parent>
        <artifactId>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>

    <artifactId>app-info-service</artifactId>
    <version>${app.info.version}</version>

    <dependencies>

        <dependency>
            <groupId>com.app.info</groupId>
            <artifactId>app-info-dao</artifactId>
            <version>${app.info.version}</version>
        </dependency>

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

    </dependencies>


</project>

pom.xml under app-info-web

<?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>
        <artifactId>app-info</artifactId>
        <groupId>com.app.info</groupId>
        <version>1.0.0</version>
    </parent>

    <artifactId>app-info-web</artifactId>
    <version>${app.info.version}</version>

    <dependencies>
        <dependency>
            <groupId>com.app.info</groupId>
            <artifactId>app-info-service</artifactId>
            <version>${app.info.version}</version>
        </dependency>

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

    </dependencies>

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

</project>

application.properties under app-info-web

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=xxxxxx
spring.datasource.username=xxxxxxx
spring.datasource.password=xxxxxx
# Specify as HikariDataSource
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
# Connection pool name
spring.datasource.hikari.pool-name=HikariCP
# Minimum number of idle connections
spring.datasource.hikari.minimum-idle=5
# The maximum number of connections in the connection pool
spring.datasource.hikari.maximum-pool-size=20
# The maximum time for an idle connection to survive, the default is 10 minutes
spring.datasource.hikari.idle-timeout=600000
# Database connection timeout
spring.datasource.hikari.connection-timeout=60000

spring.redis.database=xxxxxx
spring.redis.host=xxxxx
spring.redis.port=xxxx
spring.redis.password=xxxxxx
# Maximum number of connections
spring.redis.jedis.pool.max-active=200
# max idle
spring.redis.jedis.pool.max-idle=100
# minimum idle
spring.redis.jedis.pool.min-idle=100
# Maximum blocking waiting time (negative number means no limit)
spring.redis.jedis.pool.max-wait=-1
# Connection timeout
spring.redis.timeout=2000

start service

If the error is reported when starting: [java: error: invalid source release: 17], the high probability is that when building the project, springboot selects a version above 3.0.0, and 3.0.0 requires jdk17 support. You can downgrade the springboot version to 2.7.10 or 2.7.6