这个插件是基于https://github.com/mybatis/generator maven插件做的.这个gradle插件是mybatis生成插件. 这个插件拥有了mybatisGeneratorPlugin(mbg)的全部功能,这个插件实现在gradle3.3上使用groovy实现. English
使用在所有Gradle版本的构建脚本片段:buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.cuisongliu.plugin:mybatis-generator:0.9.6"
}
}
apply plugin: "com.cuisongliu.plugin.mybatis-generator"
为Gradle 2.1中引入的新的,潜在的插件机制构建脚本代码段
plugins {
id "com.cuisongliu.plugin.mybatis-generator" version "0.9.6"
}
mbg {
overwrite = false
consoleable = true
jdbc{
driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://[ip]:[port]/[dbname]?useSSL=false"
username = "username"
password = "password"
}
xml{
resourcesProject = "src/main/java"
mapperPlugin = tk.mybatis.mapper.generator.MapperPlugin.class
mapperMapper = "com.cuisongliu.springboot.core.mapper.MyMapper"
mapperPackage= "com.cuisongliu.mapper"
modelPackage = "com.cuisongliu.entity"
xmlPackage = "com.cuisongliu.mapper"
tableName ="s_system"
objectName ="System"
mapperSuffix ="Mapper"
}
}
如果没有设置,插件试图智能使用默认值.
属性名 | 类型 | 描述 | 默认值 | |
---|---|---|---|---|
overwrite |
boolean |
是否覆盖已经生成的xml或者代码 | true |
|
generatorFile |
String |
mbg的配置文件位置 | generatorConfig.xml |
|
sqlScript |
String |
要在生成代码之前运行的 SQL 脚本文件的位置. | null |
|
consoleable |
boolean |
如果指定该参数,执行过程会输出到控制台。 | false |
|
skip |
boolean |
是否跳过生成代码的mbg的任务 | false |
|
contexts |
String |
如果指定了该参数,逗号隔开的这些context会被执行。 | null |
|
tableNames |
String |
如果指定了该参数,逗号隔开的这个表会被运行。 | null |
属性名 | 类型 | 描述 | 默认值 | |
---|---|---|---|---|
jdbc |
driver |
String |
jdbc的驱动类.不能为空 | null |
url |
String |
jdbc的数据库url.不能为空 | null |
|
username |
String |
jdbc的数据库用户名.不能为空 | root |
|
password |
String |
jdbc的数据库密码.不能为空 | null |
|
xml |
javaProject |
String |
生成java文件所在的目录. | src/main/java |
resourcesProject |
String |
生成xml配置文件mapper所在的目录. | src/main/resources |
|
mapperPackage |
String |
mapper配置,生成的Mapper(dao)所在的包所在的位置.不能为空 | null |
|
modelPackage |
String |
model配置,生成的实体类所在的包所在的位置.不能为空 | null |
|
xmlPackage |
String |
mapper的xml配置,生成的mapper的xml所在的包的位置.不能为空 | null |
|
mapperPlugin |
Class |
插件信息,xml中插件的类名.不能为空 |
tk.mybatis.mapper.
generator.MapperPlugin.
class
|
|
mapperMapper |
String |
mapper配置,生成的Mapper方法的父类.不能为空 |
tk.mybatis.mapper.
common.Mapper
|
|
tableName |
String |
mybatis生成对应的数据库表名.不能为空 |
null
|
|
objectName |
String |
mybatis生成对应的实体类名.不能为空 |
null
|
|
mapperSuffix |
String |
mybatis生成Mapper的后缀 |
Mapper
|
-
加入generatorConfig.xml到你的执行模块中去,设置table信息中的变量即可,文件中.
-
在build.gradle中加入参数配置
-
在build.gradle所在目录执行
gradle mbg
-
默认支持mysql,若使用oracle或者其他的数据库需要额外增加如下配置
buildscript{ def baseUrl = "http://maven.cuisongliu.com" def nexusUrl = "$baseUrl/content/groups/public/" repositories { mavenLocal() maven { url "$nexusUrl" } } dependencies { classpath "com.oracle:ojdbc6:11.1.0.7.0" } }
在使用插件之前加入buildscript,配置classpath的driver依赖jar包(这里的maven地址根据情况修改)