-
jdbc.properties generatorConfig.xml mybatis插件启动
在gradle中使用MyBatis Generator生成model,dao,mapper
Mybatis Generator是一个mybatis工具项目,用于生成mybatis的model
,mapper
,dao
持久层代码。Mybatis Generator提供了maven plugin
,ant target
,java
三种方式启动。现在主流的构建工具是gradle,虽然mybatis generator没有提供gradle的插件,但gradle可以调用ant任务,因此,gradle也能启动Mybatis Generator。
配置依赖
在build.gradle中:
运行ant需要运行环境,也就是相应的jar包,因此添加一个配置
configurations {
mybatisGenerator
}
如果你是使用mysql8 请数据库连接引擎换成(Unknown system variable 'query_cache_size')
mybatisGenerator 'mysql:mysql-connector-java:8.0.11'
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.2' mybatisGenerator 'mysql:mysql-connector-java:5.1.36' mybatisGenerator 'tk.mybatis:mapper:3.3.2' |
配置task
在build.gradle中,定义一个task,在task里面调用ant
def getDbProperties = { def properties = new Properties() file("src/main/resources/mybatis/db-mysql.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.user") ant.properties['password'] = properties.getProperty("jdbc.pass") 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') } } }
从db-mysql.propertis
读取配置大致思路是:
- 把配置注入
ant
任务 - 运行
ant
,生成文件
数据库配置文件,文件在src/main/resources/db-mysql.properties
防止出现乱码 ?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
# JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名 jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC jdbc.user=root jdbc.pass=123 jdbc.driverClassName=com.mysql.jdbc.Driver #mysql8.0 #jdbc.driverClassName=com.mysql.cj.jdbc.Driver # 生成实体类所在的包 package.model=com.ssm.model # 生成 mapper 类所在的包 package.mapper=com.ssm.dao # 生成 mapper xml 文件所在的包,默认存储在 resources 目录下 package.xml=mapper #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWait=60000
db/generatorConfig配置其他配置,gradle.propertis
db/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="MysqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="${driverClass}"
connectionURL="${connectionURL}"
userId="${userId}"
password="${password}">
</jdbcConnection>
<javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}"/>
<sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}"/>
<javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER"/>
<!-- sql占位符,表示所有的表 -->
<table tableName="%" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
// % 表示数据库所有表
</table>
// 只建一个表的实体类 tableName数据库表的名字 domainObjectName实体类的名字
<table tableName="test" domainObjectName="Test" enableCountByExample="true" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
run,生成代码
在gradle中刷新,点开module下的other
找到 mybatisGenerat ,双击
https://chenkaihua.com/2015/12/19/running-mybatis-generator-with-gradle/