1. 程式人生 > >(八)Intellij mybatis 外掛mybatis-generator 使用

(八)Intellij mybatis 外掛mybatis-generator 使用

Eclipse中可以可以使用mybatis外掛自動生成實體類、以及mybatis 的xml檔案,在intellij中同樣適用。

1、在resources資料夾下建立檔案generatorConfig.xml,程式碼如下所示:

<span style="font-family:SimSun;font-size:14px;"><?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>
   
    <properties resource="generator.properties"></properties>

     <classPathEntry location="${jdbc.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">
   
        <commentGenerator>
            <property name="suppressDate" value="true" />
        </commentGenerator>
     
        <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}">
        </jdbcConnection>
      
        <javaModelGenerator targetPackage="hf.generateEntity" targetProject="src/main/java">
             <property name="constructorBased" value="true"/>
              
            <property name="immutable" value="true"/>
            
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
      
        <sqlMapGenerator targetPackage="hf.generateSqlXml" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
     
            <javaClientGenerator targetPackage="hf.generateEntity" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value=""/>
           
            <property name="exampleMethodVisibility" value=""/>
           
            <property name="methodNameCalculator" value=""/>

            <property name="rootInterface" value=""/>

        </javaClientGenerator>


        <!-- tableName 為表名. schema 為資料庫名稱-->
        <table tableName="audition_author" schema="bookbar">
                   
            <generatedKey column="author_id" sqlStatement="MySql" identity="true" />

        </table>
    </context>
</generatorConfiguration></span>

上面的xml看著可能有些亂,不過複製貼上到自己的編輯器中就好些,下面逐一解釋。

<classPathEntry>

驅動檔案制定配置項,配置驅動檔案所在的位置:

例如:<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />

<context> 

主標籤,下面可以包含其他標籤,targetRuntime="MyBatis3",預設是mybatis3,可以填選值為MyBatis3,MyBatis3Simple(預設的),Ibatis2Java2,Ibatis2Java5

<span style="font-family:SimSun;font-size:14px;"><context id="DB2Tables" targetRuntime="MyBatis3">
    ...
</context></span>
<commentGenerator>

程式碼上的註釋規則

屬性:properties

name為suppressAllComments   value 為false時開啟註釋,true時為關閉註釋。

name為suppressDate  false開啟時間標誌,為true時關閉。

<span style="font-family:SimSun;font-size:14px;"><commentGenerator>
  <property name="suppressDate" value="true" />
</commentGenerator></span>
<jdbcConnection>

db連線相關配置

<span style="font-family:SimSun;font-size:14px;"><jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
    connectionURL="jdbc:db2:MBGTEST"
    userId="db2admin"
    password="db2admin">
</jdbcConnection></span>

上述示例中採用的是在配置檔案中配置

<javaTypeResolver>

mybatis專門用來處理numeric和decimal 型別的策略。

<span style="font-family:SimSun;font-size:14px;"><javaTypeResolver>
  <property name="forceBigDecimals" value="true" />
</javaTypeResolver></span>
<javaModelGenerator>

實體類生成配置

<span style="font-family:SimSun;font-size:14px;"><javaModelGenerator targetPackage="domain"
            targetProject="app">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="false" />
        </javaModelGenerator>
<sqlMapGenerator>

</span>
在mybatis2中是必須的,在mybatis3中,只有xml方式時需要。
<span style="font-family:SimSun;font-size:14px;"><pre name="code" class="html"><sqlMapGenerator targetPackage="test.model"
     targetProject="\MyProject\src">
  <property name="enableSubPackages" value="true" />
</sqlMapGenerator></span>

targetProject是生成實體所在的資料夾位置,一個是是否允許有子程式包。
javaClientGenerator<
<span style="font-family:SimSun;font-size:14px;"><javaClientGenerator type="XMLMAPPER"
            targetPackage="dao.mapper" targetProject="app">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator></span>

客戶端程式碼,生成實體類

targetPackage:生成實體類的包位置,targetProject:原始碼位置。

type=“ANNOTATEDMAPPER” 生成java Model和基於註解的Mapper物件。

type=“MIXEDMAPPER” 生成基於註解的java Model 和相應的Mapper物件。

type="XMLMAPPER" ,生成SQL Map xml檔案和獨立的Mapper介面。

<table>資料庫相關配置

tableName:資料庫表名稱,schema 為資料庫名稱。

<generatedKey>

是否在插入的時候生成主鍵,column 為主鍵名稱,sqlStatement  MySql,則為生成mysql的主鍵方式,identity是否是自增主鍵。

<ignoreColumn>

忽略的欄位

<span style="font-family:SimSun;font-size:14px;"><table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
      <ignoreColumn column="FRED" />
      ..
    </table>

</span>

<columnRenamingRule>
按規則將資料庫中的欄位重新命名為實體類的屬性.
<span style="font-family:SimSun;font-size:14px;"><table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
  <columnRenamingRule searchString="^CUST_" replaceString="" />
  ..
</table></span>

<columnOverride>
將資料庫中的欄位重新命名為實體類的屬性.
column:資料庫中欄位名.
property: pojo屬性名
javaType:pojo型別
jdbcType:資料庫欄位型別.
<span style="font-family:SimSun;font-size:14px;"><table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
      <property name="useActualColumnNames" value="true"/>
      <generatedKey column="ID" sqlStatement="DB2" identity="true" />
      <columnOverride column="DATE_FIELD" property="startDate" />
      <ignoreColumn column="FRED" />
      <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
    </table></span>
2 在maven專案的pom中新增如下外掛
<span style="font-family:SimSun;font-size:14px;"><span style="font-family:SimSun;font-size:18px;">  <build>
    <finalName>xxx</finalName>
    <plugins>
      <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>
    </plugins>
  </build></span></span>
3 採用讀取配置檔案讀取相關配置,配置檔案generator.properties 配置如下.(ps:我用的是mysql的資料庫)
<span style="font-family:SimSun;font-size:14px;">#jdbc.driverLocation=D:\\maven\\com\\oracle\\ojdbc14\\10.2.0.4.0\\ojdbc14-10.2.0.4.0.jar

jdbc.driverLocation=D:\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.9\\mysql-connector-java-5.1.9.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://12.233.221.***:3306/dbName?useUnicode=true&characterEncoding=UTF-8
jdbc.userId=root
jdbc.password=root</span>
4 新增外掛的執行命令


在Runner中最好新增引數VM Options:    -Dmaven.multiModuleProjectDirectory=E:\project_github\j2ee-demo\mybatis-generator

然後執行run或者debug都可以,如果一次沒有成功,一定是generatorConfig.xml的配置檔案中某些配置配置錯了,根據錯誤提示改動,或者先註釋掉某些配置,執行成功後再看這些配置的作用。

生成如下所示:


ps:intellij外掛iBATIS/MyBatis mini-plugin 可以讓我們從dao層直接直接點選到對應的xml,很方便快捷.

總結:

天下武功,無堅不破,唯快不破。多想想天天讓自己重複做的事情,哪些是每天必須用滑鼠點啊點的,使用怎樣更快捷的方式來完成呢?你可能費了點時間來查詢一些快捷鍵,或者一種更快的方式,但是點點滴滴的快起來,你的劍會更加鋒利。出招吧!O(∩_∩)O~