美文网首页springboot学习Spring 框架
SpringBoot多模块项目实践(Multi-Module)

SpringBoot多模块项目实践(Multi-Module)

作者: rainbowz | 来源:发表于2019-02-02 13:39 被阅读34次

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

一、创建聚合父工程

1.首先使用 Spring Initializr 来快速创建好一个Maven工程。然后删除无关的文件,只需保留pom.xml 文件。

图片.png

然后在 pom.xml 里面声明该父工程包含的子模块。(其它信息就不逐一讲述了,诸如继承SpringBoot官方父工程以及统一依赖管理 请查看下面的注释说明

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

    <!-- 基本信息 -->
    <description>SpringBoot 多模块构建示例</description>
    <modelVersion>4.0.0</modelVersion>
    <name>spring-boot-integration</name>
    <packaging>pom</packaging>

    <!-- 项目说明:这里作为聚合工程的父工程 -->
    <groupId>com.hehe</groupId>
    <artifactId>springboot-integration</artifactId>
    <version>1.0.0.RELEASE</version>

    <!-- 继承说明:这里继承SpringBoot提供的父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath/>
    </parent>

    <!-- 模块说明:这里声明多个子模块 -->
    <modules>
        <module>mm-web</module>
        <module>mm-service</module>
        <module>mm-repo</module>
        <module>mm-entity</module>
    </modules>

    <!-- 版本说明:这里统一管理依赖的版本号 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.hehe</groupId>
                <artifactId>mm-web</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.hehe</groupId>
                <artifactId>mm-service</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.hehe</groupId>
                <artifactId>mm-repo</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.hehe</groupId>
                <artifactId>mm-entity</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </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 以继承上面的父工程。
    例如mm-web模块的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>

    <!-- 基本信息 -->
    <groupId>com.hehe</groupId>
    <artifactId>mm-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>mm-web</name>

    <!-- 继承本项目的父工程 -->
    <parent>
        <groupId>com.hehe</groupId>
        <artifactId>springboot-integration</artifactId>
        <version>1.0.0.RELEASE</version>
    </parent>

    <!-- Web模块相关依赖 -->
    <dependencies>
        <dependency>
            <groupId>com.hehe</groupId>
            <artifactId>mm-service</artifactId>
        </dependency>
        <!--<dependency>
            <groupId>com.hehe</groupId>
            <artifactId>mm-entity</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <!--该插件主要用途:构建可执行的JAR -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

三、编写子模块代码

1. 控制层(mm-web)

图片.png
启动类 :SpringbootIntegrationApplication.java (mm-web)
package com.hehe.springbootintegration;

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

@SpringBootApplication
public class SpringbootIntegrationApplication {

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

}


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

package com.hehe.springbootintegration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("city")
@RestController
public class CityController {

    @Autowired
    CityService service;

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

    }

    @GetMapping("cityList/{id}")
    public  R city(@PathVariable("id")Long id){
        try
        {
            return  R.isOk().data(service.findCity(id));
        }catch (Exception e){
            return R.isFail();
        }
    }
}

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

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

2. 业务层(mm-service)

实现类:UserServiceImpl.java (mm-service)

package com.hehe.springbootintegration;

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

import java.util.List;
@Service
public class CityServiceImpl implements  CityService {

    @Autowired
    CityRepo repo;

    public List<City> list() {
        return repo.findAll();
    }

    public  City findCity(Long id){

        return  repo.findOne(id);
    }
}

3. 数据层(mm-repo)

图片.png
数据层代码:CityRepo.java(mm-repo)
package com.hehe.springbootintegration;



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

public interface CityRepo extends JpaRepository<City,Long>{
}

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

图片.png
R.java 作为统一返回的Bean对象
package com.hehe.springbootintegration;

import java.io.Serializable;

public class R<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 R isOk() {
        return new R();
    }

    public static R isFail() {
        return new R().status(FAIL);
    }

    public static R isFail(Throwable e) {
        return isFail().msg(e);
    }

    public R msg(Throwable e) {
        this.setMsg(e.toString());
        return this;
    }

    public R data(T data) {
        this.setData(data);
        return this;
    }

    public R status(int status) {
        this.setStatus(status);
        return this;
    }


    //Constructors
    public R() {

    }

   //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.springbootintegration;

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

@Entity
@Table
public class City {
    @Id
    @Column(name = "id")
    private  long id;

    @Column(name = "province_id")
    private  String provinceId;

    @Column(name = "city_name")
    private  String cityName;

    @Column(name = "description")
    private  String description;

    public City(long id, String provinceId, String cityName, String description) {
        this.id = id;
        this.provinceId = provinceId;
        this.cityName = cityName;
        this.description = description;
    }

    public City() {
    }

    public long getId() {
        return id;
    }

    @Override
    public String toString() {
        return "City{" +
                "id=" + id +
                ", provinceId='" + provinceId + '\'' +
                ", cityName='" + cityName + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(String provinceId) {
        this.provinceId = provinceId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

三、运行项目

图片.png
CREATE TABLE city
(
  id          INT AUTO_INCREMENT
    PRIMARY KEY,
  province_id INT          NULL,
  city_name   VARCHAR(255) NULL,
  description VARCHAR(255) NULL
)
  ENGINE = InnoDB;


配置好整个项目之后,这里只需要运行mm-web模块下的Application的启动类就可以了,如正常启动后,访问http://localhost:8080 可查询到用户列表信息。如下图:

http://localhost:8080/city/list http://localhost:8080/city/cityList/1

参考:
https://segmentfault.com/a/1190000011367492
https://github.com/yizhiwazi/springboot-socks/tree/master/springboot-integration

相关文章

网友评论

    本文标题:SpringBoot多模块项目实践(Multi-Module)

    本文链接:https://www.haomeiwen.com/subject/vkessqtx.html