Ibatis 生成工具ibator的使用 適用於ibaits2

這裏使用的是ibator做爲ibatisEclipse插件,ibator的主頁是:http://ibatis.apache.org/ibator.htmlhtml

它提供兩種方式:一種是Eclipse插件,一種是獨立運行。java

目前針對Eclipse的版本有兩個版本,sql

1.2.1能運行在:數據庫

· Eclipse 3.4.1 or higherapache

· JDK 5.0 or Higheroracle

· 它的Eclipse插件URL下載地址爲:http://ibatis.apache.org/tools/ibatordom

1.1.0運行在:eclipse

· Eclipse 3.2 or higheride

· JDK 1.4 or Highersqlserver

· 它的Eclipse插件URL下載地址爲:http://ibatis.apache.org/tools/abator

注意: 版本問題,筆者開始用的是myEclipse6.0.1,是eclipse 3.3.1平臺,裝了1.2.1怎麼也不能用,調查才發現,不要由於版本而影響做用。

範例 :

新建一個工程,而後在該工程下新建文件,截圖以下:(若是這裏沒有請到other..中找,截圖如右:)

點擊後就會新建一個文件:abatorConfig.xml。

如下是1.1.0的寫法,供你們參照:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >

<abatorConfiguration >

<abatorContext >

<jdbcConnection driverClass="com.microsoft.jdbc.sqlserver.SQLServerDriver" connectionURL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=cmspro" userId="cmspro" password="cmspro" >

<classPathEntry location="D:\POM\project\msbase.jar" />

<classPathEntry location="D:\POM\project\mssqlserver.jar"/>

<classPathEntry location="D:\POM\project\msutil.jar" />

</jdbcConnection>

<javaModelGenerator targetPackage="com.ucap.account.model" targetProject="account-service-impl" />

<sqlMapGenerator targetPackage="resources.sqlmaps" targetProject="account-service-impl" />

<daoGenerator targetPackage="com.ucap.account.dao" targetProject="account-service-impl" type="GENERIC-CI" />

<table tableName="cmspro_channel_privilege" domainObjectName="ChannelPrivilege"/>

</abatorContext>

</abatorConfiguration>

這裏<table>標籤中的domainObjectName,是用來從新命名類名。

如下是1.2.1的寫法,供你們參照:

<?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>
 <!-- 
      url     指定屬性文件絕對路徑。注意與指定數據庫jdbc驅動jar包路徑的區別哈。 
                    可使用${property}的格式引用屬性文件中的屬性值。 
    <properties url="D:\WorkSpace\SSIStudy\ibator-config.properties"/>
    -->
    <!-- 
      classPathEntry 指定數據庫jdbc驅動jar包的絕對路徑。 可爲爲空
      The full path name of a JAR/ZIP file to add to the classpath, or a directory to add to the classpath
    -->  
    <classPathEntry location="D:\WorkSpace\SSIStudy\WebRoot\WEB-INF\lib" /> 
    
    <!-- 
      id                 這個id能夠在使用命令行運行Abator時指定,以單獨處理某一個ibatorContext 
      targetRuntime        Ibatis2Java5 生成適合JDK5.0的類,另外一個選項是 Ibatis2Java2,生成適合Java2的類。 
    --> 
  <ibatorContext id="SSIStudy" targetRuntime="Ibatis2Java2">
   <!-- 
           ibatorPlugin 繼承自IbatorPluginAdapter,包名必須是 org.apache.ibatis.ibator.plugins,具體實現能夠參考官方文檔 
             必須有替換和被替換字符屬性。 能夠沒有
     --> 
     <!-- 
   <ibatorPlugin type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin"> 
             <property name="searchString" value="Example$" /> 
             <property name="replaceString" value="Criteria" /> 
     </ibatorPlugin> 
     
           driverClass        數據庫驅動類  這裏鏈接sqlserver2000
           connectionURL      數據庫鏈接地址 
           userId             用戶 
           password           密碼 
            還可使用如下格式添加數據庫的其餘鏈接屬性
           <property name="" value=""/> 
     --> 
       
     <jdbcConnection driverClass="com.microsoft.jdbc.sqlserver.SQLServerDriver" connectionURL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo" userId="sa" password="sa" >
     </jdbcConnection>
     
     <javaTypeResolver > 
           <!-- 
                   默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer 
                   true,把JDBC DECIMAL 和 NUMERIC 類型解析爲java.math.BigDecimal 
           -->  
           <property name="forceBigDecimals" value="false" /> 
     </javaTypeResolver>
     <!-- //When running outside of an IDE environment like Eclipse, Ibator interprets the targetProject and targetPackage attributes in all XML configurations as follows
           targetProject        生成的Java Bean放置在哪一個項目的哪一個目錄下 
           targetPackage        生成的Java Bean的包名 
           
             一個有用的屬性
            從數據庫返回的值被清理先後的空格 
           <property name="trimStrings" value="true" /> 
            是否在包名後加上scheme名稱        
           <property name="enableSubPackages" value="false" />       
     -->  
     <javaModelGenerator targetPackage="cn.per.jiabeis.model" targetProject="D:\WorkSpace\SSIStudy\src">
      <property name="enableSubPackages" value="true" />
     <property name="trimStrings" value="true" />
     </javaModelGenerator>
     <!-- 
           targetProject        生成的 SqlMap.xml 文件放置在哪一個項目的哪一個目錄下 
           targetPackage        生成的 SqlMap.xml 文件的包名 
           <property name="enableSubPackages" value="false" /> 
                   是否在包名後加上scheme名稱 
      --> 
     <sqlMapGenerator targetPackage="cn.per.jiabeis.model.config" targetProject="D:\WorkSpace\SSIStudy\src">
      <property name="enableSubPackages" value="true" />
     </sqlMapGenerator>
      <!-- 
           targetProject        生成的 dao類文件放置在哪一個項目的哪一個目錄下 
           targetPackage        生成的 dao類文件的包名 
           <property name="enableSubPackages" value="false" /> 
                   是否在包名後加上scheme名稱 
           type        生成dao文件的類型,可選擇IBATIS、SPRING、GENERIC-CI、GENERIC-SI。默認使用GENERIC-CI 
                       dao類在構造器中獲取 SqlMapClient。 
                    
     --> 
     <daoGenerator targetPackage="cn.per.jiabeis.dao" targetProject="D:\WorkSpace\SSIStudy\src" type="IBATIS">
     <property name="enableSubPackages" value="true" />
  </daoGenerator>
  
  <!-- 
           tableName 數據庫代表,聽說能夠包含SQL通配符%和_。 
           domainObjectName 數據庫表對應的數據對象名稱,默認使用表名做爲對象名稱。 
     --> 
     <table tableName="student" domainObjectName="Student"> 
      <!-- 
                   對象的屬性名是否使用字段名稱,默認爲true
      --> 
      <property name="stuname" value="stuname"/> 
       <!-- 
               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" /> 
                   MySql:<generatedKey  
                       column="lid"  
                       sqlStatement="MySql"  
                       identity="false" />
                
         --> 
      <generatedKey column="stuid" sqlStatement="SqlServer" identity="true"/>
      
         <!-- 
                   數據庫字段名稱到對象屬性名稱的影射關係。就是一個替換處理。 
          --> 
         <columnRenamingRule searchString="^CUST_" replaceString="" />  
         <!-- 
                   column  字段名 
                   property 字段對應的屬性名。(默認使用字段名的) 
                   javaType 對應的Java類型 
                   jdbcType 對應的jdbc類型 
                    
                       這裏的設置覆寫javaTypeResolver中的指定 
          --> 
       <columnOverride column="DATE_FIELD" property="startDate" /> 
            
          <!-- 
               column    須要忽略的數據庫字段 
          -->   
          <ignoreColumn column="FRED" />   
          
     </table>
    
     <table tableName="teacher"/>
     <table tableName="course"/> 
     <table tableName="stu_course"/>
    </ibatorContext>
</ibatorConfiguration>

以上書寫好xml後,請鼠標右鍵點擊該xml文件生成Artifacts。

相關文章
相關標籤/搜索