欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-10 21:51:04
自动生成框架的意义 主要为了解决人为添加mapper,模型等工作,减少错误,提交效率! 添加引用build.gradle 添加配置文件 sources/mybaits目录 config.properties generatorConfig.xml文件内容 在build.gradle添加脚本 def ......

自动生成框架的意义

主要为了解决人为添加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/customer_management
# jdbc 用户名及密码
jdbc.username=a
jdbc.password=a123

# 生成实体类所在的包
package.model=cn.customer.entity
# 生成 mapper 类所在的包
package.mapper=cn.customer.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.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,然后点击发布即可。