雖然maven已經(jīng)提供了maven-archetype-webapp炭臭、maven-archetype-quickstart等項(xiàng)目骨架幫助我們快速構(gòu)建項(xiàng)目架構(gòu),但是默認(rèn)提供的archetype初始化的項(xiàng)目架構(gòu)并不能滿足開發(fā)需求敌呈,這時(shí)候就有必要自己寫一個(gè)滿足項(xiàng)目需求的archetype了
使用自定義archrtype生成的項(xiàng)目結(jié)構(gòu)圖
為了激發(fā)閱讀興趣贸宏,先放一張使用自定義archetype生成項(xiàng)目的項(xiàng)目結(jié)構(gòu)圖
基本上的類都是
archetype
生成的
archetype是什么
可以簡(jiǎn)單的理解為模板工具類,通過archetype
我們可以快速的生成項(xiàng)目的基本架構(gòu)驱富。比如我們使用idea
創(chuàng)建一個(gè)maven web
項(xiàng)目時(shí)锚赤,常常會(huì)選擇maven-archetype-webapp
模板來初始化項(xiàng)目,使用maven-archetype-webapp
生成的項(xiàng)目中包括webapp
目錄褐鸥,里面包含web
的配置文件
archetype的組成
要想寫一個(gè)自定義archetype
线脚,首先得知道一個(gè)archetype的組成。archetype由四部分組成:
-
prototype files
原型文件
位于src/main/resources/archetype-resource
目錄下叫榕。prototype files
原型文件可以理解為多模塊中的子模塊或是單模塊工程中的源文件[即src文件]浑侥。這些原型文件在使用對(duì)應(yīng)archetype
生成項(xiàng)目時(shí)被生成 -
archetype-metadata.xml
位于src/main/resources/META-INF/maven/
目錄下。該配置文件中主要列出了原型文件以及使用archetype
生成模板工程需要的參數(shù) -
prototype pom
位于src/main/resources/archetype-resources
目錄下晰绎。這個(gè)pom
文件會(huì)出現(xiàn)在archetype
創(chuàng)建的模板工程中寓落,如果是單模塊工程,則是對(duì)整個(gè)項(xiàng)目的依賴管理荞下;如果是多模塊工程伶选,該pom
是總pom
文件,該文件中會(huì)定義項(xiàng)目的子模塊以及對(duì)子模塊的依賴進(jìn)行管理等尖昏,子模塊pom
定義在子模塊下仰税,子模塊pom文件只管理子模塊的依賴。 -
archetype pom
位于自定義archetype
工程的根目錄下抽诉。這是archetype
工程項(xiàng)目的pom
文件陨簇,里面一般沒什么東西,不會(huì)出現(xiàn)在archetype
創(chuàng)建的模板工程中
superman[自定義archetype]結(jié)構(gòu)說明
-
superman
項(xiàng)目結(jié)構(gòu)圖
包含了archetype
的四個(gè)組成部分迹淌,兩個(gè)pom
文件河绽,一個(gè)archtype-metadata
文件和五個(gè)原型文件[__rootArtifactId__-*
]己单,其中__rootArtifactId__
在生成模板工程時(shí)會(huì)被傳入的值替代 - archtype-metadata配置文件
- 1.定義使用
archetype
生成模板工程需要傳入的參數(shù)
<!--需要輸入的屬性--> <requiredProperties> <requiredProperty key="groupId"> <!--默認(rèn)的groupId--> <defaultValue>com.h2t.test</defaultValue> </requiredProperty> <requiredProperty key="artifactId"> <!--默認(rèn)的artifactId--> <defaultValue>demo</defaultValue> </requiredProperty> <requiredProperty key="package"> <!--默認(rèn)的包名和groupId一樣--> <defaultValue>${groupId}</defaultValue> </requiredProperty> </requiredProperties>
${}
標(biāo)識(shí)的變量都是通過maven中的命令行傳進(jìn)來的- 2.定義原型文件
<module id="${rootArtifactId}-web" name="${rootArtifactId}-web" dir="__rootArtifactId__-web"> <fileSets> <fileSet filtered="true" encoding="UTF-8" packaged="true"> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet filtered="true" encoding="UTF-8" packaged="true"> <directory>src/test/java</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet encoding="UTF-8"> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </fileSet> <fileSet encoding="UTF-8"> <directory>src/test/resources</directory> <includes> <include>**/*.*</include> </includes> </fileSet> </fileSets> </module>
module
屬性介紹:
id
:子模塊工程的artifactId
dir
:子模塊工程源文件在archetype-resources
里對(duì)應(yīng)的directory
name
:子模塊的名字. - 1.定義使用
- prototype pom文件
- 1.定義了五個(gè)子模塊
<!--項(xiàng)目子模塊--> <modules> <module>${rootArtifactId}-common</module> <module>${rootArtifactId}-dao</module> <module>${rootArtifactId}-service</module> <module>${rootArtifactId}-web</module> <module>${rootArtifactId}-model</module> </modules>
- 子模塊依賴版本統(tǒng)一管理
子模塊所需依賴都定義在該<dependencyManagement> <!--modules--> <dependency> <groupId>${groupId}</groupId> <artifactId>${rootArtifactId}-common</artifactId> <version>${version}</version> </dependency> <dependency> <groupId>${groupId}</groupId> <artifactId>${rootArtifactId}-dao</artifactId> <version>${version}</version> </dependency> <dependency> <groupId>${groupId}</groupId> <artifactId>${rootArtifactId}-service</artifactId> <version>${version}</version> </dependency> <dependency> <groupId>${groupId}</groupId> <artifactId>${rootArtifactId}-model</artifactId> <version>${version}</version> </dependency> </dependencies> </dependencyManagement>
pom
中,子模塊使用依賴時(shí)不需要<version>
標(biāo)簽 - 原型文件以web模塊說明
就是一個(gè)簡(jiǎn)單的maven工程耙饰,里面寫了使用archetype生成模板項(xiàng)目的類
快速開始【superman archetype使用指南】
- 1.下載源碼
git clone https://github.com/TiantianUpup/superman.git
- 2.打開superman工程纹笼,將其安裝到本地倉(cāng)庫(kù)
運(yùn)行如下命令
mvn clean install
- 3.使用自定義archetype初始化項(xiàng)目
mvn archetype:generate
-DgroupId=com.h2t.test
-DartifactId=superman-demo
-Dversion=1.0.0-SNAPSHOT
-DarchetypeGroupId=com.h2t.study
-DarchetypeArtifactId=superman -DarchetypeVersion=0.0.1-SNAPSHOT -X -DarchetypeCatalog=local
參數(shù)說明
-DgroupId
組ID,默認(rèn)項(xiàng)目的包名的組ID相同
DartifactId
:項(xiàng)目唯一標(biāo)識(shí)符苟跪,即項(xiàng)目名稱
-DarchetypeGroupId
:superman的組ID允乐,值不需要進(jìn)行修改
-DarchetypeArtifactId
:superman的artifactId,值不需要進(jìn)行改變
4.移動(dòng)配置文件
因?yàn)槭褂?code>archetype生成項(xiàng)目時(shí)會(huì)將resource
下面的文件丟失削咆,所以目前將配置文件放在了web
模塊下的resource
包下,創(chuàng)建項(xiàng)目成功后需手動(dòng)將文件移動(dòng)到web
模塊下的resource
文件夾下蠢笋,并將resource
文件成標(biāo)記成Resources Root
-
5.修改resource文件夾下的配置文件
該文件夾下有application.properties
拨齐,logback.properties
,logback-spring.xml
三個(gè)配置文件-
application.properties
配置文件的修改
application.properties
主要是Spring
昨寞、MyBatisPlus
和數(shù)據(jù)庫(kù)的配置信息
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?characterEncoding=UTF8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=your password
修改數(shù)據(jù)庫(kù)瞻惋、密碼,默認(rèn)用戶名為
root
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml # mybatis-plus.type-aliases-package=
指定
MybatisPlus
實(shí)體類別名的包援岩,即model
模塊的po
層包名歼狼,默認(rèn)MybatiPlus
的mapper
文件保存在resource
下的mapper
文件夾下,可自行修改-
logback.properties
配置文件的修改
logback.properties
定義了error
級(jí)別日志和info
級(jí)別日志的保存地址
LOG_ERROR_HOME= LOG_INFO_HOME=
-
logback-spring.xml
配置文件的修改
logback-spring.xml
主要是日志輸出規(guī)則的定義享怀,若為windows
系統(tǒng)無需進(jìn)行修改羽峰,若為linux os
或mac os
,則需修改日志保存地址
<fileNamePattern>${LOG_ERROR_HOME}//%d.log</fileNamePattern>
將
//
修改為/
-
-
6 使用代碼生成器生成
controller
添瓷、service
梅屉、dao
、po
層代碼
代碼生成器類位于service
模塊下的generator
包下鳞贷,只需要初始化幾個(gè)字段值運(yùn)行就可以生成相應(yīng)的代碼坯汤。在運(yùn)行前首先在項(xiàng)目根目錄下創(chuàng)建一個(gè)mp-generator-output
文件夾,該文件夾的名字和OUTPUT_DIR
字段值保持一致-
PACKAGE_NAME
生成代碼的包名搀愧,和項(xiàng)目的包名一致惰聂,負(fù)責(zé)復(fù)制過去代碼會(huì)有一些小問題
-OUTPUT_DIR
生成代碼保存文件地址,默認(rèn)保存在項(xiàng)目下的mp-generator-output
文件夾下咱筛,可以修改為自定義保存地址 -
AUTHOR
注釋中作者的名字 -
DRIVER_NAME
數(shù)據(jù)庫(kù)驅(qū)動(dòng) -
HOST
數(shù)據(jù)庫(kù)主機(jī)號(hào) -
PORT
數(shù)據(jù)庫(kù)端口 -
DATABASE
數(shù)據(jù)庫(kù)名字 -
USERNAME
數(shù)據(jù)庫(kù)用戶名 -
PASSWORD
數(shù)據(jù)庫(kù)密碼
-
-
7.將生成的代碼移動(dòng)到對(duì)應(yīng)模塊對(duì)應(yīng)包下
controller
文件夾
實(shí)體類對(duì)應(yīng)的Controller
搓幌,將該目錄下的類移到web
模塊下的controller
包下mapper
文件夾
實(shí)體類對(duì)應(yīng)的DAO
層,該目錄下包含xml
文件和對(duì)應(yīng)實(shí)體的接口類眷蚓,將xml文
件移到dao
模塊resource
下的mapper
文件夾下鼻种,需自行建立mapper
文件夾,將接口移到dao
模塊下的mapper
包下并在接口類上添加@Mapper
注解沙热,需自行建立mapper
包叉钥。同時(shí)將resource文件夾標(biāo)記成Resources root
-
service
對(duì)應(yīng)實(shí)體類接口-
impl
對(duì)應(yīng)實(shí)體類接口實(shí)現(xiàn)類
將
service
目錄下的接口移到service
模塊下的service
包下罢缸,impl
目錄下的類移到service
模塊下的service.impl
包下 -
po文件夾
將該目錄下的類移到model
模塊下的po
包下,并修改繼承關(guān)系投队,統(tǒng)一繼承BasePO
類枫疆,因?yàn)?code>BasePO類 包含了id
、gmtCreate
敷鸦、gmtModified
息楔、deleted
這些數(shù)據(jù)庫(kù)基本字段,需將生成的實(shí)體類手動(dòng)刪除這些重復(fù)字段扒披。同時(shí)自動(dòng)生成的po
類缺失了@TableName
值依、@TableField
注解需手動(dòng)補(bǔ)充。注解的使用方式可參考BasePO
類
-
8.修改
web
模塊aspect
包下的環(huán)繞通知@Around("execution(* yourpackage.controller..*(..))")
該切面主要用于攔截controller層返回的結(jié)果碟案,將其封裝成統(tǒng)一結(jié)果返回
-
9 啟動(dòng)項(xiàng)目
web
模塊下的Runner
類為啟動(dòng)類愿险,運(yùn)行該類即可啟動(dòng),默認(rèn)端口為8081
附:superman archetype生成demo工程地址歡迎fork與star[劃重點(diǎn)]价说,
由于開發(fā)經(jīng)驗(yàn)有限辆亏,有些地方可能考慮不周,歡迎提bug鳖目。并且該
archetype
只定義了一些基礎(chǔ)功能扮叨,歡迎提需求。