Springboot+gradle+Mybatis-Generator 代码自动生成器

mybatis-Generator官方文档:http://www.mybatis.org/genera...
Gradle官方文档:https://plugins.gradle.org/pl...

gradle

  1 buildscript {
  2     ext {
  3         springBootVersion = '2.2.4.RELEASE'
  4     }
  5     repositories {
  6         mavenLocal()
  7         maven { url 'http://maven.aliyun.com/nexus/content/repositories/central/' }
  8     }
  9     dependencies {
 10         classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
 11         // mybatis-generator 插件路径
 12         classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"
 13     }
 14 }
 15 
 16 plugins {
 17     id 'org.springframework.boot' version '2.2.4.RELEASE'
 18     id 'io.spring.dependency-management' version '1.0.9.RELEASE'
 19     id 'java'
 20 }
 21 
 22 
 23 
 24 group = 'com.zuo'
 25 version = '1.0.0-SNAPSHOT'
 26 sourceCompatibility = '1.9'
 27 apply plugin: 'java'
 28 apply plugin: 'idea'
 29 apply plugin: 'eclipse'
 30 apply plugin: 'org.springframework.boot'
 31 //引入 mybatis-generator 插件
 32 apply plugin: "com.arenagod.gradle.MybatisGenerator"
 33 
 34 
 35 configurations {
 36     developmentOnly
 37     runtimeClasspath {
 38         extendsFrom developmentOnly
 39     }
 40 }
 41 
 42 configurations.all {
 43     resolutionStrategy {
 44         failOnVersionConflict()
 45         force 'commons-io:commons-io:2.4'
 46         force 'commons-logging:commons-logging:1.2'
 47     }
 48 }
 49 
 50 repositories {
 51     mavenLocal()
 52     mavenCentral()
 53     maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
 54 }
 55 
 56 jar {
 57     enabled = true
 58 }
 59 
 60 dependencies {
 61     implementation 'org.springframework.boot:spring-boot-starter-jdbc'
 62     implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
 63     implementation 'org.springframework.boot:spring-boot-starter-web'
 64     implementation 'org.springframework.boot:spring-boot-starter-web-services'
 65     implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'
 66     developmentOnly 'org.springframework.boot:spring-boot-devtools'
 67     runtimeOnly 'mysql:mysql-connector-java'
 68     testImplementation('org.springframework.boot:spring-boot-starter-test') {
 69         exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
 70     }
 71 
 72 //    compile('org.springframework.cloud:spring-cloud-starter-config')
 73 //    compile('org.springframework.cloud:spring-cloud-starter-ribbon')
 74 //    compile('org.springframework.cloud:spring-cloud-starter-eureka')
 75 
 76 
 77     compile('cglib:cglib-nodep:3.2.2')
 78     compile('org.javassist:javassist:3.20.0-GA')
 79     compile('ognl:ognl:3.1.4')
 80     compile('org.aspectj:aspectjrt:1.8.9')
 81     compile('org.aspectj:aspectjweaver:1.8.9')
 82     compile('org.apache.logging.log4j:log4j-core:2.5')
 83 
 84     // fastjson
 85     compile('com.alibaba:fastjson:1.2.37')
 86     compile('org.apache.poi:poi:3.17')
 87     compile('commons-fileupload:commons-fileupload:1.3.3')
 88     //compile('com.github.tobato:fastdfs-client:1.25.4-RELEASE') 有不兼容问题
 89     compile('com.github.tobato:fastdfs-client:1.26.2')
 90 
 91     compile ('dom4j:dom4j:1.6.1')
 92     compile ('com.alipay.sdk:alipay-sdk-java:3.4.27.ALL')
 93 
 94     compile('com.aliyun:aliyun-java-sdk-core:4.0.6')
 95     compile('com.aliyun:aliyun-java-sdk-dysmsapi:1.1.0')
 96     compile group: 'com.alibaba', name: 'druid', version: '1.1.12'
 97     compile group: 'com.alibaba', name: 'fastjson', version: '1.2.62'
 98     compile group: 'com.github.binarywang', name: 'weixin-java-mp', version: '3.0.0'
 99     annotationProcessor 'org.projectlombok:lombok:1.18.8'
100     compileOnly 'org.projectlombok:lombok:1.18.8'
101     compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.8.8'
102     compile group: 'tk.mybatis', name: 'mapper', version: '4.1.5'
103 
104     implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
105     implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
106     implementation 'org.springframework.boot:spring-boot-starter-data-redis'
107     implementation 'org.springframework.boot:spring-boot-starter-freemarker'
108     implementation 'org.springframework.boot:spring-boot-starter-security'
109     implementation 'org.springframework.boot:spring-boot-starter-validation'
110     implementation 'org.springframework.boot:spring-boot-starter-websocket'
111     implementation 'org.springframework.session:spring-session-data-redis'
112     implementation 'org.springframework.session:spring-session-jdbc'
113     compileOnly 'org.projectlombok:lombok'
114     developmentOnly 'org.springframework.boot:spring-boot-devtools'
115     annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
116     annotationProcessor 'org.projectlombok:lombok'
117     testImplementation 'org.springframework.security:spring-security-test'
118 
119     //mybatis-generator core 包
120     compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version:'1.3.2'
121 }
122 
123 // mybatis-generator.xml 配置路径
124 //这里会遇到个问题:MyBatis Generator 通过xml生成,有日志但是没有生成文件成功的问题,
125 //原因:mac下是找不到 ./src 路径的,需要全路径,如下配置。windows则为src/main/resources/generator.xml
126 mybatisGenerator {
127     verbose = true
128     configFile = '/Users/zuoyuhui/Documents/project/idea/model/src/main/resources/mybatis/generator.xml'
129 }
130 
131 test {
132     useJUnitPlatform()
133 }
134 
135 //bootRun {
136 ////开启页面热加载功能
137 //    addResources = true
138 //}

generator.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="my" targetRuntime="MyBatis3">

        <!--自动实现Serializable接口-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>

        <!-- 去除自动生成的注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--数据库基本信息-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/db_dfcserver"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!--生成实体类的位置以及包的名字-->
        <!--同样Mac用户:targetProject 为全路径-->
        <javaModelGenerator targetPackage="com.zuo.model.po" targetProject="/Users/zuoyuhui/Documents/project/idea/model/src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--生成映射文件存放位置-->
        <!--同样Mac用户:targetProject 为全路径-->
        <sqlMapGenerator targetPackage="mapper" targetProject="/Users/zuoyuhui/Documents/project/idea/model/src/main/resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!--生成Dao类存放位置,mapper接口生成的位置-->
        <!--同样Mac用户:targetProject 为全路径-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zuo.model.mapper" targetProject="/Users/zuoyuhui/Documents/project/idea/model/src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!-- 配置表信息 -->
        <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
            是否生成 example类 -->
        <table schema="db_dfcserver" tableName="fp_report"
               domainObjectName="FpReport" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false">
        </table>

    </context>
</generatorConfiguration>

生成代码 

点击mbGenerator

 成功

原文地址:https://www.cnblogs.com/zzuuoo666/p/12705183.html