Gradle中配置MyBatis Generator生成映射文件以及映射接口

简述:gradle管理项目时如果我们需要使用Mybatis Generator自动生成代码插件时可以调用ant任务生成映射文件以及映射接口

---------------------------------------------------------------------------

步骤一:

build.gradle文件中加入如下配置:

configurations {//新建功能插件配置名称

mybatisGenerator

}

/** Mybatis model mapper 生成工具 */
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.2'

mybatisGenerator 'tk.mybatis:mapper:3.3.2'

/** 读取config文件夹中对应的配置文件 */
ext{
def prop = new Properties();
file("src/main/resources/mybatis/generator/jdbc-mysql.properties").withInputStream {prop.load(it) }
file("src/main/resources/mybatis/generator/gradle.properties").withInputStream { prop.load(it) }
prop.each {
project.extensions.add("$it.key",it.value)
}
}


/** 利用ant创建一个自动生成MyBatis目录文件的task*/
task mybatisGenerate << {
ant.properties['targetProject'] = projectDir.path
ant.properties['jarDirection'] = project['jdbc.jarDirection']
ant.properties['driverClass'] = project['jdbc.driverClassName']
ant.properties['connectionURL'] = project['jdbc.url']
ant.properties['userId'] = project['jdbc.user']
ant.properties['password'] = project['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'] = project['modelPackage']
ant.properties['mapperPackage'] = project['mapperPackage']
ant.properties['sqlMapperPackage'] = project['sqlMapperPackage']


ant.taskdef(
name: 'mbgenerator',
classname: 'org.mybatis.generator.ant.GeneratorAntTask',
classpath: configurations.mybatisGenerator.asPath
)


ant.mbgenerator(overwrite: true,
configfile: 'src/main/resources/mybatis/generator/generatorConfig.xml',
verbose: true) {
propertyset {
propertyref(name: 'targetProject')
propertyref(name: 'jarDirection')
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')
}
}
}

---------------------------------------------------------------------------

步骤二:

springboot项目resources路径下新建文件夹并放入generatorConfig.xml,gradle.properties,jdbc-mysql.properties三个文件如图所示


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>
    <classPathEntry location="${jarDirection}"/>


    <context id="mysql" targetRuntime="MyBatis3Simple">


        <!-- 生成的pojo,将implements Serializable -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>


        <commentGenerator>
            <!-- 注释生成器(false 开启默认 true关闭) -->
            <property name="suppressAllComments" value="true"></property>
            <!-- 注释生成器时间显示(false 开启默认 true关闭) -->
            <property name="suppressDate" value="true"></property>
            <!-- 指定生成的java文件的编码,没有直接生成到项目时中文可能会乱码 -->
            <property name="javaFileEncoding" value="utf-8"/>
            <!-- 格式化java代码 -->
            <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
            <!-- 格式化XML代码 -->
            <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>


        </commentGenerator>


        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}"
                        userId="${userId}"
                        password="${password}">
        </jdbcConnection>


        <!-- 定义java类型解析器的属性 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- 生成实体类配置 -->
        <javaModelGenerator
                targetPackage="${modelPackage}"
                targetProject="${src_main_java}">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>


        <!-- 生成映射文件配置 xml -->
        <sqlMapGenerator
                targetPackage="${sqlMapperPackage}"
                targetProject="${src_main_resources}">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>


        <!-- 生成映射接口配置 mapper -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="${mapperPackage}"
                             targetProject="${src_main_java}">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>


        <!--&lt;!&ndash; 每一个数据库表都需要建立一个对应的table字段 &ndash;&gt;-->
        <!--<table schema="dev_wisdom_nu_system_1_0" tableName="sys" domainObjectName="ursery"-->
        <!--enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"-->
        <!--enableSelectByExample="false" selectByExampleQueryId="false">-->
        <!--<generatedKey column="id" sqlStatement="Mysql" identity="true"/>-->
        <!--</table>-->


        <!-- 每一个数据库表都需要建立一个对应的table字段 -->
        <table schema="dev_wisdom_as_1_0" tableName="recipe" domainObjectName="ipe"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
        </table>


    </context>

</generatorConfiguration>

此时generatorConfig.xml文件配置完成

gradle.properties文件配置内容如下:

#生成代码所在包位置配置
modelPackage=com.aorise.model.recipe
#生成的mapper接口类所在包
mapperPackage=com.aorise.mapper.recipe
#生成的mapper xml文件所在包

sqlMapperPackage=mybatis.mapper.recipe

此时gradle.properties文件配置完成

jdbc-mysql.properties数据库配置文件内容如下:

# 将jdbc.jarDirection后的内容更改为gradle下载的
# mysql-connector-java-5.1.x.jar所在的路径,gradle自动下载的路径如下所示
jdbc.jarDirection=C:/Users/Admin/.gradle/caches/modules-2/files-2.1/mysql/mysql-connector-java/5.1.40/ef2a2ceab1735eaaae0b5d1cccf574fb7c6e1c52/mysql-connector-java-5.1.40.jar        //数据库驱动包所在路径
jdbc.driverClassName=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/dev_wisdom_nursery_system_1_0
jdbc.url=jdbc:mysql://localhost:3306/dev_wisdom_nursery_as_1_0
jdbc.user=root

jdbc.pass=123456

步骤三:

使用gradle  mybatisGenerate指令生成映射文件或者直接在idea的gradle管理工具界面下点击配置按钮生成映射文件


配置完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值