目录

(一)创建一个父工程(一级)

(二)创建子工程(二级)

(三)创建子子工程(三级)

(四)效果展示

(一)创建一个父工程

首先创建一个父工程,选择Spring initalizr(idea社区版没有这个选项,反正我下载过几个版本社区版都没有),SDK选1.8的(根据个人需求选择),然后点next。

ideal如何打开多模块maven项目 idea maven多模块_java

ideal如何打开多模块maven项目 idea maven多模块_intellij-idea_02

 

ideal如何打开多模块maven项目 idea maven多模块_java_03

这边的依赖根据自己需求选择,这个只是初步选择,后续也可以直接在pom中添加,我这边就随便选了几个,主要是演示多模块如何搭建,最后点击next,父工程创建完成。

(二)搭建子工程

对于子工程,主要是与父工程的pom建立关系,父工程的src等目录用不到可以直接删掉。

ideal如何打开多模块maven项目 idea maven多模块_java_04

ideal如何打开多模块maven项目 idea maven多模块_ideal如何打开多模块maven项目_05

 修改父工程的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>
    <modules>
        <module>demo_01</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.mmm</groupId>
    <artifactId>demo</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <mybatis.version>2.2.2</mybatis.version>
        <mysql.versio>5.1.40</mysql.versio>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
<!--                <version>2.2.2</version>-->
                <version>${mybatis.version}</version>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

父工程的pom文件有几个地方需要注意一下。

1.需要添加<packaging>pom</packaging>

ideal如何打开多模块maven项目 idea maven多模块_spring_06

2.所有的依赖版本都放到<properties></properties>标签中,这样便于后续的版本管理

3.需要添加<dependencyManagement></dependencyManagement>标签,所有的依赖都放在这个里面,版本号的地方用${}表示,{}中填的就是<properties></properties>标签中的内容,例如:

ideal如何打开多模块maven项目 idea maven多模块_ideal如何打开多模块maven项目_07

 后续添加依赖以及版本号的时候就按照这个往里面添加即可。

(二)创建子工程

右键父工程demo,创建Module,这个时候选择maven项目,点击next

ideal如何打开多模块maven项目 idea maven多模块_maven_08

 

ideal如何打开多模块maven项目 idea maven多模块_ideal如何打开多模块maven项目_09

ideal如何打开多模块maven项目 idea maven多模块_java_10

 创建成功后,父工程的pom文件中会出现<module>demo_01</module>标签,没有则说明创建失败,也可以自己手动加一下 

ideal如何打开多模块maven项目 idea maven多模块_spring_11

 

ideal如何打开多模块maven项目 idea maven多模块_ideal如何打开多模块maven项目_12

 如果子工程中还想加入一个子子工程,pom文件加入<packaging>pom</packaging>

 

ideal如何打开多模块maven项目 idea maven多模块_spring_13

 如果子工程需要加入依赖,再手动加入即可,比如加入mybatis依赖,此时不需要加入版本号,继承父项目的版本号了。

<?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>demo</artifactId>
        <groupId>com.example.mmm</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo_01</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>demo_02</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>


</project>

删掉src及没用的目录,继续新建一个module模块,依然选择maven项目,这边要注意

ideal如何打开多模块maven项目 idea maven多模块_maven_14

ideal如何打开多模块maven项目 idea maven多模块_ideal如何打开多模块maven项目_15

 

创建结束,demo_01中会有module标签

ideal如何打开多模块maven项目 idea maven多模块_ideal如何打开多模块maven项目_16

 然后在demo_02里面添加application.properties文件或者yml文件,然后新建启动类,最终效果如下,启动成功。

application.properties文件如下

# 服务端口
server.port=8001

# 服务名
spring.application.name=service-edu
# 环境设置:dev、test、prod
spring.profiles.active=dev
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

启动类如下

package com.example.aaa;

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

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

ideal如何打开多模块maven项目 idea maven多模块_spring_17

 

 本次介绍到此结束,后续补充从一个子模块引用另一个子模块包。