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

今天看了mybatis-Generator的官方文档,研究了一下,决定自己也搭一个,但是官方文档和大部分资料都是基于maven项目的,查了gradle的官方文档,终于找到了官方配置,很激动(^-^)V,记录下来,方便记忆也同时分享给大家~
mybatis-Generator官方文档:http://www.mybatis.org/genera...
Gradle官方文档:https://plugins.gradle.org/pl...

gradle
buildscript {
    ext {
        springBootVersion = '1.5.10.RELEASE'
    }
    repositories {
        mavenCentral()
        //添加maven仓库
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        // mybatis-generator 插件路径
        classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
//引入 mybatis-generator 插件
apply plugin: "com.arenagod.gradle.MybatisGenerator"

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    //数据源
    compile 'com.alibaba:druid-spring-boot-starter:1.1.2'
    compile 'mysql:mysql-connector-java:6.0.6'
    //配置mybatis
    compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1"
    //mybatis-generator core 包
    compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version:'1.3.2'
}

configurations {
    mybatisGenerator
}
// mybatis-generator.xml 配置路径
//这里会遇到个问题:MyBatis Generator 通过xml生成,有日志但是没有生成文件成功的问题, 
//原因:mac下是找不到 ./src 路径的,需要全路径,如下配置。windows则为src/main/resources/generator.xml
mybatisGenerator {
    verbose = true
    configFile = '/Users/murasakiseifu/mybatisGenerator/src/main/resources/generator.xml'
}
generator.xml

按照刚才的配置路径,在 resources 文件夹下建立 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/takin_write"
                        userId="murasakiseifu"
                        password="123456">
        </jdbcConnection>

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

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

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

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

    </context>
</generatorConfiguration>
生成代码

双击 mbGenerator

clipboard.png
出现如下提示为成功

clipboard.png

包结构

clipboard.png

参考

http://blog.csdn.net/u0110947...
http://blog.csdn.net/shaohx05...
https://github.com/kingcos/My...

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值