Gradle/MybatisGenerator

Gradle/MybatisGenerator

MybatisGenerator

这个 Gradle plugin 帮助自动生成 Mybatis 的 Mapper 代码

com.arenagod.gradle.MybatisGenerator

Config

build.gradle

plugins {
    id "com.arenagod.gradle.MybatisGenerator" version "1.4" // task mbGenerator
}

# Note the above "plugins" block must at the top position of build.gradle
# ...

mybatisGenerator {
  verbose = true
  configFile = "${projectDir}/generatorConfig.xml"
}

generatorConfig.xml

路径在 build.gradle 里指定

需要修改:

  • classpathEntry 指向数据库驱动 jar
  • jdbcConnection 设定数据库 jdbc 连接信息
  • 各 generator 里生成代码包名(以下示例用 "hello" 作为程序包名)
<?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 >
    <classPathEntry location="libs/ojdbc7.jar" />
    <context id="Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true" />
        </commentGenerator>
        <jdbcConnection
                driverClass="oracle.jdbc.driver.OracleDriver"
                connectionURL="jdbc:oracle:thin:@192.168.167.203:1521:acc"
                userId="user"
                password="pass" />
        <javaModelGenerator
                targetPackage="hello.model"
                targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaModelGenerator>
        <sqlMapGenerator
                targetPackage="hello.mapper"
                targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <javaClientGenerator
                targetPackage="hello.mapper"
                targetProject="src/main/java" type="MIXEDMAPPER">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <table tableName="ExampleTableName"
               enableInsert="true"
               enableSelectByPrimaryKey="true"
               enableSelectByExample="false"
               enableUpdateByPrimaryKey="true"
               enableUpdateByExample="false"
               enableDeleteByPrimaryKey="true"
               enableDeleteByExample="false"
               enableCountByExample="false"
               selectByExampleQueryId="false"
               modelType="flat">
        </table>
    </context>
</generatorConfiguration>

Run

gradle mbGenerator

Reference

参考:


Last update: 2018-04-09 03:07:16 UTC