在 Gradle 中使用 MyBatis Generator
在 Intellij IDEA 中结合 Gradle 使用 MyBatis Generator 逆向生成代码
- Info:
- JDK 1.8
- Gradle 2.14
- Intellij IDEA 2016.3
前言
由于 IDEA 的教程较少,且 MyBatis Generator 不支持 Gradle 直接运行,因此这次是在自己折腾项目过程中,根据一些参考资料加上自己的实践得出的结论,并附上相应的 Demo 可供自己未来参考,也与大家分享。
本文的 Demo 也可以当作工具直接导入 IDEA,加上自己的数据库信息即可生成代码。
创建项目
详细的创建步骤可以参考使用 Gradle 构建 Struts 2 Web 应用中「新建 Gradle Web 项目」一节即可。当创建完毕,需要等待 Gradle 联网构建,由于国内网络因素,可能需要稍作等待。当构建完成,目录结构应如下图一致:

配置依赖
这里需要使用 MyBatis Generator,MySQL 驱动,以及 MyBatis Mapper。由于代码生成单独运行即可,不需要参与到整个项目的编译,因此在 build.gradle 中添加配置:
configurations {
mybatisGenerator
}
在 Maven Repository (http://mvnrepository.com/) 分别搜索依赖:



依赖的版本并不是局限于某个特定版本,可以选择适时相应的更新版本。添加到 build.gradle 的 dependencies 中(注意前面需将「compile group:」改为 「mybatisGenerator」):
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5'
mybatisGenerator 'mysql:mysql-connector-java:5.1.40'
mybatisGenerator 'tk.mybatis:mapper:3.3.9'
}
设置数据库信息
在 resources 下,新建 mybatis 文件夹,并新建 config.properties 和 generatorConfig.xml,文件结构如下:

在 config.properties 中配置数据库和要生成的 Java 类的包:
# JDBC 驱动类名
jdbc.driverClassName=com.mysql.jdbc.Driver
# JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名
jdbc.url=jdbc:mysql://
# JDBC 用户名及密码
jdbc.username=
jdbc.password=
# 生成实体类所在的包
package.model=com.maimieng.model
# 生成 mapper 类所在的包
package.mapper=com.maimieng.mapper
# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=mybatis
设置生成代码的配置文件
在 generatorConfig.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="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<commentGenerator>
<property name="suppressAllComments" value="true"></property>
<property name="suppressDate" value="true"></property>
<property name="javaFileEncoding" value="utf-8"/>
</commentGenerator>
<jdbcConnection driverClass="${driverClass}"
connectionURL="${connectionURL}"
userId="${userId}"
password="${password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
<property name="enableSubPackages" value="true"></property>
<property name="trimStrings" value="true"></property>
</javaModelGenerator>
<sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}">
<property name="enableSubPackages" value="true"></property>
</sqlMapGenerator>
<javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="ANNOTATEDMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- sql占位符,表示所有的表 -->
<table tableName="%">
<generatedKey column="epa_id" sqlStatement="Mysql" identity="true" />
</table>
</context>
</generatorConfiguration>
Gradle 设置 Ant Task
由于 MyBatis Generator 尚不支持 Gradle,所以只能使用 Gradle 来执行 Ant Task,达到相同的效果。
build.gradle:
def getDbProperties = {
def properties = new Properties()
file("src/main/resources/mybatis/jdbc.properties").withInputStream { inputStream ->
properties.load(inputStream)
}
properties
}
task mybatisGenerate << {
def properties = getDbProperties()
ant.properties['targetProject'] = projectDir.path
ant.properties['driverClass'] = properties.getProperty("jdbc.driverClassName")
ant.properties['connectionURL'] = properties.getProperty("jdbc.url")
ant.properties['userId'] = properties.getProperty("jdbc.username")
ant.properties['password'] = properties.getProperty("jdbc.password")
ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
ant.properties['src_main_resources'] = sourceSets.main.resources.srcDirs[0].path
ant.properties['modelPackage'] = properties.getProperty("package.model")
ant.properties['mapperPackage'] = properties.getProperty("package.mapper")
ant.properties['sqlMapperPackage'] = properties.getProperty("package.xml")
ant.taskdef(
name: 'mbgenerator',
classname: 'org.mybatis.generator.ant.GeneratorAntTask',
classpath: configurations.mybatisGenerator.asPath
)
ant.mbgenerator(overwrite: true,
configfile: 'src/main/resources/mybatis/generatorConfig.xml', verbose: true) {
propertyset {
propertyref(name: 'targetProject')
propertyref(name: 'userId')
propertyref(name: 'driverClass')
propertyref(name: 'connectionURL')
propertyref(name: 'password')
propertyref(name: 'src_main_java')
propertyref(name: 'src_main_resources')
propertyref(name: 'modelPackage')
propertyref(name: 'mapperPackage')
propertyref(name: 'sqlMapperPackage')
}
}
}
配置好 build.gradle,就可以在 IDEA 的 Gradle 菜单中找到新的 Task:

如果这里没有,可能是 Gradle 没有 build,重新刷新即可:

双击「mybatisGenerate」即可运行,如果配置正确即可运行成功:

注:这里我在 generatorConfig.xml 配置选择的是按注解映射,因此没有 xml 文件生成。
总结
这次的 Demo 您可以在 https://github.com/kingcos/MyBatisGenerator-Tool 下载查看。Demo 本身也可作为工具来生成代码。由于目前正在完善一个 Android + Java Web 的小系统,因此未来可能还会更新 Spring Boot、Druid、Swagger 的配置脚手架。
参考资料
感谢您的阅读。才疏学浅,如有不当之处,定虚心接纳。也欢迎您关注我的博客和公众号。
作者:萌面大道
链接:https://www.jianshu.com/p/5c85becf5f73
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
在 Gradle 中使用 MyBatis Generator的更多相关文章
- idea中使用MyBatis Generator
1.新建maven项目 2.新建Generator配置文件 generator_config.xml <?xml version="1.0" encoding="U ...
- 在IDEA中使用MyBatis Generator自动生成代码
转载自 https://blog.csdn.net/hua_faded/article/details/78900780 一.配置Maven pom.xml 文件 在pom.xml增加以下插件: ...
- 在IDEA中使用MyBatis Generator逆向工程生成代码
本文介绍一下用Maven工具如何生成Mybatis的代码及映射的文件. 一.配置Maven pom.xml 文件 在pom.xml增加以下插件: <build> <finalName ...
- 在springboot中使用Mybatis Generator的两种方式
介绍 Mybatis Generator(MBG)是Mybatis的一个代码生成工具.MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率.如果需要联合查询仍然需要手写sql. ...
- Spring中使用MyBatis Generator
简介 MyBatis Generator 是由MyBatis官方提供的MyBatis代码生成器.可以根据数据库表生成相关代码,比如POJO.Mapper接口.SQL Map xml等. 使用方式 MB ...
- idea 中使用Mybatis Generator逆向工程生成代码
通过MAVEN完成 Mybatis 逆向工程 1. POM文件中添加插件 在 pom 文件的build 标签中 添加 plugin 插件和 数据库连接 jdbc 的依赖. <build> ...
- mybatis(一)MyBatis Generator
在gradle中使用MyBatis Generator时,build.gradle配置如下: dependencies { mybatisGenerator group: 'org.mybatis.g ...
- Maven多模块项目使用MyBatis Generator
开发环境: JDK:8u102 Maven:3.3.9 MySQL:5.7.10 MySQL Connector:5.1.40 IDE:IntelliJ IDEA 2016 MyBatis:3.4.1 ...
- MyBatis Generator自动生成MyBatis的映射代码
MyBatis Generator大大简化了MyBatis的数据库的代码编写,有了一个配置文件,就可以直接根据表映射成实体类.Dao类和xml映射.资源地址:MyBatis项目地址:http://my ...
随机推荐
- su和sudo命令详解
我们知道,在Linux下对很多文件进行修改都需要有root(管理员)权限,比如对/ect/profile等文件的修改.很多情况下,我们在进行开发的时候都是使用普通用户进行登录的,尤其在进行一些环境变量 ...
- 分享我对JS插件开发的一些感想和心得
本文阅读目录: •起因•如何开发一个轻量级的适用性强的插件•总结 起因 如果大家平时做过一些前端开发方面的工作,一定会有这样的体会:页面需要某种效果或者插件的时候,我们一般会有两种选择: 1.上网查找 ...
- jmeter常用组件
测试计划(Test Plan) 是使用 JMeter 进行测试的起点,它是其它 JMeter 测试元件的容器. 线程组(Thread Group) 代表一定数量的并发用户,它可以用来模拟并发用户发送请 ...
- IDA Pro的patch插件 KeyPatch
本来这个是没什么可写的,但是安这个插件的时候真是气到爆炸,安装文档写的不明不白,几万行的代码都写了就差那么点时间写个几十字的详细说明吗? 1.下载keypatch.py放到\IDA\plugins里 ...
- vijos 1128 N个数选K个数 (DFS )
从 n 个整数中任选 k 个整数相加,可分别得到一系列的和 要求你计算出和为素数共有多少种 IN4 33 7 12 19 OUT1 # include <iostream> # inclu ...
- centos6.5上安装配置telnet服务
https://blog.csdn.net/wx5040257/article/details/78327915
- hihocoder 编程练习赛23
第一题:H国的身份证号码I 题意:一个N位的正整数(首位不能是0).每位数字都小于等于K,并且任意相邻两位数字的乘积也小于等于K.按从小到大的顺序输出所有合法的N位号码,每个号码占一行. 思路:dfs ...
- 【UOJ】#49.铀仓库
题解: 会发现实质上运一个点就是两个点之间的距离 暴力是n^2的 考虑二分距离来计算 二分完之后还要二分这个点对应的位置 nlognlogn的 考虑一种常用的思路 用一个点来更新另一个点 首先我们先二 ...
- centos7网卡名修改
centos7网卡名不是以etho的方式命名,有时候在自动化方面不便于管理,在安装的时候输入如下代码即可命名: net.ifnames=0 biosdevname=0
- P1032 字串变换 字符串BFS
题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1 ->B_1B1 A_2A2 -> B_2B2 规则的含义为:在 AA中的子串 A_1A1 ...