maven-mybaties-generator学习笔记整理

MGB可以反向生成对应数据表的实体bean、mapper接口类以及对应的sqlmap.xml,关于该插件的详细介绍可以参考官方介绍

接下来整理一下使用maven管理的SSM框架中如何使用mybaties-generator插件:

1、pom中引入插件

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
        </plugin>

2、配置generatorConfiguration:在resources目录下新建一个xml配置文件,自行命名,主要配置信息如下

<classPathEntry>配置链接数据库的jar路径,本项目路径为maven仓库下的jar路径

<context>配置数据库,有多个数据库可配置多个context

<commentGenerator> suppressAllComments 配置为true时去掉注释,默认是false,会生成大量的注释信息

<table>要反向生成的表,定义库、表名、实体bean名称,*ByExample默认为true,建议配置为false,为true会生成很多无用的sql,有兴趣的可以试下

<?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="D:\tools\maven\repository\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar" />
    <context id="test" targetRuntime="MyBatis3">
        <!-- 注释处理 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true" />
        </commentGenerator>
        <!-- jdbc -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/test"
                        userId="root" password="" />
        <!-- bean -->
        <javaModelGenerator targetPackage="com.dubbo.biz"
            targetProject="D:\project\dubbo-consumer\biz\src\main\java" />
        <!-- mapper.xml -->
        <sqlMapGenerator targetPackage="com.dubbo.mapper"
            targetProject="D:\project\dubbo-consumer\dao\src\main\resources" />
        <!-- dao -->
        <javaClientGenerator targetPackage="com.dubbo.dao"
            targetProject="D:\project\dubbo-consumer\dao\src\main\java" type="XMLMAPPER" />
        <!-- table -->
        <table schema="test" tableName="temp_file_info"
               domainObjectName="TempFileInfo" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false">
        </table>
        <table schema="test" tableName="temp_log"
               domainObjectName="TempLog" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false">
        </table>
    </context>
</generatorConfiguration>
3、配置文件声明好后,执行生成文件:

在插件处直接右键选择Run Maven Build,或者选择该配置文件--> Run Maven --> New Goal --> 输入:mybaties-generator:generate

4、执行以上步骤后即会生成相应的文件,修改之后重新执行即可(会覆盖相同文件,但是已生成的文件不会自动删除)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值