1. 程式人生 > >IDEA中使用逆向工程

IDEA中使用逆向工程

1.建立一個名為mybatis-generator的maven工程

2.pom檔案中新增逆向工程外掛和ibats jar包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ryl</groupId>
    <artifactId>mybatis-generator</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.ibatis</groupId>
            <artifactId>ibatis-core</artifactId>
            <version>3.0</version>
        </dependency>
    </dependencies>

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


</project>

3.resource目錄下建立generator.properties和generatorConfig.xml

generator.properties:

mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://xx.xx.xx.xx/xx?useUnicode=true&amp;characterEncoding=UTF-8
mysql.username=xxx
mysql.password=xxx

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:資料庫的JDBC驅動的jar包地址 -->
    <classPathEntry
            location="D:\maven-repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">

        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />
        <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
            <property name="searchString" value="Example$" />
            <property name="replaceString" value="QueryHelper" />
        </plugin>

        <commentGenerator>
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="javaFileEncoding" value="UTF-8" />
            <property name="suppressAllComments" value="true" />
            <!--資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 -->
        </commentGenerator>
        <!--資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://xx.xx.xx.xx/treasure?useUnicode=true&amp;characterEncoding=UTF-8"
                        userId="xxx" password="xxx">
        </jdbcConnection>

        <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer true,把JDBC DECIMAL 和
            NUMERIC 型別解析為java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="true" />
        </javaTypeResolver>
        <!-- targetProject:自動生成程式碼的位置 -->
        <javaModelGenerator targetPackage="com.jiabian.beans.basic"
                            targetProject="D:\ideaProjects\treasure-house\parent\dao\src\main\java">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->
            <property name="enableSubPackages" value="true" />
            <!-- 從資料庫返回的值被清理前後的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.jiabian.dao.basic.generated"
                         targetProject="D:\ideaProjects\treasure-house\parent\dao\src\main\java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.jiabian.dao.basic"
                             targetProject="D:\ideaProjects\treasure-house\parent\dao\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- tableName:用於自動生成程式碼的資料庫表;domainObjectName:對應於資料庫表的javaBean類名 -->

        <table catalog="" tableName="" domainObjectName="">
            <property name="useActualColumnNames" value="true" />
            <generatedKey type="post" identity="true" column="id"
                          sqlStatement="Mysql" />
        </table>
    </context>

</generatorConfiguration>

如圖:

4.右上角點選 Edit Configurations...

5.選擇maven來執行mybatis-generator-maven-plugin外掛

6.進行相關配置

7.配置完畢後就可以運行了。以下兩個地方都可以執行

8.自動生成後結果如下: