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

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

一、创建聚合父工程

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

图片.png

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




    
    SpringBoot 多模块构建示例
    4.0.0
    spring-boot-integration
    pom

    
    com.hehe
    springboot-integration
    1.0.0.RELEASE

    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.7.RELEASE
        
    

    
    
        mm-web
        mm-service
        mm-repo
        mm-entity
    

    
    
        
            
                com.hehe
                mm-web
                0.0.1-SNAPSHOT
            
            
                com.hehe
                mm-service
                0.0.1-SNAPSHOT
            
            
                com.hehe
                mm-repo
                0.0.1-SNAPSHOT
            
            
                com.hehe
                mm-entity
                0.0.1-SNAPSHOT
            
        
    


二、创建子模块(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 需要改造成这样:


    4.0.0

    
    com.hehe
    mm-web
    0.0.1-SNAPSHOT
    jar
    mm-web

    
    
        com.hehe
        springboot-integration
        1.0.0.RELEASE
    

    
    
        
            com.hehe
            mm-service
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

三、编写子模块代码

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 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{
}

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

图片.png

R.java 作为统一返回的Bean对象

package com.hehe.springbootintegration;

import java.io.Serializable;

public class R 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))