MyBatis逆向工程的使用

通过cmd命令行生成

1.配置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="E:/workspace/unifiedWaiMaiPlatform/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.32.jar"/>
    <context id="my" targetRuntime="MyBatis3">
        <plugin type="org.alex.mybatis.common.plugin.PaginationPlugin"/>
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--配置数据库连接信息  -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"            connectionURL="jdbc:mysql://drds0c163972v37tpublic.drds.aliyuncs.com:3306/business"
                        userId="business"
                        password="dev_55567707"/>

        <!--生成实体对象的位置以及项目的位置-->
        <javaModelGenerator targetPackage="org.hshk.demo.orm.entity"
                            targetProject="E:\workspace\unifiedWaiMaiPlatform\src">
                      <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成Mapper映射XML文件位置以及所在项目的位置 -->
        <sqlMapGenerator targetPackage="org.hshk.demo.orm.mapper"
                         targetProject="E:\workspace\unifiedWaiMaiPlatform\src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成Mapper接口文件位置以及所在项目的位置 -->
        <javaClientGenerator targetPackage="org.hshk.demo.orm.mapper"
                             targetProject="E:\workspace\unifiedWaiMaiPlatform\src"                                                                              type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 要生成哪些表(更改tableName和domainObjectName就可以) -->
        <!-- tableName:要生成的表名  ; domainObjectName:生成后的实例名
        enableCountByExample:Count语句中加入where条件查询,默认为true开启
        enableUpdateByExample:Update语句中加入where条件查询,默认为true开启
        enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
        enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启
        selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启
        -->
       <!--检查好需要生成的表名-->
        <table tableName="saas_order" domainObjectName="SaasOrder"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               selectByExampleQueryId="true" alias="CouponOrder">
        </table>
        <table tableName="saas_food" domainObjectName="SaasFood"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               selectByExampleQueryId="true" alias="CouponOrderMenu">
        </table>
        <table tableName="saas_bill" domainObjectName="saas_bill"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               selectByExampleQueryId="true" alias="CouponRestaurant">
        </table>
    </context>
</generatorConfiguration>

2.CMD中的命令

java -jar ./WebRoot/WEB-INF/lib/mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml

mybatis-generator-core-1.3.2.jar 是mybatis逆向工程的核心jar包;

./WebRoot/WEB-INF/lib/mybatis-generator-core-1.3.2.jar 是在需要生成的项目目录下, 若jar包在其他位置自己进行路径的配置;

-configfile generatorConfig.xml 加载配置文件;

cmd中输入上面的命令,敲击回车后

***E:\workspace\unifiedWaiMaiPlatform>java -jar ./WebRoot/WEB-INF/lib/mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml
MyBatis Generator finished successfully.
E:\workspace\unifiedWaiMaiPlatform>pause
请按任意键继续…*

cmd中出现上面的命令代表生成成功,若生成失败检查下自己的配置文件以及cmd命令;

如果逆向工程多次使用的话,可以将cmd命令生成为一个 .bat文件,windows下双击直接就会运行cmd命令;

3.生成完成

1

生成后的文件

其中,Example文件用于添加条件,相当于sql中where后的部分’

mapper.xml中生成的CRUD,可以满足基本的需求;

查询时,只要熟练运用 xxxExample文件,基本可以满足所有的查询需求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值