用Maven中实现MyBatis逆向工程(IDEA版)

用Maven中实现MyBatis逆向工程(IDEA版)

MyBatis逆向工程介绍

MyBatis逆向工程是指用数据库的表直接生成Java代码,利用MyBatis官方提供的逆向工程,可以针对单表自动生MyBatis执行所需要的代码(如pojo,mapper接口和mapper.xml)。

首先

使用idea中的Maven生成MyBatis所需要的mapper类和xml文件,非常方便。

首先在IDEA中要有MyBatis的这款插件,它为idea提供mybatis的xml的模版文件、接口类与xml文件对应导航。

安装成功后,在maven的resources文件右击使用mybatis-generator-config模版文件,

内容如下:

    <?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>

        <!--数据库驱动jar包的真实路径 -->
        <classPathEntry
                location="C:\Users\Hoictas\.m2\repository\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar"/>

        <context id="context" targetRuntime="MyBatis3">
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <commentGenerator>
                <property name="suppressAllComments" value="false"/>
                <property name="suppressDate" value="true"/>
            </commentGenerator>

            <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/db_wechat"
                            userId="tk"
                            password="tk"/>
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
                NUMERIC 类型解析为java.math.BigDecimal -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>

            <!--指定包名生成实体类 以及生成的地址 (可以自定义地址,如果路径不存在会自动创建) -->
            <javaModelGenerator targetPackage="com.mybatis.demo.entity" targetProject=".\src\main\java">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false"/>
                <!-- 从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>

            <!-- !!!! Mapper XML Configurations !!!! -->
            <sqlMapGenerator targetPackage="com.mybatis.demo.mapper.impl" targetProject=".\src\main\java">
                <property name="enableSubPackages" value="false"/>
            </sqlMapGenerator>

            <!-- !!!! Mapper Interface Configurations !!!! -->
            <javaClientGenerator targetPackage="com.mybatis.demo.mapper" targetProject=".\src\main\java"
                                 type="XMLMAPPER">
                <property name="enableSubPackages" value="false"/>
            </javaClientGenerator>

            <!-- 指定数据库表 -->
            <table schema="db_wechat" tableName="t_commandcontent"
                   enableCountByExample="false" enableDeleteByExample="false"
                   enableSelectByExample="false" enableUpdateByExample="false"/>
            <table schema="db_wechat" tableName="t_automessage"
                   enableCountByExample="false" enableDeleteByExample="false"
                   enableSelectByExample="false" enableUpdateByExample="false"/>
            <table schema="db_wechat" tableName="t_command"
                   enableCountByExample="false" enableDeleteByExample="false"
                   enableSelectByExample="false" enableUpdateByExample="false"/>
        </context>
    </generatorConfiguration>

配置简单,没什么难度,再把我的项目结构比对上面的代码,你就知道该怎么根据自己的项目来配置文件。

最后点击右侧的maven project 找到mybatis-generator运行即可。

2017/11/30 19:41:19

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值