MyBatis-Plus Gradle 代码生成插件了解一下

Mybatis Plus Gradle Generator


介绍

Web开发中使用mybatis比较多,MyBatis Plus是一个比较好的扩展,并且还可以自动生成代码,比较方便。
但是MyBatis Plus 对maven比较友好,对Gradle就比较差了,现在Spring Boot也对Gradle支持比较好,在使用Gradle生成代码的时候就比较麻烦。

使用姿势

本插件需要结合Spring Boot项目一起使用,如下所示,首先添加插件依赖,当前版本为
Download

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
buildscript {
ext {
springBootVersion = '2.1.1.RELEASE'
}
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://dl.bintray.com/liushuixiaoxia/maven" }
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("cn.mycommons:mpg:${mpgVesion}")
}
}

然后配置相关属性即可,本配置可以参考MyBatis Plus 代码生成官方文档,基本配置和官方配置一样。

示例:表名为tb_app_info,前缀为tb_,生成的entity为AppInfo,mapper为AppInfoMapper.xml

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
apply plugin: 'mpg'
mpg {
enable = true

globalConfig {
outputDir = projectDir.path + "/src/main/java"
author = "Admin"
open = false
baseResultMap = true
}

dataSourceConfig {
driverName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/mpg_test?useUnicode=true&characterEncoding=utf8&useSSL=false"
username = "root"
password = "admin"
}

packageConfig {
parent = "cn.mycommons.springdemo.mpg"
entity = "mybatis.entity"
mapper = "mybatis.mapper"
}

xmlMapperConfig {
path = projectDir.path + "/src/main/resources/mapper/"
name = "AppInfoMapper.xml"
}

strategyConfig {
include = ["tb_app_info"]
tablePrefix = ["tb_"]
entityLombokModel = true
restControllerStyle = true
superEntityClass = "cn.mycommons.basic.dto.BaseEntity"
logicDeleteFieldName = "is_delete"
superEntityColumns = ["create_by", "create_time", "update_by", "update_time", "remark", "is_delete"]
entityTableFieldAnnotationEnable(true)
controllerMappingHyphenStyle = true
superControllerClass = "cn.mycommons.basic.controller.BaseController"
}
}

下面为自定义配置,表示生成xmlMapper的文件路径。

1
2
3
4
xmlMapperConfig {
path = projectDir.path + "/src/main/resources/mapper/"
name = "AppInfoMapper.xml"
}

然后执行Gradle Task mpg 即可。

mpg

相关链接

MyBatis Plus

MyBatis Plus 文档

MyBatis Plus Gradle Plugin

坚持原创技术分享,您的支持将鼓励我继续创作!