雖然maven已經提供了maven-archetype-webapp、maven-archetype-quickstart等項目骨架幫助咱們快速構建項目架構,可是默認提供的archetype初始化的項目架構並不能知足開發需求,這時候就有必要本身寫一個知足項目需求的archetype了java
爲了激發閱讀興趣,先放一張使用自定義archetype生成項目的項目結構圖 mysql
archetype
生成的,爲了讓大家學習真是用盡辦法
能夠簡單的理解爲模板工具類,經過archetype
咱們能夠快速的生成項目的基本架構。好比咱們使用idea
建立一個maven web
項目時,經常會選擇maven-archetype-webapp
模板來初始化項目,使用maven-archetype-webapp
生成的項目中包括webapp
目錄,裏面包含web
的配置文件 linux
要想寫一個自定義archetype
,首先得知道一個archetype的組成。archetype由四部分組成:git
prototype files
原型文件src/main/resources/archetype-resource
目錄下。prototype files
原型文件能夠理解爲多模塊中的子模塊或是單模塊工程中的源文件[即src文件]。這些原型文件在使用對應archetype
生成項目時被生成archetype-metadata.xml
src/main/resources/META-INF/maven/
目錄下。該配置文件中主要列出了原型文件以及使用archetype
生成模板工程須要的參數prototype pom
src/main/resources/archetype-resources
目錄下。這個pom
文件會出如今archetype
建立的模板工程中,若是是單模塊工程,則是對整個項目的依賴管理;若是是多模塊工程,該pom
是總pom
文件,該文件中會定義項目的子模塊以及對子模塊的依賴進行管理等,子模塊pom
定義在子模塊下,子模塊pom文件只管理子模塊的依賴。archetype pom
archetype
工程的根目錄下。這是archetype
工程項目的pom
文件,裏面通常沒什麼東西,不會出如今archetype
建立的模板工程中superman
項目結構圖
archetype
的四個組成部分,兩個pom
文件,一個archtype-metadata
文件和五個原型文件[__rootArtifactId__-*
],其中__rootArtifactId__
在生成模板工程時會被傳入的值替代archetype
生成模板工程須要傳入的參數<!--須要輸入的屬性-->
<requiredProperties>
<requiredProperty key="groupId">
<!--默認的groupId-->
<defaultValue>com.h2t.test</defaultValue>
</requiredProperty>
<requiredProperty key="artifactId">
<!--默認的artifactId-->
<defaultValue>demo</defaultValue>
</requiredProperty>
<requiredProperty key="package">
<!--默認的包名和groupId同樣-->
<defaultValue>${groupId}</defaultValue>
</requiredProperty>
</requiredProperties>
複製代碼
${}
標識的變量都是經過maven中的命令行傳進來的<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
裏對應的directory
name
:子模塊的名字.<!--項目子模塊-->
<modules>
<module>${rootArtifactId}-common</module>
<module>${rootArtifactId}-dao</module>
<module>${rootArtifactId}-service</module>
<module>${rootArtifactId}-web</module>
<module>${rootArtifactId}-model</module>
</modules>
複製代碼
<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
中,子模塊使用依賴時不須要<version>
標籤附:superman archetype代碼github
1.下載源碼web
git clone https://github.com/TiantianUpup/superman.git
複製代碼
2.打開superman工程,將其安裝到本地倉庫
運行命令前先切換到20190815-v2update分支,master分支被我玩壞了【真丟人!!!】
運行以下命令spring
mvn clean install
複製代碼
3.使用自定義archetype初始化項目sql
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
複製代碼
參數說明
-DgroupId
組ID,默認項目的包名的組ID相同
DartifactId
:項目惟一標識符,即項目名稱
-DarchetypeGroupId
:superman的組ID,值不須要進行修改
-DarchetypeArtifactId
:superman的artifactId,值不須要進行改變數據庫
4.修改resources文件夾下的配置文件,而且將resources文件標記成Resources Rootwindows
該文件夾下有application.properties
,logback.properties
,logback-spring.xml
三個配置文件
application.properties
配置文件的修改 application.properties
主要是Spring
、MyBatisPlus
和數據庫的配置信息
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?characterEncoding=UTF8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=your password
複製代碼
修改數據庫、密碼,默認用戶名爲root
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml
# mybatis-plus.type-aliases-package=
複製代碼
指定MybatisPlus
實體類別名的包,即model
模塊的po
層包名,默認MybatiPlus
的mapper
文件保存在resource
下的mapper
文件夾下,可自行修改
logback.properties
配置文件的修改 logback.properties
定義了error
級別日誌和info
級別日誌的保存地址
LOG_ERROR_HOME=
LOG_INFO_HOME=
複製代碼
logback-spring.xml
配置文件的修改 logback-spring.xml
主要是日誌輸出規則的定義,若爲windows
系統無需進行修改,若爲linux os
或mac os
,則需修改日誌保存地址
<fileNamePattern>${LOG_ERROR_HOME}//%d.log</fileNamePattern>
複製代碼
將//
修改成/
5 使用代碼生成器生成controller
、service
、dao
、po
層代碼 代碼生成器類位於service
模塊下的generator
包下,只須要初始化幾個字段值運行就能夠生成相應的代碼。在運行前首先在項目根目錄下建立一個mp-generator-output
文件夾,該文件夾的名字和OUTPUT_DIR
字段值保持一致
PACKAGE_NAME
OUTPUT_DIR
生成代碼保存文件地址,默認保存在項目下的mp-generator-output
文件夾下,能夠修改成自定義保存地址AUTHOR
DRIVER_NAME
HOST
PORT
DATABASE
USERNAME
PASSWORD
6.將生成的代碼移動到對應模塊對應包下
controller
文件夾
實體類對應的Controller
,將該目錄下的類移到web
模塊下的controller
包下
mapper
文件夾 實體類對應的DAO
層,該目錄下包含xml
文件和對應實體的接口類,將xml文
件移到dao
模塊resource
下的mapper
文件夾下,需自行創建mapper
文件夾,將接口移到dao
模塊下的mapper
包下並在接口類上添加@Mapper
註解,需自行創建 mapper
包。同時將resource文件夾標記成Resources root
service
對應實體類接口
impl
對應實體類接口實現類將service
目錄下的接口移到service
模塊下的service
包下,impl
目錄下的類移到service
模塊下的service.impl
包下
po文件夾 將該目錄下的類移到model
模塊下的po
包下,並修改繼承關係,統一繼承BasePO
類,由於BasePO
類 包含了id
、gmtCreate
、gmtModified
、deleted
這些數據庫基本字段,需將生成的實體類手動刪除這些重複字段。同時自動生成的po
類缺失了@TableName
、@TableField
註解需手動補充。註解的使用方式可參考BasePO
類
7.修改web
模塊aspect
包下的環繞通知
@Around("execution(* yourpackage.controller..*(..))")
複製代碼
該切面主要用於攔截controller層返回的結果,將其封裝成統一結果返回
8 啓動項目
web
模塊下的Runner
類爲啓動類,運行該類便可啓動,默認端口爲8081
附:superman archetype生成demo工程地址
歡迎fork與star[劃重點],因爲開發經驗有限,有些地方可能考慮不周,歡迎提bug。而且該archetype
只定義了一些基礎功能,歡迎提需求。