springboot構建maven多模組工程
阿新 • • 發佈:2019-01-10
一 .建立父工程:
1.首先使用 Spring Initializr 來快速建立好一個Maven工程。然後刪除無關的檔案,只需保留pom.xml 檔案。
- 然後在 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)
啟動類 :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)
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)
資料層程式碼: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 (實體模型層)
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;
}
}
三、執行專案
執行主類:https://spring.io/guides/gs/multi-module/