SpringBoot2.x总结——多模块项目实践(附打包方法)

文章目录

    • 一、创建聚合父工程
    • 二、创建子模块(module)
    • 三、编写子模块代码
      • 1.控制层(mm-web)
      • 2.业务层(mm-service)
      • 3.数据持久层(mm-repo)
      • 4. 实体模型层 (mm-entity)
    • 三、运行项目
    • 四、运维部署(多模块打包)
      • 1.添加打包插件
      • 2.打包工程
      • 3.启动项目

序言:

比起传统复杂的单体工程,使用Maven的多模块配置,可以帮助项目划分模块,鼓励重用,防止POM变得过于庞大,方便某个模块的构建,而不用每次都构建整个项目,并且使得针对某个模块的特殊控制更为方便。

接下来,本文将重点阐述SpringBoot在Maven环境的多模块构建过程。

一、创建聚合父工程

1.首先使用 Spring Initializr 来快速创建好一个Maven工程。然后删除无关的文件,只需保留pom.xml 文件。
SpringBoot2.x总结——多模块项目实践(附打包方法)_第1张图片
在这里插入图片描述
2.然后在 pom.xml 里面声明该父工程包含的子模块。(其它信息就不逐一讲述了,诸如继承SpringBoot官方父工程以及统一依赖管理 请查看下面的注释说明)


<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">

    
    <description>SpringBoot 多模块构建示例description>
    <modelVersion>4.0.0modelVersion>
    <name>springboot-integrationname>
    <packaging>pompackaging>

    
    <groupId>com.hehegroupId>
    <artifactId>springboot-integrationartifactId>
    <version>1.0.0.RELEASEversion>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.7.RELEASEversion>
        <relativePath/>
    parent>

    
    <modules>
        <module>mm-webmodule>
        <module>mm-servicemodule>
        <module>mm-repomodule>
        <module>mm-entitymodule>
    modules>

    
    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>com.hehegroupId>
                <artifactId>mm-webartifactId>
                <version>0.0.1-SNAPSHOTversion>
            dependency>
            <dependency>
                <groupId>com.hehegroupId>
                <artifactId>mm-serviceartifactId>
                <version>0.0.1-SNAPSHOTversion>
            dependency>
            <dependency>
                <groupId>com.hehegroupId>
                <artifactId>mm-repoartifactId>
                <version>0.0.1-SNAPSHOTversion>
            dependency>
            <dependency>
                <groupId>com.hehegroupId>
                <artifactId>mm-entityartifactId>
                <version>0.0.1-SNAPSHOTversion>
            dependency>

            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.42version>
            dependency>
        dependencies>
    dependencyManagement>

project>

二、创建子模块(module)

注:这里是使用IDEA来创建子模块,使用Eclipse的小伙伴可通过 Spring Initializr 构建,然后复制去进去父工程根目录即可。

  1. 右键父工程 - New - Module - > 输入 mm-web
  2. 右键父工程 - New - Module - > 输入 mm-service
  3. 右键父工程 - New - Module - > 输入 mm-repo
  4. 右键父工程 - New - Module - > 输入 mm-entity

1~4 步骤完成后,分别对应调整它们的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    
    <groupId>com.hehegroupId>
    <artifactId>mm-webartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
    <name>mm-webname>

    
    <parent>
        <groupId>com.hehegroupId>
        <artifactId>springboot-integrationartifactId>
        <version>1.0.0.RELEASEversion>
    parent>

    
    <dependencies>
        <dependency>
            <groupId>com.hehegroupId>
            <artifactId>mm-serviceartifactId>
        dependency>
        <dependency>
            <groupId>com.hehegroupId>
            <artifactId>mm-entityartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

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

project>

<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.0modelVersion>
    
    <groupId>com.hehegroupId>
    <artifactId>mm-serviceartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
    <name>mm-servicename>

    
    <parent>
        <groupId>com.hehegroupId>
        <artifactId>springboot-integrationartifactId>
        <version>1.0.0.RELEASEversion>
    parent>

    
    <dependencies>
        <dependency>
            <groupId>com.hehegroupId>
            <artifactId>mm-entityartifactId>
        dependency>
        <dependency>
            <groupId>com.hehegroupId>
            <artifactId>mm-repoartifactId>
        dependency>
    dependencies>

project>

<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.0modelVersion>
    
    <groupId>com.hehegroupId>
    <artifactId>mm-repoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
    <name>mm-reponame>

    
    <parent>
        <groupId>com.hehegroupId>
        <artifactId>springboot-integrationartifactId>
        <version>1.0.0.RELEASEversion>
    parent>

    
    <dependencies>
        <dependency>
            <groupId>com.hehegroupId>
            <artifactId>mm-entityartifactId>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>

    dependencies>

project>

<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.0modelVersion>
    
    <groupId>com.hehegroupId>
    <artifactId>mm-entityartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
    <name>mm-entityname>

    
    <parent>
        <groupId>com.hehegroupId>
        <artifactId>springboot-integrationartifactId>
        <version>1.0.0.RELEASEversion>
    parent>

    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
    dependencies>


project>

三、编写子模块代码

1.控制层(mm-web)

SpringBoot2.x总结——多模块项目实践(附打包方法)_第2张图片
启动类 :MmWebApplication.java (mm-web)

package com.hehe.integration;

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

/**
 * @Author: haocheng
 * @Date: 2019-11-13 17:03
 */
@SpringBootApplication
public class MmWebApplication {

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

}

控制层:UserController.java (mm-web )

package com.hehe.integration.user;

import com.hehe.integration.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: haocheng
 * @Date: 2019-11-13 17:03
 */
@RestController
@RequestMapping("/user/*")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("list")
    public R list() {
        try {
            return R.isOk().data(userService.list());
        } catch (Exception e) {
            return R.isFail(e);
        }
    }
}

配置文件:application.yml (mm-web)

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/socks?useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

2.业务层(mm-service)

SpringBoot2.x总结——多模块项目实践(附打包方法)_第3张图片
接口:UserService(mm-service)

package com.hehe.integration.user;

import java.util.List;

/**
 * @Author: haocheng
 * @Date: 2019-11-13 17:07
 */
public interface UserService {
    List<User> list();
}

实现类:UserServiceImpl (mm-service)

package com.hehe.integration.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: haocheng
 * @Date: 2019-11-13 17:07
 */
@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    UserRepository userRepository;

    @Override
    public List<User> list() {
        return userRepository.findAll();
    }
}

3.数据持久层(mm-repo)

SpringBoot2.x总结——多模块项目实践(附打包方法)_第4张图片
数据层:UserRepository.java (mm-repo)

package com.hehe.integration.user;

import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @Author: haocheng
 * @Date: 2019-11-13 17:09
 */
public interface UserRepository extends JpaRepository<User,String> {
}

4. 实体模型层 (mm-entity)

SpringBoot2.x总结——多模块项目实践(附打包方法)_第5张图片
Result.java 封装统一返回的Bean对象

package com.hehe.integration.common;

import java.io.Serializable;

/**
 * @Author: haocheng
 * @Date: 2019-11-13 17:11
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -4577255781088498763L;
    private static final int OK = 0;
    private static final int FAIL = 1;
    private static final int UNAUTHORIZED = 2;

    private T data; //服务端数据
    private int status = OK; //状态码
    private String msg = ""; //描述信息

    //APIS
    public static Result isOk(){
        return new Result();
    }
    public static Result isFail(){
        return new Result().status(FAIL);
    }
    public static Result isFail(Throwable e){
        return isFail().msg(e);
    }
    public Result msg(Throwable e){
        this.setMsg(e.toString());
        return this;
    }
    public Result data(T data){
        this.setData(data);
        return this;
    }
    public Result status(int status){
        this.setStatus(status);
        return this;
    }


    //Constructors
    public Result() {

    }

    //Getter&Setters

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
package com.hehe.integration.user;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @Author: haocheng
 * @Date: 2019-11-13 17:12
 */
@Entity
@Table(name = "user")
public class User {

    @Id
    @Column(name = "user_id")
    private String userId;
    @Column(name = "user_name")
    private String userName;
    @Column(name = "password")
    private String password;

    //Getter&Setters

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

三、运行项目

只需要运行mm-web模块下的MmWebApplication的启动类就可以了,如正常启动后,访问http://localhost:8080 可查询到用户列表信息。
在这里插入图片描述

四、运维部署(多模块打包)

1.添加打包插件

注意:多模块项目仅仅需要在启动类所在的模块添加打包插件即可!!不要在父类添加打包插件,因为那样会导致全部子模块都使用spring-boot-maven-plugin的方式来打包(例如BOOT-INF/com/hehe/xx),而mm-web模块引入mm-xx 的jar 需要的是裸露的类文件,即目录格式为(/com/hehe/xx)。

本案例的启动模块是 mm-web , 只需在它的pom.xml 添加打包插件(spring-boot-maven-plugin):


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

2.打包工程

首先在IDE打开Maven插件视图,然后在聚合父工程spring-boot-integration中先点击 clean ,然后点击 package 进行打包。如图:
SpringBoot2.x总结——多模块项目实践(附打包方法)_第6张图片
打包成功效果如下:
SpringBoot2.x总结——多模块项目实践(附打包方法)_第7张图片
打包地址默认在Target目录:
SpringBoot2.x总结——多模块项目实践(附打包方法)_第8张图片

3.启动项目

cmd命令模式下,切换到jar包所在路径,执行命令 java -jar mm-web-0.0.1-SNAPSHOT.jar

启动效果如下:SpringBoot2.x总结——多模块项目实践(附打包方法)_第9张图片

参考:http://www.jianshu.com/p/59ceea4f029d

你可能感兴趣的:(Springboot)