zoukankan      html  css  js  c++  java
  • gradel MyBatis Generator [ant]

    1.指定数据库连接信息、和生成entity及dao所在位置配置

    src/main/resources/mybatis/config.properties
    # JDBC
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://192.*.*.*:*/*?useUnicode=true&characterEncoding=utf-8&useSSL=false
    jdbc.username=root
    jdbc.password=*
    
    # 生成实体类所在的包
    package.model=com.scm.huaqin.codepush.entity
    # 生成 mapper 类所在的包
    package.mapper=com.scm.huaqin.codepush.dao
    # 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
    package.xml=mybatis/mapper

    2.生成使用XML、指定生成方式(那些表等、具体参照官方http://www.mybatis.org/generator/configreference/xmlconfig.html

    src/main/resources/mybatis/generatorConfig.xml
    <?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>
        <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
            <commentGenerator>
                <property name="suppressAllComments" value="true"></property>
                <property name="suppressDate" value="true"></property>
                <property name="javaFileEncoding" value="utf-8"/>
            </commentGenerator>
    
            <jdbcConnection driverClass="${driverClass}"
                            connectionURL="${connectionURL}"
                            userId="${userId}"
                            password="${password}">
            </jdbcConnection>
    
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
    
            <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
                <property name="enableSubPackages" value="true"></property>
                <property name="trimStrings" value="true"></property>
            </javaModelGenerator>
    
            <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}">
                <property name="enableSubPackages" value="true"></property>
            </sqlMapGenerator>
    
            <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="ANNOTATEDMAPPER">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
    
            <!-- sql占位符,表示所有的表 -->
            <table tableName="%">
            </table>
        </context>
    </generatorConfiguration>

    3.在build.gradle中编写ant的task

    build.gradle

    //配置mybatisGenerator在dependencies中使用
    configurations {
        mybatisGenerator
    }
    //引入依赖
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-actuator')
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
        compile('org.springframework.cloud:spring-cloud-starter')
        runtime('org.springframework.boot:spring-boot-devtools')
        runtime('mysql:mysql-connector-java')
        testCompile('org.springframework.boot:spring-boot-starter-test')
    
        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中调用
    def getDbProperties = {
        def properties = new Properties()
        file("src/main/resources/mybatis/config.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: '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')
            }
        }
    }



  • 相关阅读:
    个人项目实战
    准备
    结队项目-第一次作业
    第三次作业--团队展示
    软件工程实践第二次作业
    软件工程实践第一次作业
    circle
    calculator
    视频课程学习及学习计划
    1001.A+B Format (20)
  • 原文地址:https://www.cnblogs.com/black-/p/9517191.html
Copyright © 2011-2022 走看看