Name:Abator for Eclipse Update SiteURL:http://ibatis.apache.org/tools/abator
abator api url:http://ibatis.apache.org/docs/tools/abator/
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>
6.在我使用ibator的時候發生了以下幾處錯誤:
1)Exception :getting jdbc Driver 由於我開始的時候將
<classPathEntry location="D:"Program Files"work_soft"apache-maven-2.0.9"repository"com"oracle"ojdbc14"10.2.0.1.0"ojdbc14-10.2.0.1.0.jar"/>
這句放到了jdbcConnection裡,而新版本是放到外面的,所以報此錯誤。
2)Cannot find source folder ibatistest/src
因為連接埠號碼沒有配置正確1522配置成1521了,所以報這個問題,網上還有人說是連接埠號碼配置正確了防火牆攔截也有可能導致這個問題,那麼只需要去把防火 牆裡的“例外”裡添加你資料庫使用的連接埠號碼就可以了,如果安裝了防火牆軟體也是一樣道理,添加一個例外的連接埠。
做完以上的修改以後先用sqlplus試一下,如果能夠登入那麼就對了,如果不能夠登入,那麼你需要開啟資料庫的監聽程式,這個比較多內容就不再這裡說了,可以去網上查一下如何開啟資料庫的監聽程式。
3)Cannot find source folder ibatistest/src,由於我開始的時候將<javaModelGenerator targetPackage="com.model" targetProject="ibatistest/src">裡的 targetProject的值設定為ibatistest/src,但是我沒有建立這個檔案夾,所以就報這個錯誤了,如果你沒有建立任何源檔案夾那麼就是用你的工程名字就好了。
4)Invalid name specified: com/dao 由於我把com.dao寫成com/dao所以說是無效的包名.