本文介紹了Maven 搭建spring boot多模塊項目,分享給大家,具體如下:
備注:所有項目都在idea中創建
1.idea創建maven項目
- 1-1: 刪除src,target目錄,只保留pom.xml
- 1-2: 根目錄pom.xml可被子模塊繼承,因此項目只是demo,未考慮太多性能問題,所以將諸多依賴。都寫在根級`pom.xml`,子模塊只需繼承就可以使用。
- 1-3: 根級pom.xml文件在附錄1
- 1-4: 依賴模塊 mybatis spring-boot相關模塊
2.創建子模塊(module)
- 2-1: file > new > module 輸入 model
- 2-2: file > new > module 輸入 dao
- 2-3: file > new > module 輸入 service
- 2-4: file > new > module 輸入 webapi
3.修改子模塊pom.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? 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 >parent</ artifactId > < groupId >com.luyh.projectv1</ groupId > < version >1.0-SNAPSHOT</ version > < relativePath >../pom.xml</ relativePath > </ parent > < modelVersion >4.0.0</ modelVersion > < artifactId >projectv1-model</ artifactId > </ project > |
注意:<font color="red"><relativePath>../pom.xml</relativePath></font>此段必須加上,用來繼承父模塊
至此,項目的基礎結構搭建完畢了,接下來可以來擼代碼了,哦哦稍等,我先介紹下各個子module的工作職責吧
4.子模塊在項目中擔任的'工作職責'
- model 此模塊存放著所有的實體類
- dao 此模塊存放著數據交互的具體實現,供service調用
- service 此模塊存放業務代碼實現,供API層調用
- webapi 此模塊也可以不出現在項目中,為了寫demo故將webapi層放進來
5.model層實體類編寫
- 建立包名 com.luyh.projectv1.model
- 建實體類 Member.java 具體代碼請clone我的git,git地址在最下方
6.dao層數據庫操作層
- 建立com.luyh.projectv1.dao.config,該包內只有2個讓spring boot自動加載配置的配置java類
- 建立MemberMapper.java 具體內容看代碼
- 在resources/mybatis 下建立MemberMapper.xml
- 建立IMember.java
- 建立Member.java 實現Imember接口
- 建立resources/application.properties文件用于配置數據庫連接
7. service 編寫業務邏輯
- 建立 com.luyh.projectv1.service 包
- 建立IMemberService.java接口
- 建立MemberService.java實現類
- MemberService.java 類中自動注入DaoMember 并調用其方法獲取數據
8. webapi 編寫webapi獲取json數據
- 建立Application.java 啟動應用
- 建立 com.luyh.projectv1.webapi.controller.MemberController.java 寫個rest風格Controller
- 啟動
9.sql文件 請自行導入mysql數據 sql文件
這里是項目地址,點擊下載
附錄1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
<? 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.luyh.projectv1</ groupId > < artifactId >parent</ artifactId > < version >1.0-SNAPSHOT</ version > < packaging >pom</ packaging > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.3.3.RELEASE</ version > </ parent > < modules > < module >model</ module > < module >dao</ module > < module >service</ module > < module >webapi</ module > </ modules > <!--申明依賴關系--> < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-jdbc</ artifactId > </ dependency > < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis-spring</ artifactId > < version >1.2.2</ version > </ dependency > < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis</ artifactId > < version >3.2.8</ version > </ dependency > < dependency > < groupId >org.apache.tomcat</ groupId > < artifactId >tomcat-jdbc</ artifactId > </ dependency > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > </ dependency > </ dependencies > <!--設置maven倉庫--> < repositories > < repository > < id >spring-releases</ id > < url >https://repo.spring.io/libs-release</ url > </ repository > </ repositories > < pluginRepositories > < pluginRepository > < id >spring-releases</ id > < url >https://repo.spring.io/libs-release</ url > </ pluginRepository > </ pluginRepositories > </ project > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000005020589