Gradle配置MybatisGenerator逆向工程
之前一直都是用的maven的方式做的,由于公司现在用的是Gradle构建工程,所以也是在网上找了一下方案,大家可以参考一下我的方式,这里附上Gradle的插件官网,大家可以去官网搜索想要的插件
- Gradle插件官网: Gradle - Plugins
1.首先在buid.gradle文件里面导入相关的jar包和MybatisGenerator的插件
buid.gradle可以理解成maven的pom.mxl,这里需要注意的是gradle是2.1之前的比较久的版本跟2.1之后的新的版本导入插件的方式不一样,这一点gradle的插件官网是有说明的
- 2.1之前的版本
buildscript {
ext {
springBootVersion = '2.0.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.jarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
//数据库
compile group: 'org.apache.tomcat', name: 'tomcat-jdbc', version: '8.5.23'
//日志
compile group: 'log4j', name: 'log4j', version: '1.2.17'
//fastjson
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47'
}
//mybatis generator plugin ------ start
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"
}
}
apply plugin: "com.arenagod.gradle.MybatisGenerator"
configurations {
mybatisGenerator
}
mybatisGenerator {
verbose = true
configFile = 'src/main/resources/tools/generatorConfig.xml'
}
//mybatis generator plugin ------ end
- 2.1之后的版本
buildscript {
ext {
springBootVersion = '2.0.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.jarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
//数据库
compile group: 'org.apache.tomcat', name: 'tomcat-jdbc', version: '8.5.23'
//日志
compile group: 'log4j', name: 'log4j', version: '1.2.17'
//fastjson
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47'
}
//mybatis generator plugin ------ start
plugins {
id "com.arenagod.gradle.MybatisGenerator" version "1.4"
}
apply plugin: "com.arenagod.gradle.MybatisGenerator"
configurations {
mybatisGenerator
}
mybatisGenerator {
verbose = true
configFile = 'src/main/resources/tools/generatorConfig.xml'
}
//mybatis generator plugin ------ end
2.在resources目录下新建config.properties文件配置数据库链接信息和要生成的 Java 类的包
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.11:3306/ssmDemo?characterEncoding=utf8
jdbc.username=root
jdbc.password=111111
# 生成实体类所在的包
package.model=org.zn.user.entity
# 生成 mapper 类所在的包
package.mapper=org.zn.user.dao
# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=mapping
# 表名
package.tableName=user
# 生成实体类名称
package.entityName=User
3.在resources目录下新建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>
<!-- type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的JavaModel 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 这种方式是所有的表逆向生成类 下面是单个表具体看情况使用
<table tableName="%">
<generatedKey column="epa_id" sqlStatement="Mysql" identity="true" />
</table> -->
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="${tableName}" domainObjectName="${entityName}"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>