springboot使用maven分模組搭建環境(idea)
阿新 • • 發佈:2018-11-12
一、搭建基本架構
建立聚合父工程
刪除多餘檔案,只留一個pom檔案
在父工程下建立子模組
對著父工程右鍵 - New - Module - > 輸入 web
對著父工程右鍵 - New - Module - > 輸入 service
..等
修改pom檔案,匯入依賴模組jar(不是springboot依賴)
repo依賴entity,service依賴dao,controller依賴service
修改各個模組的pom.xml檔案,例如repo層:(加入依賴的entity的座標)
其他模組類似(注意,依賴version一定要寫,否則報錯,與父工程version一致)
<?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"> <parent> <artifactId>vote</artifactId> <groupId>com.mlj</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.mlj.repo</groupId> <artifactId>repo</artifactId> <dependencies> <dependency> <groupId>com.mlj.entity</groupId> <artifactId>entity</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
clean install各個模組
注意修改web模組的打包方式為war
看是否報錯,沒有報錯才能執行下一步
二、加入jar包依賴(springboot依賴),配置
我是為了方便全放在父模組的pom裡邊了,繼承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"> <modelVersion>4.0.0</modelVersion> <groupId>com.mlj</groupId> <artifactId>vote</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>common</module> <module>entity</module> <module>service</module> <module>web</module> <module>repo</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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> <!--spring boot Configuration Annotation Proessor not found in classpath異常--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--aop--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.mlj.vote.controller.GirlController</mainClass> </configuration> </plugin> </plugins> </build> </project>
web模組下resource下建立 application.yml
spring:
#開發環境配置
# profiles:
# active: dev
#資料庫配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/dbgirl
username: root
password: root
#jpa配置
jpa:
hibernate:
ddl-auto: update
show-sql: true
建立啟動類application
package com.mlj.vote;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class VoteApplication {
public static void main(String[] args) {
SpringApplication.run(VoteApplication.class, args);
}
}
三、測試是否搭建成功
新建資料庫dbvote
實體類
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Min;
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
@Min(value = 18,message = "禁止未成年入內!")
private Integer age;
public Girl() {
}
// 必須有無參構造
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
repo
package com.mlj.vote.repo;
import com.mlj.vote.entity.Girl;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface GirlRepository extends JpaRepository<Girl,Integer> {
}
controller
package com.mlj.vote.controller;
import com.mlj.vote.entity.Girl;
import com.mlj.vote.repo.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class GirlController {
@Autowired
private GirlRepository girlRepository;
/**
* 獲取列表
*
* @return
*/
@GetMapping(value = "/girls")
public List<Girl> girlList() {
return girlRepository.findAll();
}
}
啟動執行:
四、問題與解決
Unable to find main class
Spring Boot Maven Plugin打包異常及三種解決方法:Unable to find main class
IDEA_maven依賴錯誤 包下面紅色波浪線
開始搭建環境時候先把springboot依賴寫上了,有些依賴匯入不進去,修改pom 配置檔案,講標紅的依賴先刪除,並點選reimport, 之後重新加上出錯的依賴,再reimport
推薦先把多模組跑起來再加入依賴和配置
Error:java:JDK isn't specified for module
忘記部署了。。web打包方式應該改為war,部署webwar包