gradleでMyBatis Generatorを使ってmodel,dao,mapperを生成します。


Mybatis Generatorはmybatisツールプロジェクトで、mybatisのmodelmapperdaoの耐久層コードを生成します。Mybatis Generatorはmaven pluginant targetjavaの3つの方法で起動します。現在主流の構築ツールはgradleで、mybatis generatorはgradleのプラグインを提供していませんが、gradleはantタスクを呼び出すことができますので、gradleもMybatis Generatorを起動することができます。
環境の説明
  • データベース:mysql
  • データベース構成ファイル:src/main/resources/db-mysql.properties
  • プロジェクトにはmybatis tk.mybatis:mapper:3.3.2プラグイン
  • が使われています。
    設定依存
    はい、
    build.gradle
    中:
    ANtを実行するには、実行環境、すなわち、Jarパッケージが必要です。そのため、設定を追加します。
    configurations {
        mybatisGenerator
    }
    この設定に依存を追加します。
    repositories {
        mavenCentral()
    }
    dependencies {
        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/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'] = this.modelPackage
        ant.properties['mapperPackage'] = this.mapperPackage
        ant.properties['sqlMapperPackage'] = this.sqlMapperPackage
        ant.taskdef(
                name: 'mbgenerator',
                classname: 'org.mybatis.generator.ant.GeneratorAntTask',
                classpath: configurations.mybatisGenerator.asPath
        )
        ant.mbgenerator(overwrite: true,
                configfile: 'db/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にあります。
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost/mhml
    jdbc.user=root
    jdbc.pass=rootpass
    その他の設定、gradle.propertis
    modelPackage=com.dreamliner.mhml.entity
    #   mapper      
    mapperPackage=com.dreamliner.mhml.mapper
    #   mapper xml     ,     resources   
    sqlMapperPackage=mybatis_mapper
    db/generator Config配置db/generatorConfig.xmlファイルは生成ポリシーを設定するためのもので、公式サイトで詳細に紹介されています。
    
    
    
        
            
                
                
                
            
            
                
            
            
            
            
            
            
          
    	    
                    
    コードを生成
    gradle mybatis Generate
    http://www.chenkaihua.com/2015/12/19/running-mybatis-generator-with-gradle/