gradleでMyBatis Generatorを使ってmodel,dao,mapperを生成します。
Mybatis Generatorはmybatisツールプロジェクトで、mybatisの
環境の説明データベース: データベース構成ファイル: プロジェクトにはmybatis が使われています。
設定依存
はい、
build.gradle
中:
ANtを実行するには、実行環境、すなわち、Jarパッケージが必要です。そのため、設定を追加します。
build.gradleでは、taskを定義して、taskの中でANtを呼び出します。は、 を読み出す。は、構成を に注入する。は を生成する。
データベースの設定ファイルは
gradle mybatis Generate
http://www.chenkaihua.com/2015/12/19/running-mybatis-generator-with-gradle/
model
、mapper
、dao
の耐久層コードを生成します。Mybatis Generatorはmaven plugin
、ant target
、java
の3つの方法で起動します。現在主流の構築ツールはgradleで、mybatis generatorはgradleのプラグインを提供していませんが、gradleはantタスクを呼び出すことができますので、gradleもMybatis Generatorを起動することができます。環境の説明
mysql
src/main/resources/db-mysql.properties
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/