1. 程式人生 > >Intellij IDEA 14中使用MyBatis generator 自動生成MyBatis程式碼

Intellij IDEA 14中使用MyBatis generator 自動生成MyBatis程式碼

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

Intellij IDEA 14 作為Java IDE 神器,接觸後發現,非常好用,對它愛不釋手,打算離開eclipse和myeclipse,投入Intellij IDEA的懷抱。

     然而在使用的過程中會發現Intellij IDEA也有一些不盡如意的地方,難免會有些不爽:Intellij IDEA 的外掛庫遠不及eclipse的豐富。 mybatis-generator在eclipse中有專門的外掛,而沒有開發出Intellij IDEA能夠使用的外掛。

    不過不用灰心,如果你的專案是使用maven組織的,那麼我們可以在Intellij IDEA中使用 mybatis-generator-maven-plugin外掛來完成MyBatis model 和Mapper檔案的自動生成。


STEP 0.在Intellij IDEA建立maven專案(本過程比較簡單,略)


STEP 1. 在maven專案的pom.xml 新增mybatis-generator-maven-plugin 外掛

[html]  view plain
copy 在CODE上檢視程式碼片 派生到我的程式碼片
  1. <build>  
  2.   <finalName>xxx</finalName>  
  3.   <plugins>  
  4.     <plugin>  
  5.       <groupId
    >org.mybatis.generator</groupId>  
  6.       <artifactId>mybatis-generator-maven-plugin</artifactId>  
  7.       <version>1.3.2</version>  
  8.       <configuration>  
  9.         <verbose>true</verbose>  
  10.         <overwrite>true</overwrite>  
  11.       </configuration>  
  12.     </plugin>  
  13.   </plugins>  
  14. </build>  


STEP 2. 在maven專案下的src/main/resources 目錄下建立名為 generatorConfig.xml的配置檔案,作為mybatis-generator-maven-plugin 外掛的執行目標,模板如下:


[html]  view plain copy 在CODE上檢視程式碼片 派生到我的程式碼片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE generatorConfiguration  
  3.         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  4.         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
  5. <generatorConfiguration>  
  6.     <!--匯入屬性配置 -->  
  7.     <properties resource="generator.properties"></properties>  
  8.   
  9.     <!--指定特定資料庫的jdbc驅動jar包的位置 -->  
  10.     <classPathEntry location="${jdbc.driverLocation}"/>  
  11.   
  12.     <context id="default" targetRuntime="MyBatis3">  
  13.   
  14.   
  15.         <!-- optional,旨在建立class時,對註釋進行控制 -->  
  16.         <commentGenerator>  
  17.             <property name="suppressDate" value="true" />  
  18.         </commentGenerator>  
  19.   
  20.   
  21.         <!--jdbc的資料庫連線 -->  
  22.         <