Mybatis自动生成代码

来源:互联网 发布:手机mac地址修改软件 编辑:程序博客网 时间:2024/06/08 01:24

Mybatis自动生成代码(dao,mapper,pojo)

 <!--自动生成代码脚本--> <?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">        <property name="javaFileEncoding" value="UTF-8"/>        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>            <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->            <property name="caseSensitive" value="true"/>        </plugin>        <commentGenerator>            <property name="suppressAllComments" value="true"/>        </commentGenerator>        <!--jdbc参数-->        <jdbcConnection driverClass="${driverClass}"                        connectionURL="${connectionURL}"                        userId="${userId}"                        password="${password}">        </jdbcConnection>        <!--dao生成路径-->        <javaModelGenerator targetPackage="${modelPackage}"                                targetProject="${src_main_java}"/>        <!--mybatisxml生成路径-->        <sqlMapGenerator targetPackage="${sqlMapperPackage}"                            targetProject="${src_main_resources}"/>        <!--pojo生成路径-->        <javaClientGenerator targetPackage="${mapperPackage}"                                targetProject="${src_main_java}"                                type="XMLMAPPER"/>        <table tableName="TB_LOG_OPERATE"               domainObjectName="OperateLogs"               enableCountByExample="false"               enableUpdateByExample="false"               enableSelectByExample="false"               selectByExampleQueryId="false">            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>        </table>    </context></generatorConfiguration>

配置文件

env=dev# 需要生成的model存放路径modelPackage=com.asiainfo.etrip.modules.ecar.bean# 生成的mapper接口类所在包mapperPackage=com.asiainfo.etrip.modules.ecar.dao# 生成的mapper xml文件所在包,默认存储在resources目录下sqlMapperPackage=mybatis_mapper

build.gradle 任务配置

// 依赖dependencies {    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'}// jdbc参数def getDbProperties = {    def properties = new Properties()    file("src/main/resources/jdbc-mysql.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.user")    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'] = this.modelPackage    ant.properties['mapperPackage'] = this.mapperPackage    ant.properties['sqlMapperPackage'] = this.sqlMapperPackage    ant.taskdef(            name: 'mbgenerator',            classname: 'org.mybatis.generator.ant.GeneratorAntTask',            classpath: configurations.mybatisGenerator.asPath    )    ant.mbgenerator(overwrite: true,            configfile: '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')        }    }}

执行方式

graph LR在build.gradle文件中右击-->选中run上面配置的任务
0 0