0%

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

Mybatis Plus Gradle Generator


介绍

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

使用姿势

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

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

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

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

1
apply plugin: 'mpg'
2
mpg {
3
    enable = true
4
5
    globalConfig {
6
        outputDir = projectDir.path + "/src/main/java"
7
        author = "Admin"
8
        open = false
9
        baseResultMap = true
10
    }
11
12
    dataSourceConfig {
13
        driverName = "com.mysql.jdbc.Driver"
14
        url = "jdbc:mysql://localhost:3306/mpg_test?useUnicode=true&characterEncoding=utf8&useSSL=false"
15
        username = "root"
16
        password = "admin"
17
    }
18
19
    packageConfig {
20
        parent = "cn.mycommons.springdemo.mpg"
21
        entity = "mybatis.entity"
22
        mapper = "mybatis.mapper"
23
    }
24
25
    xmlMapperConfig {
26
        path = projectDir.path + "/src/main/resources/mapper/"
27
        name = "AppInfoMapper.xml"
28
    }
29
30
    strategyConfig {
31
        include = ["tb_app_info"]
32
        tablePrefix = ["tb_"]
33
        entityLombokModel = true
34
        restControllerStyle = true
35
        superEntityClass = "cn.mycommons.basic.dto.BaseEntity"
36
        logicDeleteFieldName = "is_delete"
37
        superEntityColumns = ["create_by", "create_time", "update_by", "update_time", "remark", "is_delete"]
38
        entityTableFieldAnnotationEnable(true)
39
        controllerMappingHyphenStyle = true
40
        superControllerClass = "cn.mycommons.basic.controller.BaseController"
41
    }
42
}

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

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

然后执行Gradle Task mpg 即可。

mpg

相关链接

MyBatis Plus

MyBatis Plus 文档

MyBatis Plus Gradle Plugin

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