eclipse安裝mybatis-generator

gxlineji發表於2016-09-01
利用mybatis-generator可以自動生成程式碼
mybatis-generator的安裝方法有很多種:

MyBatis Generator (MBG) can be run in the following ways:

  • From the  with an XML configuration
  • As an  with an XML configuration
  • As a 
  • From another  with an XML configuration
  • From another  with a Java based configuration
  • As an 
本文介紹eclipse外掛、maven外掛的安裝辦法。
官方文件:


一 、eclipse外掛 方式 
eclipse外掛:
(1)eclipse help -->eclipse marketplace -->輸入:mybatis-generator搜尋外掛

(2) generatorConfig.xml配置檔案
在src/main/resources下建立generatorConfig.xml檔案,內容如下:

點選(此處)摺疊或開啟

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "">
  3. <generatorConfiguration>
  4.   <!--資料庫驅動-->
  5.   <classPathEntry location="D:\\app\java\apache-maven-3.3.9\repo\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"/>
  6.   <context id="context1">
  7.     <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  8.                         connectionURL="jdbc:mysql://10.50.8.19:3306/test" userId="root"
  9.                         password="biostime123"/>
  10.     <javaModelGenerator targetPackage="com.jiang.mavenSSM.entity"
  11.                             targetProject="mavenSSM">
  12.             <property name="enableSubPackages" value="true"/>
  13.             <property name="trimStrings" value="true"/>
  14.     </javaModelGenerator>
  15.     <sqlMapGenerator targetPackage="com.jiang.mavenSSM.entity.xml"
  16.                          targetProject="mavenSSM">
  17.             <property name="enableSubPackages" value="true"/>
  18.         </sqlMapGenerator>
  19.     <javaClientGenerator targetPackage="com.jiang.mavenSSM.mapper"
  20.                              targetProject="mavenSSM" type="XMLMAPPER">
  21.             <property name="enableSubPackages" value="true"/>
  22.         </javaClientGenerator>


  23.     <table tableName="user_t" domainObjectName="FeeBase"
  24.                enableCountByExample="false" enableUpdateByExample="false"
  25.                enableDeleteByExample="false" enableSelectByExample="false"
  26.                selectByExampleQueryId="false">
  27.             <!--<columnRenamingRule searchString="^D_"
  28.                                 replaceString=""/>-->
  29.             <!--<columnOverride column="???" property="???" /> -->
  30.         </table>
  31.   </context>
  32. </generatorConfiguration>
注意:mybatis-generator以eclipse外掛方式安裝時,generatorConfig.xml配置檔案裡的targetProject="mavenSSM"為專案名稱
 其中generatorConfig.xml的位置,大家根據實際情況自行調整

(3)自動生成檔案
如果是在eclipse 中,選擇generatorConfig.xml檔案,擊右鍵先擇"Generator MyBatis/iBATIS  Artifacts" 

二 、maven外掛 方式 
maven外掛安裝請參考我的另一篇文章
(1)下載mybatis-generator
下載地址:

下載後解壓並把lib目錄下的Jar包,複製到MAVEN安裝路徑的lib目錄下,我的MAVEN安裝目錄為 D:\app\java\apache-maven-3.3.9\lib

(2)在pom.xml中新增plugin

點選(此處)摺疊或開啟

  1. <build>
  2. ……
  3. <plugins>
  4. ……
  5. <plugin>
  6.     <groupId>org.mybatis.generator</groupId>
  7.     <artifactId>mybatis-generator-maven-plugin</artifactId>
  8.     <version>1.3.4</version>
  9.     <configuration>
  10.         <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
  11.         <verbose>true</verbose>
  12.         <overwrite>true</overwrite>
  13.     </configuration>
  14.     <executions>
  15.         <execution>
  16.             <id>Generate MyBatis Artifacts</id>
  17.             <goals>
  18.                 <goal>generate</goal>
  19.             </goals>
  20.         </execution>
  21.     </executions>
  22.     <dependencies>
  23.         <dependency>
  24.             <groupId>org.mybatis.generator</groupId>
  25.             <artifactId>mybatis-generator-core</artifactId>
  26.             <version>1.3.4</version>
  27.         </dependency>
  28.     </dependencies>
  29. </plugin>
  30. </plugins>
  31. </build>

(3) 建立generatorConfig.xml檔案
generatorConfig.xml檔案內容,除了targetProject標籤的值不同外,其它與eclipse外掛方式的generatorConfig.xml一樣,

targetProject改為絕對路徑:
targetProject="D:\app\java\eclipse\workspace\mavenSSM\src\main\java"

(4)自動生成檔案
如果是在eclipse 中,選擇generatorConfig.xml檔案,擊右鍵先擇Run AS——>Maven Build… ——>在Goals框中輸入:mybatis-generator:generate 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10995764/viewspace-2124352/,如需轉載,請註明出處,否則將追究法律責任。

相關文章