IDEA使用mybatis generator自動生成代碼


主要就三步:

1、pom 文件中引入jar包並配置 build 屬性

    <dependencies>
  <!-- 自動生產mapper Begin! -->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.34</version>
  </dependency>
    <!-- 自動生產mapper End! -->
    </dependencies>
 
 
<build>
  <plugins>
   <!-- 自動生產mapper Begin! -->
   <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
    <configuration>
     <verbose>true</verbose>
     <overwrite>true</overwrite>
    </configuration>
   </plugin>
   <!-- 自動生產mapper End! -->
  </plugins>
</build>

2、代碼中配置 generatorConfig.xml 文件,放入resource根目錄下

具體說明可以參考注釋.

<?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>
    <!-- 配置mysql 驅動jar包路徑.用了絕對路徑 -->
    <classPathEntry location="c:/Users/panghaichen/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar" />
 
 
    <context id="wangyongzhi_mysql_tables" targetRuntime="MyBatis3">
        <!-- 防止生成的代碼中有很多注釋,使用如下配置;想自動生成注釋,則使用后面的配置。 -->
<!--        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>-->
 
        <commentGenerator>
            <!--  suppressAllComments設置為false,addRemarkComments設置為true會將數據庫中的注釋自動添加過來。雖然很多廢棄說明,但是可以使用正則替換並刪除空行,整體來說還是很方便的,具體使用見后續-->
            <property name="suppressAllComments" value="false" />
            <property name="suppressDate" value="false" />
            <property name="addRemarkComments" value="true" />
        </commentGenerator>
 
 
        <!-- 數據庫連接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://10.248.224.12:21202/dmall_virtual_business?useUnicode=true&amp;characterEncoding=UTF-8"
                        userId="marketing"
                        password="3KLPHtdmKAwtA18">
        </jdbcConnection>
 
 
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
 
 
        <!-- 數據表對應的model層輸出目錄:DO實體類  -->
        <javaModelGenerator targetPackage="main.java.com.dmall.virtual.dao.auto" targetProject="src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
 
 
        <!-- sql mapper 映射配置文件輸出目錄:XML文件 -->
        <sqlMapGenerator targetPackage="main.java.com.dmall.virtual.dao.auto"  targetProject="src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
 
 
        <!-- mybatis3中的mapper接口輸出目錄:DAO接口類 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="main.java.com.dmall.virtual.dao.auto"  targetProject="src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
 
 
        <!-- 數據表進行生成操作 schema:相當於庫名; tableName:表名; domainObjectName:對應的DO類名-->
        <table schema="dmall_virtual_business" tableName="denomination_manage" domainObjectName="DenominationManage"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
 
 
        <table schema="dmall_virtual_business" tableName="denomination_manage_log" domainObjectName="DenominationManageLog"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

3、運行插件生成代碼

4、清除自動生成注釋中的廢棄代碼

  1. 在實體類文件中,用正則替換,將包含廢棄說明的行替換為空白;
    正則表達式:^.* 大師兄.*$。將大師兄替換為需要的字符串即可。
  2. 刪除自動生成的get、set方法;
  3. 使用Ctrl + Alt +L自動格式化代碼。然后上述步驟中替換生成的空白行會被自動刪除。
  4. 使用 Alt +Insert自動生成get、set方法

上述步驟絕對比你為每個字段添加注釋要方便(如果字段太多的話)。

5、參考:

  1. Maven 配置之 MyBatis Generator Maven Plugin (三步學會使用 MyBatis Generator 生成代碼)- 四個空格
  2. MyBatis Generator Core – Introduction to MyBatis Generator
    注:官網,及介紹。詳細信息可以自行觀看。
  3. MyBatis Generator Core – MyBatis Generator XML Configuration File Reference
    注:XML文件全部配置屬性可參考以上。有一個中文可參考:Mybatis Generator 最完整配置詳解 - 簡書
  4. Intellij IDEA 中使用 MyBatis-generator 自動生成 MyBatis 代碼 - 志哥的成長筆記 - SegmentFault 思否


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM