java逆向工程-mybatis-generator

題記:在快速開發的項目中有使用到,這樣能夠避免冗餘工做html

聲明:參考於http://www.javashuo.com/article/p-otedbjot-cu.htmljava

環境:必須先安裝maven環境,不然沒法實現哦!mysql

mybatis和mysql驅動包下載:https://pan.baidu.com/s/1fE83MJQUPMb4OSU__jm3qwspring

方式一(命令行執行):sql

  一、目錄結構數據庫

 

  二、generatorConfig.xml內容springboot

<?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="mysql-connector-java-5.1.30.jar"/>  
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!--數據庫連接URL,用戶名、密碼 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test1" userId="root" password="yang156122">  
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置-->  
        <javaModelGenerator targetPackage="test.model" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!-- 生成映射文件的包名和位置-->  
        <sqlMapGenerator targetPackage="test.mapping" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        <!-- 生成DAO的包名和位置-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
        <!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名-->  
        <table tableName="syspermission" domainObjectName="Syspermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        
        <table tableName="sysrole" domainObjectName="Sysrole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

        <table tableName="user_t" domainObjectName="User_t" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>  
</generatorConfiguration>  

不少博客有不少冗餘信息,我這裏就直接給出一些簡單的信息了,但這些信息絕對是實用的。mybatis

  三、進入到步驟1中的目錄中,執行下面命令:app

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

 

  這樣就實現了逆向工程,能夠將代碼拷貝進項目中。dom

 

方式二(idea工具中實現):

  一、重申:maven須要先配置。首先創建一個springboot工程,在pom.xml中添加如下配置:

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

 

  二、generatorConfig.xml內容,注意,targetPackage是包名,targetProject是路徑名,數據庫驅動是本地的mysql數據鏈接驅動。

<?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="D:/21CN/Generator/mysql-connector-java-5.1.30.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!--數據庫連接URL,用戶名、密碼 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test1" userId="root" password="yang156122">  
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置-->  
        <javaModelGenerator targetPackage="com.test.shiro.entity" targetProject="D:/springboot-shiro2/src/main/java">
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!-- 生成映射文件的包名和位置-->  
        <sqlMapGenerator targetPackage="mapper" targetProject="D:/springboot-shiro2/src/main/resources">
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        <!-- 生成DAO的包名和位置-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.shiro.mapper" targetProject="D:/springboot-shiro2/src/main/java">
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
        <!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名-->  
        <table tableName="syspermission" domainObjectName="Syspermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        
        <table tableName="sysrole" domainObjectName="Sysrole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

        <table tableName="user_t" domainObjectName="User_t" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>  
</generatorConfiguration>  

 

  三、idea中使用maven命令執行

 

這樣,在Idea中就實現了逆向工程了。

親測有效,若是有什麼問題,能夠留言.

相關文章
相關標籤/搜索