MyBatis-Plus Gradle 代碼生成插件瞭解一下

Mybatis Plus Gradle Generator


介紹

Web開發中使用mybatis比較多,MyBatis Plus是一個比較好的擴展,並且還可以自動生成代碼,比較方便。
但是MyBatis Plus 對maven比較友好,對Gradle就比較差了,現在Spring Boot也對Gradle支持比較好,在使用Gradle生成代碼的時候就比較麻煩。

使用姿勢

本插件需要結合Spring Boot項目一起使用,如下所示,首先添加插件依賴,當前版本爲
[圖片上傳失敗...(image-a43763-1545391159731)]

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

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的文件路徑。

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

然後執行Gradle Task mpg 即可。

相關鏈接

MyBatis Plus

MyBatis Plus 文檔

MyBatis Plus Gradle Plugin

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章