赞
踩
mybatis-generator是非常方便的mybatis映射生成插件,gradle不支持这个,但是支持ant,所以可以配置gradle调用ant来生成。
首先介绍完整的写法(简易写法直接看最后):
先准备jdbc.properties文件:
jdbc.driver=com.mysql.jdbc.Driver #这里的mysql驱动文件注意版本不要太高,不要到6,不然会出问题
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
#mybatis-generator 参数
modelPackage=com.wk.entity.model #实体类的路径
daoPackage=com.wk.entity.dao #dao接口的路径
sqlMapperPackage=mapping #xml文件路径
我的目录结构
配置好数据库设置和生成路径以后就可以开始配置mybatis-generator了。
首先加一个configurations :
configurations {
mybatisGenerator
}
然后在dependencies加入相应的包:
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.2'
mybatisGenerator 'mysql:mysql-connector-java:5.1.45'
mybatisGenerator 'tk.mybatis:mapper:3.3.2'
最后加入task:
def getDbProperties = {
def properties = new Properties()
file("src/main/resources/config/db/jdbc.properties").withInputStream { inputStream ->
properties.load(inputStream)
}
properties
}
task mybatisGenerate << {
def properties = getDbProperties()
ant.properties['targetProject'] = projectDir.path
ant.properties['driverClass'] = properties.getProperty("jdbc.driver")
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("modelPackage")
ant.properties['mapperPackage'] = properties.getProperty("daoPackage")
ant.properties['sqlMapperPackage'] = properties.getProperty("sqlMapperPackage")
ant.taskdef(
name: 'mbgenerator',
classname: 'org.mybatis.generator.ant.GeneratorAntTask',
classpath: configurations.mybatisGenerator.asPath
)
ant.mbgenerator(overwrite: true,
configfile: 'src/main/resources/config/db/mybatis-generator.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')
}
}
}
task内容很好理解,从文件中读取的变量名可以根据你的命名修改,和配置属性对应就好,task读取了两个文件,一个是前面说的jdbc的配置,还一个是generator的配置:
<?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">
<!--<plugin type="tk.mybatis.mapper.generator.MapperPlugin">-->
<!--<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>-->
<!--<!– caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true –>-->
<!--<property name="caseSensitive" value="true"/>-->
<!--</plugin>-->
<commentGenerator>
<property name="suppressAllComments" 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"/>
<!-- 要生成的表 -->
<!-- 这里的百分号代表对该数据库中所有的表进行生成 -->
<table tableName="%">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
</context>
</generatorConfiguration>
前面注释的部分为生成注解注释,需要的将注释取消就好了。
现在介绍简单写法:
先在gradle加一个configurations
configurations {
mybatisGenerator
}
然后在依赖中加入mybatisGenerator的依赖:
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'
最后再在grade了加入一个task,configfile的值为mybatis-generator的配置文件路径:
task mybatisGenerate << {
ant.taskdef(
name: 'mbgenerator',
classname: 'org.mybatis.generator.ant.GeneratorAntTask',
classpath: configurations.mybatisGenerator.asPath
)
ant.mbgenerator(overwrite: true,
configfile: 'src/main/resources/generatorConfig.xml', verbose: true)
}
下面附上简易的mybatis-generator的配置文件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">
<!--<plugin type="tk.mybatis.mapper.generator.MapperPlugin">-->
<!--<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>-->
<!--<!– caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true –>-->
<!--<property name="caseSensitive" value="true"/>-->
<!--</plugin>-->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="connectionURL"
userId="username"
password="password">
</jdbcConnection>
<!--自动生成的实体的存放包路径 -->
<!-- targetPackage为包路径,targetPackage为包相对于项目根目录的路径 -->
<javaModelGenerator targetPackage="com.wk.entity.po" targetProject="src/main/java/">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--自动生成的*Mapper.xml文件存放路径 -->
<sqlMapGenerator targetPackage="mybatis-mapper" targetProject="src/main/resources/">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--自动生成的*Mapper.java存放路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.wk.entity.dao" targetProject="src/main/java/">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 -->
<!-- 这里的百分号代表对该数据库中所有的表进行生成 -->
<table tableName="tableName">
<!--数据库schema-->
<property name="runtimeSchema" value="databaseName"/>
<!--主键-->
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
配置好了以后打开gradle操作界面可以看到task任务,双击运行即可:
关于mybatis-generator配置文件详细讲解:
https://www.cnblogs.com/ygjlch/p/6471924.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。