ibatis代碼自動產生工具ibator修改備忘

來源:互聯網
上載者:User

由於對ibator瞭解的不夠深入,毅然決然的開始了修改ibator外掛程式的過程,修改的過程收穫很大,瞭解了這個外掛程式的諸多提示。

 

1.自動產生的程式碼中的討厭的Example怎麼改名。

   這個也是驅動我去修改ibator plugin的原動力,因為我懶,不想每次產生代碼以後再去替換Example單詞,使用UltraEditor自動把Ibator來源程式中的所有Example都替換了,替換完以後才發現,ibator外掛程式可以自動幫我們替換,配置一個參數就可以了,汗啊。替換內容如下:

 

 <!--
      id  這個id可以在使用命令列運行Abator時指定,以單獨處理某一個ibatorContext
      targetRuntime   Ibatis2Java5 產生適合JDK5.0的類,另一個選項是 Ibatis2Java2,產生適合Java2的類。
    -->
    
        <ibatorPlugin type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin">
            <property name="searchString" value="Example$" />
            <property name="replaceString" value="Criteria" />
        </ibatorPlugin>
        
        <!--
          ibatorPlugin 繼承自IbatorPluginAdapter,包名必須是 org.apache.ibatis.ibator.plugins,具體實現可以參考官方文檔
            必須有替換和被替換字元屬性。
        -->

2.既然動手改了,就把看著不順眼的地方統統的改掉吧,以下是便於日後再想改的時候要看的內容:

 

   代碼注釋在org.apache.ibatis.ibator.internal這個package下的DefaultCommentGenerator.java中,可以把英文的注釋改成中文的,但是sqlmap中的注釋不能直接改中文,因為sqlmap.xml檔案時utf-8編碼的,直接寫入中文注釋會出現亂碼的情況。

 

  在org.apache.ibatis.ibator.config這個package下的MergeConstants.java中包含有代碼註解中的某些單詞,可以考慮修改。

 

3.下面是從別人的部落格轉過來的內容:

 

1、Abator產生Java類檔案時,根據注釋區分屬性和方法是系統產生或使用者自訂,以此決定保留或者覆寫.
 
2、Abator產生SQLMap的xml檔案時,根據元素id是否包含首碼 ibatorgenerated_ 區分元素是系統
      產生或使用者自訂,以此決定保留或者覆寫.。
 
3、注意ibatorConfig.xm 檔案中節點的順序
 
4、產生的資料對象
 
    Primary Key Class 主鍵的所有組成欄位在一個類中
    
    Record Class 非主鍵欄位非BLOB欄位組成的類,繼承於Primary Key Class
    
    Record With BLOBs Class 所有BLOB欄位組成的類,繼承於Record Class (如不存在),就會繼承Primary Key Class
                            不支援只包含BLOB欄位的表。
    
    Example Class    用於產生動態where條件的類
 
5、example Class 使用(可以使用邏輯運算的結合律簡化where條件)
 
代碼:
  TestTableExample example = new TestTableExample();
  example.createCriteria().andField1EqualTo(5);
 
產生條件:
 
  where field1 = 5
 
代碼:
 
  TestTableExample example = new TestTableExample();
 
  example.createCriteria()
    .andField1EqualTo(5)
    .andField2IsNull();
 
  example.or(example.createCriteria()
    .andField3NotEqualTo(9)
    .andField4IsNotNull());
 
  List<Integer> field5Values = new ArrayList<Integer>();
  field5Values.add(8);
  field5Values.add(11);
  field5Values.add(14);
  field5Values.add(22);
 
  example.or(example.createCriteria()
    .andField5In(field5Values));
 
  example.or(example.createCriteria()
    .andField6Between(3, 7));
 
 
產生條件:
 
  where (field1 = 5 and field2 is null)
     or (field3 <> 9 and field4 is not null)
     or (field5 in (8, 11, 14, 22))
     or (field6 between 3 and 7)
    
 
6、ibatorConfig.xm 檔案分析
 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ibatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN" "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd" >
<ibatorConfiguration>
 
    <properties url="file:/home/guo/workspace_google/iBATIS/config/config.properties"/>
    
    <!--
      url     指定屬性檔案絕對路徑。注意與指定資料庫jdbc驅動jar包路徑的區別哈。
                    可以使用${property}的格式引用屬性檔案中的屬性值。
    -->
    
    <classPathEntry location="/home/guo/java/workspace/newbee/lib/ibatis/postgresql-8.3-604.jdbc3.jar" />
    
    <!--
      classPathEntry 指定資料庫jdbc驅動jar包的絕對路徑。
    -->
    
    <ibatorContext id="context1" targetRuntime="Ibatis2Java5">
    
    <!--
      id                 這個id可以在使用命令列運行Abator時指定,以單獨處理某一個ibatorContext
      targetRuntime        Ibatis2Java5 產生適合JDK5.0的類,另一個選項是 Ibatis2Java2,產生適合Java2的類。
    -->
    
        <ibatorPlugin type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin">
            <property name="searchString" value="Example$" />
            <property name="replaceString" value="Criteria" />
        </ibatorPlugin>
        
        <!--
          ibatorPlugin 繼承自IbatorPluginAdapter,包名必須是 org.apache.ibatis.ibator.plugins,具體實現可以參考官方文檔
            必須有替換和被替換字元屬性。
        -->
    
        <jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://192.168.1.2:5432/newbee" userId="sa" password="esoon" />
        
        <!--
          driverClass        資料庫驅動類
          connectionURL        資料庫連接地址
          userId            使用者
          password            密碼
          
                還可以使用以下格式添加資料庫的其他串連屬性
          <property name="" value=""/>
        -->
        
        <javaTypeResolver >
        
          <property name="forceBigDecimals" value="false" />
          
          <!--
                  預設false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer
                  true,把JDBC DECIMAL 和 NUMERIC 類型解析為java.math.BigDecimal
        -->
          
        </javaTypeResolver>
            
        <javaModelGenerator targetPackage="com.newbee.bean" targetProject="newbee/src" />
        
        <!--
          targetProject        產生的Java Bean放置在哪個項目的哪個目錄下
          targetPackage        產生的Java Bean的包名
                 一個有用的屬性
          <property name="trimStrings" value="true" />
                  從資料庫返回的值被清理前後的空格
          <property name="enableSubPackages" value="false" />
                  是否在包名後加上scheme名稱
        -->
            
        <sqlMapGenerator targetPackage="com.newbee.xml" targetProject="newbee/src" />
        
        <!--
          targetProject        產生的 SqlMap.xml 檔案放置在哪個項目的哪個目錄下
          targetPackage        產生的 SqlMap.xml 檔案的包名
          <property name="enableSubPackages" value="false" />
                  是否在包名後加上scheme名稱
        -->
            
        <daoGenerator targetPackage="com.newbee.dao" targetProject="newbee/src" type="GENERIC-CI" />
        
        <!--
          targetProject        產生的 dao類檔案放置在哪個項目的哪個目錄下
          targetPackage        產生的 dao類檔案的包名
          <property name="enableSubPackages" value="false" />
                  是否在包名後加上scheme名稱
          type        產生dao檔案的類型,可選擇IBATIS、SPRING、GENERIC-CI、GENERIC-SI。預設使用GENERIC-CI
                      dao類在構造器中擷取 SqlMapClient。
                  
        -->
            
        <table tableName="ALLTYPES" domainObjectName="Customer" >
        
        <!--
          tableName 資料庫表明,據說可以包含SQL萬用字元%和_。
          domainObjectName 資料庫表對應的資料對象名稱,預設使用表名作為對象名稱。
        -->
        
          <property name="useActualColumnNames" value="true"/>
          
          <!--
                  對象的屬性名稱是否使用欄位名稱
          -->
          
          <generatedKey column="ID" sqlStatement="DB2" identity="true" />
          
          <!--
              column    自增長或使用sequence產生的欄位名
              sqlStatement 產生欄位的sql片段或其簡稱(參考官方文檔)
              identity    true表示後產生,false表示預產生
              
                  例如:
                  
                  postgresql:<generatedKey 
                      column="lid" 
                      sqlStatement="select nextval('tb000000producttype_lid_seq')" 
                      identity="false" />
                      
                  sqlserver:<generatedKey 
                      column="lid" 
                      sqlStatement="SqlServer" 
                      identity="true" />
                      
                  oracle:<generatedKey 
                      column="lid" 
                      sqlStatement="select tb000000producttype_lid_seq.nextval from dual" 
                      identity="false" />
              
          -->
          
          <columnOverride column="DATE_FIELD" property="startDate" />
          
              <!--
                  column  欄位名
                  property 欄位對應的屬性名稱。(預設使用欄位名的)
                  javaType 對應的Java類型
                  jdbcType 對應的jdbc類型
                  
                      這裡的設定覆寫javaTypeResolver中的指定
              -->
          
          <ignoreColumn column="FRED" />
          
          <!--
              column    需要忽略的資料庫欄位
          -->
          
          <columnRenamingRule searchString="^CUST_" replaceString="" />
          
          <!--
                  資料庫欄位名稱到對象屬性名稱的影射關係。就是一個替換處理。
          -->
          
        </table>
        
    </ibatorContext>
    
</ibatorConfiguration>

 

 


  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.