更新时间:
#MyBatisGenerator-Tool
网上找到一个pom maven的mybatis generator
https://github.com/kingcos/MyBatisGenerator-Tool
下面通过ant生成mybatis; 官方gradle也支持mybatisgenarator,可以自己尝试
也可参考 : https://segmentfault.com/a/1190000013534059
//由于代码生成单独运行即可,不需要参与到整个项目的编译
configurations {
mybatisGenerator
}
dependencies {
// 前面需将「compile group:」改为 「mybatisGenerator」
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.7'
mybatisGenerator 'mysql:mysql-connector-java:5.1.35'
mybatisGenerator 'tk.mybatis:mapper:3.3.9'
mybatisGenerator 'com.itfsw:mybatis-generator-plugin:1.2.10'
}
def getDbProperties = {
def properties = new Properties()
file("src/main/resources/mybatis-generator/generator.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.properties['tablename'] = properties.getProperty("table.name")
ant.properties['domainName'] = properties.getProperty("domain.name")
ant.taskdef(
name: 'mbgenerator',
classname: 'org.mybatis.generator.ant.GeneratorAntTask',
classpath: configurations.mybatisGenerator.asPath
)
ant.mbgenerator(overwrite: true,
configfile: 'src/main/resources/mybatis-generator/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')
propertyref(name: 'tablename')
propertyref(name: 'domainName')
}
}
}
# JDBC 驱动类名
jdbc.driverClassName=com.mysql.jdbc.Driver
# JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名
jdbc.url=jdbc:mysql://192.168.0.1:3306/blog?autoReconnect=true
# JDBC 用户名及密码
jdbc.username=root
jdbc.password=root
# 生成实体类所在的包
package.model=com.xxx.puredb.model
# 生成 mapper 类所在的包
package.mapper=com.xxx.puredb.dao
# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=com.xxx.puredb.dao
table.name=user_mecha
domain.name=UserMecha //也可以自动驼峰生成