赞
踩
由于在 IntelliJ IDEA 中的 Gradle 项目无法直接使用著名的 MybatisGenerator 插件,所以要间接的通过一个 Ant Task 来生成
configurations {
mybatisGenerator
}
<span style="font-size:18px;">dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5'
mybatisGenerator 'mysql:mysql-connector-java:5.1.40'
mybatisGenerator 'tk.mybatis:mapper:3.3.9'
}</span>
# MySQL 驱动
jdbc.driverClassName = com.mysql.jdbc.Driver
# JDBC URL
jdbc.url =j dbc:mysql://127.0.0.1:3306/fullstack?useUnicode:true&characterEncoding:UTF-8
# 数据库用户名及密码
jdbc.username = root
jdbc.password = root
# 实体类所在包
package.model = me.zbl.fullstack.entity
# mapper 所在包
package.mapper = me.zbl.fullstack.mapper
# mapper xml 文件所在包
package.xml = mapping
- <?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>
- <!-- java 文件编码格式 -->
- <property name="javaFileEncoding" value="utf-8"/>
- </commentGenerator>
-
- <!-- jdbc 连接信息 -->
- <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>
-
- <!-- xml 所在包名 -->
- <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}">
- <property name="enableSubPackages" value="true"></property>
- </sqlMapGenerator>
-
- <!-- mapper 所在包名 -->
- <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="ANNOTATEDMAPPER">
- <property name="enableSubPackages" value="true"/>
- </javaClientGenerator>
-
- <!-- 要生成的表 -->
- <!-- 这里的百分号代表对该数据库中所有的表进行生成 -->
- <table tableName="%">
- <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
- </table>
- </context>
- </generatorConfiguration>
def getDbProperties = {
def properties = new Properties()
file("src/main/resources/mybatis/jdbc.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.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')
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。