gradle下mybatis自动生成框架的使用

栏目: Java · 发布时间: 4年前

内容简介:主要为了解决人为添加mapper,模型等工作,减少错误,提交效率!sources/mybaits目录config.properties

自动生成框架的意义

主要为了解决人为添加mapper,模型等工作,减少错误,提交效率!

添加引用build.gradle

configurations {
    mybatisGenerator
}
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5'
   mybatisGenerator 'mysql:mysql-connector-java:5.1.40'
   mybatisGenerator 'tk.mybatis:mapper:3.3.9'

添加配置文件

sources/mybaits目录

config.properties

# JDBC 驱动类名
jdbc.driverClassName=com.mysql.jdbc.Driver
# JDBC URL: jdbc:mysql:// + 数据库主机地址 + :端口号 + /数据库名
jdbc.url=jdbc:mysql://rm-2ze54unnv814ng1wv3o.mysql.rds.aliyuncs.com:3306/pilipa_crm_customer_management
# JDBC 用户名及密码
jdbc.username=pilipa
jdbc.password=Pilipa-2018

# 生成实体类所在的包
package.model=cn.pilipa.customer.management.entity
# 生成 mapper 类所在的包
package.mapper=cn.pilipa.customer.management.mapper
# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=mapper

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">
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="cn.pilipa.customer.management.mapper"/>
            <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
            <property name="caseSensitive" value="true"/>
        </plugin>
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}"
                        userId="${userId}"
                        password="${password}">
        </jdbcConnection>
        <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}"/>
        <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}"/>
        <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER"/>
        <!-- sql占位符,表示所有的表 -->
        <table tableName="%">
            <generatedKey column="epa_id" sqlStatement="Mysql" identity="true" />
        </table>
    </context>
</generatorConfiguration>

在build.gradle添加脚本

def getDbProperties = {
    def properties = new Properties()
    file("src/main/resources/mybatis/jdbc.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')
        }
    }
}

在左侧gradle工具栏里可以找到mybatisGenerate,然后点击发布即可。


以上所述就是小编给大家介绍的《gradle下mybatis自动生成框架的使用》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java 8函数式编程

Java 8函数式编程

[英] Richard Warburton / 王群锋 / 人民邮电出版社 / 2015-3 / 39.00元

通过每一章的练习快速掌握Java 8中的Lambda表达式 分析流、高级集合和其他Java 8类库的改进 利用多核CPU提高数据并发的性能 将现有代码库和库代码Lambda化 学习Lambda表达式单元测试和调试的实践解决方案 用Lambda表达式实现面向对象编程的SOLID原则 编写能有效执行消息传送和非阻塞I/O的并发应用一起来看看 《Java 8函数式编程》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具