mybatis generator 用法

Posted 重八

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis generator 用法相关的知识,希望对你有一定的参考价值。

<?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>
    <!--这里是connector的jar所在的绝对路径-->
    <classPathEntry
            location="/Users/zhenghao/.m2/repository/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar"/>

    <!--id 必填, defaultModelType是Model的类型,flat不会产生Example,Key,WithBLOB等-->
    <context id="my" defaultModelType="flat" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库连接串-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/oms?useSSL=false"
                        userId="root"
                        password="1100131943"/>

        <!--models-->
        <javaModelGenerator targetPackage="com.rxhui.oms.core.domain"
                            targetProject="/Users/zhenghao/git/quant-oms/core/src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--xmlMappers-->
        <sqlMapGenerator targetPackage="mappers"
                         targetProject="/Users/zhenghao/git/quant-oms/core/src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--DAO-->
        <javaClientGenerator targetPackage="com.rxhui.oms.core.dao"
                             targetProject="/Users/zhenghao/git/quant-oms/core/src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>


        <!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"/>-->

        <table tableName="orderEntrust" domainObjectName="OrderEntrust"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <!--<columnRenamingRule searchString="^D_"
                                replaceString=""/>-->
        </table>

        <table tableName="orderCancel" domainObjectName="OrderCancel"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <!--<columnRenamingRule searchString="^D_"
                                replaceString=""/>-->
        </table>

        <table tableName="dealPush" domainObjectName="DealPush"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <!--<columnRenamingRule searchString="^D_"
                                replaceString=""/>-->
        </table>


    </context>
</generatorConfiguration>

如果使用maven,

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis-generator}</version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>${mybatis-generator}</version>
                    </dependency>
                </dependencies>
            </plugin>

 另外,生成的mapper.xml中,insert如果有默认值(如自增序列或创建时间等),需要使用insertSelective才能正确插入默认值。

以上是关于mybatis generator 用法的主要内容,如果未能解决你的问题,请参考以下文章

mybatis-generator 代码自动生成插件

转利用mybatis-generator自动生成代码

MyBatis-Generator 用法介绍

基于mybatis generator 的逆向代码生成器

MyBatis Generator的一些知识点释疑

MyBatis动态SQL标签用法