mybatis-generator使用配置

添加依赖,配置mybatis-generator

build.gradle

// 配置阿里云的源
buildscript {
    repositories {
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public"
        }
    }
    // 构建时候的一些依赖
    dependencies {
    }
}
// 编辑打包jar包时需要用到的依赖
plugins {
//    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'java'
//    // lombok使用打包时需加上这个 否则打包时候会报无法找到 getter 等一些方法
    id("io.freefair.lombok") version "3.1.4"
}
jar.enabled=true
group = 'com.drink'
version = '1.0'
sourceCompatibility = '1.8'
apply plugin: 'java'
//1、添加
configurations {
    mybatisGenerator
}

dependencies {
    // springboot 的基本依赖
    compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.1.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.1.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-solr', version: '2.1.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '2.1.4.RELEASE'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    compile group: 'org.mybatis', name: 'mybatis', version: '3.4.1'
    compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version: '1.3.5'
    mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.2'
    mybatisGenerator 'mysql:mysql-connector-java:5.1.21'
    mybatisGenerator 'tk.mybatis:mapper:3.3.1'
    // mysql
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.47'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

//3、添加任务
def getDbProperties = {
    def properties = new Properties()
    file("src/main/resources/mybatis/db-mysql.properties").withInputStream { inputStream ->
        properties.load(inputStream)
    }
    properties;
}

// 生成mapper
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'] = 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: "${rootDir}/drink-model/src/main/resources/mybatis/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')
        }
    }
}

再resource下添加配置文件

db-mysql.properties

# JDBC 驱动类名

# JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名
jdbc.url=jdbc:mysql://localhost:8889/bar-local
# JDBC 用户名及密码
jdbc.user=root
jdbc.pass=root
jdbc.driverClassName=com.mysql.jdbc.Driver
# 生成实体类所在的包
package.model=com.drink.model
# 生成 mapper 类所在的包
package.mapper=com.drink.model.mapper
# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=mapper

generatorConfig.xml




    
        
        
        
        
        
        
        
        
        
        







        
            
        

        
            
            
        

        
        
        
        
        
        

你可能感兴趣的:(mybatis-generator使用配置)