IntelliJ IDEA 搭建 Spring Boot Maven 多模块项目 亲测

本文详细介绍如何使用IntelliJ IDEA搭建Spring Boot Maven多模块项目,包括父项目配置、子模块创建步骤、环境配置文件设置及核心代码模块的集成。

IntelliJ IDEA 搭建 Spring Boot Maven 多模块项目

1. 创建父项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.1 父项目创建完需手动删除 src 目录

在这里插入图片描述

1.2 父级项目 multiple-project 配置 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mutiple</groupId>
  <artifactId>mutiple-project</artifactId>
  <version>1.0-SNAPSHOT</version>

  <packaging>pom</packaging>

  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring-boot.version>1.5.19.RELEASE</spring-boot.version>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.19.RELEASE</version>
  </parent>

  <dependencyManagement>
    <dependencies>
      <!--Spring IO 控制依赖版本适配-->
      <dependency>
        <groupId>io.spring.platform</groupId>
        <artifactId>platform-bom</artifactId>
        <version>Brussels-SR16</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--Spring Boot 控制依赖版本适配-->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
        <version>1.9.22</version>
      </dependency>
      <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity3</artifactId>
        <version>3.0.4.RELEASE</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>${spring-boot.version}</version>
          <executions>
            <execution>
              <goals>
                <goal>repackage</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

2. 创建子级 Web 模块, 一般用于业务入口

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.1 删除模块内 webapp 目录

在这里插入图片描述

2.2 java, resources 如没有需要手动生成,之后需手动设置为 IntelliJ IDEA 认的之源目录

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. 子级模块 multiple-web

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mutiple-project</artifactId>
        <groupId>com.mutiple</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mutiple</groupId>
    <artifactId>mutiple-web</artifactId>
    <packaging>jar</packaging>

    <name>mutiple-web Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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

3.2 resources 目录内创建环境文件 application.properties

#HTML Templete
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:public/
spring.thymeleaf.content-type=text/html
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8

3.3 resources 目录内创建目录 public 再生成 test.html 文件 (用于测试)

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<div>
    <h4>Hello World!!</h4>
</div>
</body>
</html>

3.4 创建 Controller

package com.mutiple;

import com.mutiple.service.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class TestController {

    @RequestMapping("/test")
    public String test(Model model) {
        return "test";
    }

}

3.5 创建启动文件

package com.mutiple;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"com.mutiple"})
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

3.6 启动项目

在这里插入图片描述
在这里插入图片描述

4. 创建子级 核心模块, 一般主要逻辑都写到此模块 如:Service,mapper,DAO等

在这里插入图片描述
在这里插入图片描述

4.1 模块内创建工具类 (用于测试)

package com.mutiple.service;

public class DateUtil {
    public static long getUnixTime10() {
        return System.currentTimeMillis() / 1000L;
    }

}

4.2 配置 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mutiple-project</artifactId>
        <groupId>com.mutiple</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mutiple</groupId>
    <artifactId>mutiple-service</artifactId>

    <name>mutiple-service</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>mutiple-service</finalName>
    </build>
 </project>

5. 业务入口模块导入 核心代码模块

5.1 在 multiple-web 的 pom.xml 写入以下

在这里插入图片描述

        <dependency>
            <groupId>com.mutiple</groupId>
            <artifactId>mutiple-service</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

5.2 在 multiple-web 的 Controller 写入以下

在这里插入图片描述

    @RequestMapping("/test")
    public String test(Model model) {

        model.addAttribute("time", DateUtil.getUnixTime10());

        return "test";
    }

5.3 在 multiple-web 的 test.html 写入以下

在这里插入图片描述

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<div>
    <h4>Hello World!!</h4>
    <h6>时间戳: <strong th:text="${time}"></strong></h6>
</div>
</body>
</html>

5.4 最后重启

在这里插入图片描述

如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值