本文介紹了Maven 搭建spring boot多模塊項(xiàng)目,分享給大家,具體如下:
備注:所有項(xiàng)目都在idea中創(chuàng)建
1.idea創(chuàng)建maven項(xiàng)目
- 1-1: 刪除src,target目錄,只保留pom.xml
- 1-2: 根目錄pom.xml可被子模塊繼承,因此項(xiàng)目只是demo,未考慮太多性能問題,所以將諸多依賴。都寫在根級(jí)`pom.xml`,子模塊只需繼承就可以使用。
- 1-3: 根級(jí)pom.xml文件在附錄1
- 1-4: 依賴模塊 mybatis spring-boot相關(guān)模塊
2.創(chuàng)建子模塊(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>此段必須加上,用來(lái)繼承父模塊
至此,項(xiàng)目的基礎(chǔ)結(jié)構(gòu)搭建完畢了,接下來(lái)可以來(lái)擼代碼了,哦哦稍等,我先介紹下各個(gè)子module的工作職責(zé)吧
4.子模塊在項(xiàng)目中擔(dān)任的'工作職責(zé)'
- model 此模塊存放著所有的實(shí)體類
- dao 此模塊存放著數(shù)據(jù)交互的具體實(shí)現(xiàn),供service調(diào)用
- service 此模塊存放業(yè)務(wù)代碼實(shí)現(xiàn),供API層調(diào)用
- webapi 此模塊也可以不出現(xiàn)在項(xiàng)目中,為了寫demo故將webapi層放進(jìn)來(lái)
5.model層實(shí)體類編寫
- 建立包名 com.luyh.projectv1.model
- 建實(shí)體類 Member.java 具體代碼請(qǐng)clone我的git,git地址在最下方
6.dao層數(shù)據(jù)庫(kù)操作層
- 建立com.luyh.projectv1.dao.config,該包內(nèi)只有2個(gè)讓spring boot自動(dòng)加載配置的配置java類
- 建立MemberMapper.java 具體內(nèi)容看代碼
- 在resources/mybatis 下建立MemberMapper.xml
- 建立IMember.java
- 建立Member.java 實(shí)現(xiàn)Imember接口
- 建立resources/application.properties文件用于配置數(shù)據(jù)庫(kù)連接
7. service 編寫業(yè)務(wù)邏輯
- 建立 com.luyh.projectv1.service 包
- 建立IMemberService.java接口
- 建立MemberService.java實(shí)現(xiàn)類
- MemberService.java 類中自動(dòng)注入DaoMember 并調(diào)用其方法獲取數(shù)據(jù)
8. webapi 編寫webapi獲取json數(shù)據(jù)
- 建立Application.java 啟動(dòng)應(yīng)用
- 建立 com.luyh.projectv1.webapi.controller.MemberController.java 寫個(gè)rest風(fēng)格Controller
- 啟動(dòng)
9.sql文件 請(qǐng)自行導(dǎo)入mysql數(shù)據(jù) 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 > <!--申明依賴關(guān)系--> < 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 > <!--設(shè)置maven倉(cāng)庫(kù)--> < 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 > |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000005020589