1. 程式人生 > >在IDEA中使用MyBatis Generator逆向工程生成程式碼 實踐

在IDEA中使用MyBatis Generator逆向工程生成程式碼 實踐

第一步:在pom檔案下加入

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency
>
<groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> </dependencies> <configuration
>
<overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin>

第二步:Maven的專案配置檔案存放路徑如下圖
這裡寫圖片描述

第三步:建立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> <context id="MySql2Tables" targetRuntime="MyBatis3"> <property name="mergeable" value="false"/> <!-- http://www.mybatis.org/generator/reference/plugins.html --> <plugin type="org.mybatis.generator.plugins.CachePlugin"> <property name="cache_flushInterval" value="20000"/> </plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <!--<plugin type="com.yingu.account.structure.common.utils.MySQLLimitPlugin"/>--> <!--<plugin type="com.yingu.account.structure.common.utils.BatchInsertPlugin"/>--> <!--<plugin type="com.yingu.account.structure.common.utils.BatchUpdatePlugin"/>--> <!--去除註釋 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- &lt;!&ndash;匯入屬性配置&ndash;&gt; <properties resource="generator.properties"></properties> &lt;!&ndash;指定特定資料庫的jdbc驅動jar包的位置&ndash;&gt; <classPathEntry location="${jdbc.driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> &lt;!&ndash; optional,旨在建立class時,對註釋進行控制 &ndash;&gt; <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> &lt;!&ndash;jdbc的資料庫連線 &ndash;&gt; <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> </jdbcConnection>--> <!--資料庫連線 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/account_dev" userId="root" password=""> </jdbcConnection> <!-- 非必需,型別處理器,在資料庫型別和java型別之間的轉換控制--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類 targetPackage 指定生成的model生成所在的包名 targetProject 指定在該專案下所在的路徑 --> <javaModelGenerator targetPackage="com.example.demo.persistence.entity" targetProject="./src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--Mapper對映檔案生成所在的目錄 為每一個數據庫的表生成對應的SqlMap檔案 --> <sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 客戶端程式碼,生成易於使用的針對Model物件和XML配置檔案 的程式碼 type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper物件 type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper物件 type="XMLMAPPER",生成SQLMap XML檔案和獨立的Mapper介面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.persistence.mapper" targetProject="./src/main/java" > <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成實體類 <columnOverride> 將資料庫中的欄位重新命名為實體類的屬性 column 資料庫中欄位名 property POJO屬性名 javaType POJO型別 jdbcType 資料庫欄位型別 --> <table tableName="user_account_entity" domainObjectName="UserAccountEntity"> <columnOverride column="disabled" javaType="boolean" jdbcType="TINYINT"/> <columnOverride column="activated" javaType="boolean" jdbcType="TINYINT"/> <columnOverride column="version" javaType="int"/> </table> <table tableName="business_entity" domainObjectName="BusinessEntity"> <columnOverride column="verified" javaType="int"/> <columnOverride column="version" javaType="int"/> </table> <table tableName="business_operation_history_entity" domainObjectName="BusinessOperationHistoryEntity"> <columnOverride column="version" javaType="int"/> </table> <table tableName="customer_entity" domainObjectName="CustomerEntity"> <columnOverride column="version" javaType="int"/> </table> <table tableName="customer_business_entity" domainObjectName="CustomerBusinessEntity"> <columnOverride column="business_available" javaType="boolean" jdbcType="TINYINT"/> <columnOverride column="version" javaType="int"/> </table> </context> </generatorConfiguration>

第四步:匯入資料
這裡寫圖片描述