title: Intellij IDEA 中結合 Gradle 使用 MyBatis Generator 逆向生成代碼
date: 2016-04-26 18:17:26
categories: "Java"
tags:
- MyBatis
在 Intellij IDEA 中結合 Gradle 使用 MyBatis Generator 逆向生成代碼
- JDK 1.8
- Gradle 3.5
- Intellij IDEA 2017
前言
Intellij IDEA 的教程較少,且 MyBatis Generator 不支持 Gradle 直接運行,因此這次是在自己折騰項目過程中,根據一些參考資料加上自己的實踐得出的結論,并附上相應的 Demo 可供自己未來參考,也與大家分享。
本文的 Demo 也可以當作工具直接導入 IDEA,加上自己的數據庫信息即可生成代碼。
創建項目
創建步驟可以參考intellij idea 2016 gradle搭建web工程中。當創建完畢,需要等待 Gradle 聯網構建,由于國內網絡因素,可能需要稍作等待。當構建完成,目錄結構應如下圖一致:
配置依賴
這里需要使用 MyBatis Generator,MySQL 驅動,以及 MyBatis Mapper。由于代碼生成單獨運行即可,不需要參與到整個項目的編譯,因此在 build.gradle 中添加配置:
configurations {
mybatisGenerator
}
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'
}
設置數據庫信息
在 resources 下,新建 mybatis 文件夾,并新建 jdbc.properties 和 generatorConfig.xml,文件結構如下:
在 config.properties 中配置數據庫和要生成的 Java 類的包:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.11:3306/ssmDemo?characterEncoding=utf8
jdbc.username=root
jdbc.password=111111
# 生成實體類所在的包
package.model=org.zn.user.entity
# 生成 mapper 類所在的包
package.mapper=org.zn.user.dao
# 生成 mapper xml 文件所在的包,默認存儲在 resources 目錄下
package.xml=mapping
# 表名
package.tableName=user
# 生成實體類名稱
package.entityName=User
設置生成代碼的配置文件
在 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">
<commentGenerator>
<property name="suppressAllComments" value="true"></property>
<property name="suppressDate" value="true"></property>
<property name="javaFileEncoding" value="utf-8"/>
</commentGenerator>
<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>
<sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}">
<property name="enableSubPackages" value="true"></property>
</sqlMapGenerator>
<!-- type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對象
type="MIXEDMAPPER",生成基于注解的JavaModel 和相應的Mapper對象
type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 -->
<javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 這種方式是所有的表逆向生成類 下面是單個表具體看情況使用
<table tableName="%">
<generatedKey column="epa_id" sqlStatement="Mysql" identity="true" />
</table> -->
<!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名-->
<table tableName="${tableName}" domainObjectName="${entityName}"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
Gradle 設置 Ant Task
由于 MyBatis Generator 尚不支持 Gradle,所以只能使用 Gradle 來執行 Ant Task,達到相同的效果。
build.gradle:
def getDbProperties = {
def properties = new Properties()
file("src/main/resources/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("package.model")
ant.properties['mapperPackage'] = properties.getProperty("package.mapper")
ant.properties['sqlMapperPackage'] = properties.getProperty("package.xml")
ant.properties['tableName'] = properties.getProperty("package.tableName")
ant.properties['entityName'] = properties.getProperty("package.entityName")
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) {
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: 'entityName')
}
}
}
配置好 build.gradle,就可以在 IDEA 的 Gradle 菜單中找到新的 Task:
如果這里沒有,可能是 Gradle 沒有 build,重新刷新即可:
雙擊「mybatisGenerate」即可運行,如果配置正確即可運行成功:
總結
感謝網上博友的無私奉獻,參考你們的博文最終得以完成
Gradle+SSM+MyBatis Generator源碼地址:https://github.com/enamor/ssm
原文地址: http://oxy.pub