1. 程式人生 > >springboot基於maven多模塊項目搭建(直接啟動webApplication)

springboot基於maven多模塊項目搭建(直接啟動webApplication)

3.2 pen left sco 定義 project localhost pack view

1. 新建maven項目springboot-module

技術分享圖片

2.把src刪掉,新建module項目

  • springboot-module-api

  • springboot-module-model

  • springboot-module-service

  • springboot-module-util

  • springboot-module-web

技術分享圖片

3. 添加模塊之間的依賴

3.1 springboot-module.pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.springboot.module</groupId> 8 <artifactId>
springboot-module</artifactId> 9 <packaging>pom</packaging> 10 <version>1.0-SNAPSHOT</version> 11 <modules> 12 <module>springboot-module-api</module> 13 <module>springboot-module-model</module> 14 <module>
springboot-module-service</module> 15 <module>springboot-module-util</module> 16 <module>springboot-module-web</module> 17 </modules> 18 19 <!-- Spring boot 父引用--> 20 <parent> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-parent</artifactId> 23 <version>1.4.1.RELEASE</version> 24 </parent> 25 <dependencies> 26 <!-- Spring boot 核心web--> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-web</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.projectlombok</groupId> 33 <artifactId>lombok</artifactId> 34 <version>1.16.18</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter-logging</artifactId> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-test</artifactId> 43 </dependency> 44 </dependencies> 45 46 <build> 47 <plugins> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 <executions> 52 <execution> 53 <goals> 54 <goal>repackage</goal> 55 </goals> 56 </execution> 57 </executions> 58 <configuration> 59 <executable>true</executable> 60 </configuration> 61 </plugin> 62 </plugins> 63 </build> 64 </project>

3.2 springboot-module-api.pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>springboot-module</artifactId>
 7         <groupId>com.springboot.module</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11     <artifactId>springboot-module-api</artifactId>
12 
13     <dependencies>
14         <dependency>
15             <groupId>com.springboot.module</groupId>
16             <artifactId>springboot-module-util</artifactId>
17             <version>1.0-SNAPSHOT</version>
18             <scope>compile</scope>
19         </dependency>
20     </dependencies>
21 </project>

3.3 springboot-module-model.pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>springboot-module</artifactId>
 7         <groupId>com.springboot.module</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11     <artifactId>springboot-module-model</artifactId>
12 
13     <dependencies>
14         <dependency>
15             <groupId>org.springframework.boot</groupId>
16             <artifactId>spring-boot-starter-data-jpa</artifactId>
17         </dependency>
18         <dependency>
19             <groupId>mysql</groupId>
20             <artifactId>mysql-connector-java</artifactId>
21         </dependency>
22         <dependency>
23             <groupId>com.springboot.module</groupId>
24             <artifactId>springboot-module-util</artifactId>
25             <version>1.0-SNAPSHOT</version>
26             <scope>compile</scope>
27         </dependency>
28     </dependencies>
29 
30 </project>

3.4 springboot-module-service.pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>springboot-module</artifactId>
 7         <groupId>com.springboot.module</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11     <artifactId>springboot-module-service</artifactId>
12 
13     <dependencies>
14         <dependency>
15             <groupId>com.springboot.module</groupId>
16             <artifactId>springboot-module-model</artifactId>
17             <version>1.0-SNAPSHOT</version>
18         </dependency>
19         <dependency>
20             <groupId>com.springboot.module</groupId>
21             <artifactId>springboot-module-api</artifactId>
22             <version>1.0-SNAPSHOT</version>
23         </dependency>
24         <dependency>
25             <groupId>ma.glasnost.orika</groupId>
26             <artifactId>orika-core</artifactId>
27             <version>1.5.1</version>
28         </dependency>
29         <dependency>
30             <groupId>org.apache.commons</groupId>
31             <artifactId>commons-lang3</artifactId>
32             <version>3.5</version>
33         </dependency>
34     </dependencies>
35 </project>

3.5 springboot-module-util.pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>springboot-module</artifactId>
 7         <groupId>com.springboot.module</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11     <artifactId>springboot-module-util</artifactId>
12 
13     <dependencies>
14         <dependency>
15             <groupId>org.springframework.boot</groupId>
16             <artifactId>spring-boot-starter-data-jpa</artifactId>
17         </dependency>
18     </dependencies>
19 </project>

3.6 springboot-module-web.pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>springboot-module</artifactId>
 7         <groupId>com.springboot.module</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11     <artifactId>springboot-module-web</artifactId>
12 
13     <dependencies>
14         <dependency>
15             <groupId>com.springboot.module</groupId>
16             <artifactId>springboot-module-service</artifactId>
17             <version>1.0-SNAPSHOT</version>
18         </dependency>
19     </dependencies>
20 </project>

4. 模塊依賴關系圖

技術分享圖片

5. 層說明

springboot-module-api ——業務邏輯接口

springboot-module-model ——實體類

springboot-module-service ——數據訪問層,業務邏輯層的實現

springboot-module-util ——工具模塊

springboot-module-web ——表現層

6. 包名設計

包名推薦以groupId為開頭定義,所有模塊的包名結構統一規範,比如groupId是com.springboot.module,而所有模塊包名都以com.springboot.module開頭,其中web層的WebApplication需要放在com.springboot.module下,不能放在com.springboot.module.web或者com.springboot.module.controller下。推薦使用下面的第一種情況。

6.1. 包名以groupId開頭,註意WebApplication.java的位置,必須放在com.springboot.module包下,不能放在com.springboot.module.controller或者com.springboot.module.web包下

技術分享圖片技術分享圖片技術分享圖片技術分享圖片

6.2. 包名以groupId開頭,web層WebApplication.java放在了com.springboot.module.web下。

技術分享圖片技術分享圖片技術分享圖片

啟動WebApplication.java會報如下錯誤

技術分享圖片

6.2.1. 解決方法:

1) 第一步:在WebApplication中加入

1 @ComponentScan(basePackages = {"com.springboot.module"})不是@ComponentScan(basePackages = {"com.springboot.module.*"})

技術分享圖片

1) 第二步,在springboot-module-service模塊中添加ServiceApplication.java類,

技術分享圖片技術分享圖片

6.2.2. 包名不以groupId開頭,其他和第一種情況一樣也是可以的。但最好推薦是以groupId為開頭的

7.只有 web層有application.properties

 1 server.port=8086
 2 server.servlet-path=/
 3 spring.resources.static-locations=classpath:/static/,classpath:/templates/
 4 spring.mvc.view.suffix=.html
 5 
 6 #配置數據源
 7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 8 spring.datasource.url=jdbc:mysql://localhost:3306/springboot-module
 9 spring.datasource.username=root
10 spring.datasource.password=root
11 spring.jpa.hibernate.ddl-auto=update
12 spring.jpa.show-sql=true
13 
14 #在控制臺輸出彩色日誌
15 spring.output.ansi.enabled=always

springboot基於maven多模塊項目搭建(直接啟動webApplication)