springboot构建maven多模块工程

时间:2022-02-11 16:13:51

一 .创建父工程:

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

springboot构建maven多模块工程

  1. 然后在 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>springboot-multi-module</name>
	<packaging>pom</packaging>

	<!-- 项目说明:这里作为聚合工程的父工程 -->
	<groupId>com.cicoding</groupId>
	<artifactId>springboot-multi-module</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>cicoding-web</module>
		<module>cicoding-service</module>
		<module>cicoding-repo</module>
		<module>cicoding-entity</module>
	</modules>

	<!-- 版本说明:这里统一管理依赖的版本号 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>com.cicoding</groupId>
				<artifactId>cicoding-web</artifactId>
				<version>0.0.1-SNAPSHOT</version>
			</dependency>
			<dependency>
				<groupId>com.cicoding</groupId>
				<artifactId>cicoding-service</artifactId>
				<version>0.0.1-SNAPSHOT</version>
			</dependency>
			<dependency>
				<groupId>com.cicoding</groupId>
				<artifactId>cicoding-repo</artifactId>
				<version>0.0.1-SNAPSHOT</version>
			</dependency>
			<dependency>
				<groupId>com.cicoding</groupId>
				<artifactId>cicoding-entity</artifactId>
				<version>0.0.1-SNAPSHOT</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

</project>

二、创建子模块(module)

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

1.对着父工程右键 - New - Module - > 输入 cicoding-web
2.对着父工程右键 - New - Module - > 输入 cicoding-service
3.对着父工程右键 - New - Module - > 输入 cicoding-repo
4.对着父工程右键 - New - Module - > 输入 cicoding-entity
1~4 步骤完成后,分别调整它们的pom.xml 以继承上面的父工程。
例如cicoding-web模块的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.cicoding</groupId>
	<artifactId>cicoding-web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>cicoding-web</name>

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

	<!-- Web模块相关依赖 -->
	<dependencies>
		<dependency>
			<groupId>com.cicoding</groupId>
			<artifactId>cicoding-service</artifactId>
		</dependency>
		<dependency>
			<groupId>com.cicoding</groupId>
			<artifactId>cicoding-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>

</project>

三、编写子模块代码

1. 控制层(cicoding-web)

springboot构建maven多模块工程

启动类 :MmWebApplication.java (cicoding-web)
package com.cicoding.integration;

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

/**
 *  注:为了避免扫描路径不一致,请将启动类放在Root Package 即 com.cicoding.integration
 */

@SpringBootApplication
public class MmWebApplication {

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

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

package com.cicoding.integration.user;

import com.cicoding.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;

@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 (cicoding-web)
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

2. 业务层(cicoding-service)

springboot构建maven多模块工程


实现类:UserServiceImpl.java (cicoding-service)
package com.cicoding.integration.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    UserRepository userRepository;

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

3. 数据层(cicoding-repo)

springboot构建maven多模块工程
数据层代码:UserRepository.java (cicoding-repo)

 
 
package com.cicoding.integration.user;

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

public interface UserRepository extends JpaRepository<User,String> {
}

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

springboot构建maven多模块工程
 
 R.java 作为统一返回的Bean对象 
 
package com.cicoding.integration.common;

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.cicoding.integration.user;

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

@Entity
@Table(name = "T_USER")
public class User {

    @Id
    @Column(name = "USERID")
    private String userId;
    @Column(name = "USERNAME")
    private String username;
    @Column(name = "PASSWORD")
    private String password;

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

三、运行项目

运行主类:
springboot构建maven多模块工程
到此就结束了!
参考文献

https://www.jianshu.com/p/59ceea4f029d

https://spring.io/guides/gs/multi-module/