Mybatis逆向工程使用

 引入pom依赖:

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.0</version>
        </dependency>

引入pom插件

            <!-- mybatis代码生成插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

配置generator.properties文件

driverLocation是配置数据库连接jar包的地址。这里直接配置
address.project=src/main/java
address.entity=com.lhw.mybatisgenerator.entity
address.mapper=com.lhw.mybatisgenerator.mapper
address.dao=com.lhw.mybatisgenerator.dao

#oracle 测试1
#jdbc.driverLocation=C:\Users\61990\.m2\repository\com\oracle\ojdbc8\12.2.0.1\ojdbc8-12.2.0.1.jar
#jdbc.driver=oracle.jdbc.OracleDriver
#jdbc.url=jdbc:oracle:thin:@192.168.1.190:1521:orcl2
#jdbc.userId=
#jdbc.pwd=

#Mysql 测试1
jdbc.driverLocation=C:\Users\61990\.m2\repository\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.178:3306/zabbix
jdbc.userId=
jdbc.pwd=

配置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>

    <!--加载资源文件-->
    <properties resource="generator.properties"></properties>

    <!--指定特定数据库的jdbc驱动jar包的位置-->
    <classPathEntry location="${jdbc.driverLocation}"/>

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

        <!--数据库连接-->
        <jdbcConnection
                driverClass="${jdbc.driver}"
                connectionURL="${jdbc.url}"
                userId="${jdbc.userId}"
                password="${jdbc.pwd}">
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
<!--targetPackage:生成实体类的位置--> <javaModelGenerator targetPackage="${address.entity}" targetProject="${address.project}"> <!--enableSubPackages,是否让schema作为包的后缀--> <property name="enableSubPackages" value="false"/> <!--从数据库返回的值被清除前后空格--> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--targetProject:mapper映射文件生成的位置--> <sqlMapGenerator targetPackage="${address.mapper}" targetProject="${address.project}"> <property name="enableSubPackages" value="false"></property> </sqlMapGenerator> <!--targetPackage:mapper接口生成的位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="${address.dao}" targetProject="${address.project}"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--指定数据库表,要和数据库中进行对应,否则将会出错--> <!--oracle 190测试表--> <!--<table tableName="UOS_AREA_EXTENDS" domainObjectName="UosAreaExtends"--> <!--enableCountByExample="false" enableUpdateByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--selectByExampleQueryId="false">--> <!--</table>--> <!--mysql 1978测试表--> <table tableName="dhosts" domainObjectName="dhosts" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>

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>

    <!--加载资源文件-->
    <properties resource="generator.properties"></properties>

    <!--指定特定数据库的jdbc驱动jar包的位置-->
    <classPathEntry location="${jdbc.driverLocation}"/>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- POJO类是否自动实现序列化 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        
        <commentGenerator>
            <!--是否去除自动生成的注释 true是:false 否-->
            <property name="suppressAllComments" value="true"/>
            <!--是否去除自动生成的时间 true是:false 否-->
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <!--数据库连接-->
        <jdbcConnection
                driverClass="${jdbc.driver}"
                connectionURL="${jdbc.url}"
                userId="${jdbc.userId}"
                password="${jdbc.pwd}">
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        
        <!--targetPackage:生成实体类的位置-->
        <javaModelGenerator targetPackage="${address.entity}" targetProject="${address.project}">
            <!--enableSubPackages,是否让schema作为包的后缀-->
            <property name="enableSubPackages" value="false"/>
            <!--从数据库返回的值被清除前后空格-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--targetProject:mapper映射文件生成的位置-->
        <sqlMapGenerator targetPackage="${address.mapper}" targetProject="${address.project}">
            <property name="enableSubPackages" value="false"></property>
        </sqlMapGenerator>

        <!--targetPackage:mapper接口生成的位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="${address.dao}" targetProject="${address.project}">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!--指定数据库表,要和数据库中进行对应,否则将会出错-->
        <!--enableCountByExample、
            enableUpdateByExample、
            enableDeleteByExample、
            enableSelectByExample、
            selectByExampleQueryId 代表是否要生成Example文件-->
        <!--oracle 190测试表-->
        <!--<table tableName="UOS_AREA_EXTENDS"  domainObjectName="UosAreaExtends"-->
               <!--enableCountByExample="false" enableUpdateByExample="false"-->
               <!--enableDeleteByExample="false" enableSelectByExample="false"-->
               <!--selectByExampleQueryId="false">-->
        <!--</table>-->
        <!--mysql 178测试表-->
        <table tableName="dhosts"  domainObjectName="dhosts"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <!--生成带有Example的测试表-->
        <table tableName="M_DB_TASK_LOG"  domainObjectName="DbTaskLogBo" ></table>
    </context>
</generatorConfiguration>

使用maven启动

mybatis-generator:generate -e

配置完成后 可以在maven project看到下面这个。启动就可以运行了

启动后看到控制台成功。并且文件成功生成

下载mybatis-Reverse项目: https://github.com/linHongWenGithub/Mybatis-generator.git

原文地址:https://www.cnblogs.com/linhongwenBlog/p/12588238.html