LoginSignup
12
15

More than 3 years have passed since last update.

GradleからMybatisGeneratorを実行する

Last updated at Posted at 2017-07-01

GradleでMybatisGeneratorのタスクを作成する機会があったのでメモ

カスタムプラグインも適用したい方 -> GradleでMybatisGenerator実行した際にMybatisのカスタムプラグインを適用する

環境

  • JDK 1.8
  • IntelliJ IDEA Ultimate 2017.1.4
  • Gradle 3.4.1

MyBatis Generator Config 周り

MybatisGeneratorの自動生成の設定

generator_config.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="schema" defaultModelType="hierarchical">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>

        <commentGenerator>
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="${driverClass}" connectionURL="${connectionURL}${schema}?useSSL=false" userId="${userId}" password="${password}"/>

        <javaModelGenerator targetPackage="${modelPackage}" targetProject="${targetProject}"/>
        <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${targetProject}" type="ANNOTATEDMAPPER"/>

        <table tableName="*"/>
    </context>
</generatorConfiguration>
generator_config.properties
# Project
target.project=project/src/main/java

# DB
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/
db.username=root
db.password=

# Schema
db.schema=schema

# model 生成パッケージ
package.model=project.core.mybatis.generate.model
# mapper 生成パッケージ
package.mapper=project.core.mybatis.generate.mapper

上記のようにpropertiesファイルに値を定義しているとuser系、master系などのようにスキーマが増えた時でもgenerator_config.xmlにdbのurl、username,passwordなどを複数定義しなくて済む。
passwordを変更したときにあちこち修正するという手間を省ける

Gradle

build.gradleでMybatisGeneratorのタスク作成

build.gradle
~~~ その他の詳細設定省略 ~~~

project(':project-core') {
    ext {
        // Mybatis Generator
        mybatisMapperVersion = '3.4.0'
        mybatisGeneratorVersion = '1.3.5'

        // Mysql
        mysqlConnectorJavaVersion = '5.1.42'
    }

    configurations {
        mybatisGenerator
    }

    dependencies {      
        // Mybatis Generator 関連
        mybatisGenerator group: 'org.mybatis.generator', name: 'mybatis-generator-core', version: mybatisGeneratorVersion
        mybatisGenerator group: 'mysql', name: 'mysql-connector-java', version: mysqlConnectorJavaVersion
        mybatisGenerator group: 'tk.mybatis', name: 'mapper', version: mybatisMapperVersion
    }

    //**************************************
    // Mybatis Generator task
    //**************************************

    task mybatisGenerator {
        doLast {
            def properties = new Properties()
            file('src/main/resources/mybatis/generator_config.properties').withInputStream { inputStream ->
                properties.load(inputStream)
            }
            ant.properties['targetProject'] = properties.getProperty('target.project')
            ant.properties['driverClass'] = properties.getProperty('db.driverClassName')
            ant.properties['connectionURL'] = properties.getProperty('db.url')
            ant.properties['userId'] = properties.getProperty('db.username')
            ant.properties['password'] = properties.getProperty('db.password')
            ant.properties['schema'] = properties.getProperty('db.schema')
            ant.properties['modelPackage'] = properties.getProperty('package.model')
            ant.properties['mapperPackage'] = properties.getProperty('package.mapper')
            ant.taskdef(name: 'mbgenerator', classname: 'org.mybatis.generator.ant.GeneratorAntTask', classpath: configurations.mybatisGenerator.asPath)
            ant.mbgenerator(overwrite: true, configfile: 'project-core/src/main/resources/mybatis/generator_config.xml', verbose: true) {
                propertyset {
                    propertyref(name: 'targetProject')
                    propertyref(name: 'userId')
                    propertyref(name: 'driverClass')
                    propertyref(name: 'connectionURL')
                    propertyref(name: 'password')
                    propertyref(name: 'masterSchema')
                    propertyref(name: 'userSchema')
                    propertyref(name: 'modelPackage')
                    propertyref(name: 'mapperPackage')
                    propertyref(name: 'sqlMapperPackage')
                }
            }
        }
    }

    // mybatisGeneratorのタスクグループをmybatisにしておく
    // これをしておくことによってgradleのタスクがグループ分けされて見やすくなる
    mybatisGenerator.group = 'mybatis'
}

propertiesファイルの内容を取得して、generator_config.xmlの${}で変数定義してある部分に値を追加している

propertiesファイルを使用しない方法

generator_config.xmlには変数定義してある${}の部分を直接書いてしまえば大丈夫で、
mybatisGeneratorのタスクは以下のようになおせばできるのでないかと(試してない)2017/07/03 動作確認済み

build.gradle
task mybatisGenerator {
        doLast {
            ant.taskdef(name: 'mbgenerator', classname: 'org.mybatis.generator.ant.GeneratorAntTask', classpath: configurations.mybatisGenerator.asPath)
            ant.mbgenerator(overwrite: true, configfile: 'project-core/src/main/resources/mybatis/generator_config.xml', verbose: true)
        }
    }

参考

GradleでMyBatis Generatorを使う
在 Gradle 中使用 MyBatis Generator

12
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
15