gradle中使用mybatis-generator自动生成工具

  1. gradle中引入必须的jar包
dependencies{
    compile 'mysql:mysql-connector-java:5.1.36'
    compile 'org.mybatis.generator:mybatis-generator-core:1.3.5'
    compile 'tk.mybatis:mapper:3.4.2'
}

2.新建db.properties,配置数据库连接等

#数据库配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=UTF-8
jdbc.username=name
jdbc.password=pass

# 生成实体类所在的包
package.model=com.xxx.model

# 生成 mapper 类所在的包
package.mapper=com.xxx.mapper

# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=mapper

3.新建generatorConfig.xml




    
        
            
            
            
        
        
            
        
        
        
        
        
        
        
        
        

4.gradle暂时不支持mybatis generator,使用gradle执行ANT task,脚本如下:

def DB_PROPERTIES = "src/main/resources/config/db.properties"
def GENERATOR_CONFIG = "src/main/resources/config/generatorConfig.xml"
//
configurations {
    mybatisGenerator
}

dependencies{
    mybatisGenerator 'mysql:mysql-connector-java:5.1.36'
    mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5'
    mybatisGenerator 'tk.mybatis:mapper:3.4.2'
}

def getDbProperties = {
    def properties = new Properties()
    file(DB_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.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.taskdef(
            name: 'mbgenerator',
            classname: 'org.mybatis.generator.ant.GeneratorAntTask',
            classpath: configurations.mybatisGenerator.asPath
    )
    ant.mbgenerator(overwrite: true,
            configfile: GENERATOR_CONFIG, 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')
        }
    }
}

你可能感兴趣的:(gradle中使用mybatis-generator自动生成工具)