為了在項目中快捷友善的代碼生成,将mybatis-plus-generator封裝為了一個maven的插件,在要使用的項目pom檔案引入該插件,執行mvn指令,即可直接生成代碼到項目中,生成基于
mybatis-plus-generator-maven-plugin
的mapper、service、controller三層結構,包括entity實體類和mapper.xml檔案,生成後直接能夠滿足基本的條件查詢和分頁查詢。下面介紹該插件的使用步驟:
mybatis-plus
一、下載下傳插件
方式1:CSDN中下載下傳:jar包位址:mybatis-plus-generator-maven-plugin-1.0.0.jar,pom檔案位址:pom.xml
方式2:将源代碼導入項目工程中,執行
mvn intall
,源碼位址
github:https://github.com/xiweile/mybatis-plus-generator-maven-plugin
gitee:https://gitee.com/xiweile/mybatis-plus-generator-maven-plugin
二、插件上傳至本地倉庫
在下載下傳好的mybatis-plus-generator-maven-plugin-1.0.0.jar和pom.xml檔案目錄下打開指令行工具,執行下面指令
-DpomFile
為pom.xml所在目錄,
-Dfile
是jar所在位置,
-Dpackaging
固定為 maven-plugin,其他參數此處不介紹。
mvn install:install-file -DpomFile=pom.xml -Dfile=mybatis-plus-generator-maven-plugin-1.0.0.jar -DgroupId=com.weiller -DartifactId=mybatis-plus-generator-maven-plugin -Dversion=1.0.0 -Dpackaging=maven-plugin
三、在pom中引入插件
在要使用插件的工程pom檔案中引入該插件,如下案例
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis-plus generator 自動生成代碼插件 -->
<plugin>
<groupId>com.weiller</groupId>
<artifactId>mybatis-plus-generator-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/mp-code-generator-config.yaml</configurationFile>
</configuration>
<dependencies>
<!-- 如果是oracle庫,需要額外引入jdbc驅動包(另一種方式:直接該oracle依賴在pom.xml中,此處就可以不需要引入),mysql則可忽略 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${ojdbc.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- mybatis-plus generator 自動生成代碼插件 -->
</plugins>
</build>
注意
configurationFile
參數為 下一步中配置檔案generator-config的位置,該檔案類型為
yaml
。
四、填寫配置檔案
配置完整案例
globalConfig:
author: weiller
open: false
idType: INPUT
dateType: ONLY_DATE
enableCache: false
activeRecord: false
baseResultMap: true
baseColumnList: true
swagger2: false
fileOverride: true
dataSourceConfig:
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&useSSL=false&characterEncoding=utf8
driverName: com.mysql.jdbc.Driver
username: root
password: xiweile
packageConfig:
parent: com.weiller
moduleName: rest
entity: model
service: service
serviceImpl: service.impl
mapper: dao
xml: mapper
controller: controller
pathInfo:
entity_path: src\main\java\com\weiller\rest\model
service_path: src\main\java\com\weiller\rest\service
service_impl_path: src\main\java\com\weiller\rest\service\impl
mapper_path: src\main\java\com\weiller\rest\dao
xml_path: src\main\resources\com\weiller\rest\mapper
controller_path: src\main\java\com\weiller\rest\controller
strategyConfig:
naming: underline_to_camel
columnNaming: underline_to_camel
entityLombokModel: true
superMapperClass: com.baomidou.mybatisplus.core.mapper.BaseMapper
superServiceClass: com.baomidou.mybatisplus.extension.service.IService
superServiceImplClass: com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
controllerMappingHyphenStyle: true
restControllerStyle: true
tablePrefix:
include:
- t_user
配置項參數解釋:https://mp.baomidou.com/config/generator-config.html#基本配置
五、運作maven指令
在指令工具中,進入到要生成項目的根目錄(即pom.xml目錄),執行以下指令
mvn mybatis-plus-generator:generator
如果是使用InterlliJ IDEA工具,使用更加友善,步驟如下圖:
生成結果如下: