1. 介紹
Abator 是一款iBATIS代碼生成工具。它從數據庫獲取信息,然後產生如下文件。
1) 數據庫表的SqlMap.xml file 文件,如UserSQL.xml;
2) 數據庫表的Java Bean Model,如User.java;
3) 包含一個綜合主鍵的數據庫表的主鍵model,如UserKey.java;
4) 包含DAO層 selectByExample方法的example文件, UserExample.java;
5) 提供插入、更新、查詢、刪除方法的DAO接口 UserDAO.java;
6) DAO接口的實現類 UserDAOImpl.java。
2. 安裝Abator到MyEclipse(Eclipse)中
1) 點擊MyEclipse(Eclipse)工具欄中的Help-->software update -->find and install --> search for new features to install --> new remote site。
2) 在name處填寫abator ,在url處填寫http://ibatis.apache.org/tools/abator。
3) 點擊OK, 然後點擊finish,系統開始安裝Abator到MyEclipse(Eclipse)中。
4) Abator插件安裝成功後會在file -->new 菜單中看到Abator for iBATIS Configuration File。
3. 使用Abator生成程序文件
1) 在MyEclipse(Eclipse)中,點擊File --> new 選擇Abator for iBATIS Configuration File,創建一個名叫做abatorConfig.xml的配置文件。
2) 修改配置文件。下面給出一個示例。
-
- <?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 generatorSet=”java5”>
- <!--JDBC驅動程序、連接字符串、用戶名以及密碼-->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/evaluation"
- userId="root"
- password="138246s" >
- <!--連接數據庫所用的jar包地址-->
- <classPathEntry
- location=".\mysql-connector-java\5.1.18\mysql-connector-java-5.1.18.jar" />
- </jdbcConnection>
- <!--數據庫表的Java Bean Model 存放的目錄-->
- <javaModelGenerator targetPackage="com.sogou.mobile.tc.ev.report.db"
- targetProject="tcevaluation" />
- <!--sqlmap的存放目錄-->
- <sqlMapGenerator targetPackage="com.sogou.mobile.tc.ev.report.db"
- targetProject="tcevaluation" />
- <!--DAO的存放目錄, IBATIS DAO在最新版中已經不再更新,可以用spring框架DAO管理代替,或者使用GENERIC-CI不使用框架,直接使用SQL client map-->
- <daoGenerator targetPackage="com.sogou.mobile.tc.ev.report.db"
- targetProject="tcevaluation" type="GENERIC-CI" />
- <!--設置需要生成的數據庫表,schema:數據庫名稱 tableName表名-->
- <table tableName="Page" domainObjectName="Page">
- <generatedKey column="id" identity="true"
- sqlStatement="select p.nextval from dual"/>
- <columnOverride column="id" property="id" javaType="int"
- jdbcType="int"/>
- <columnOverride column="typeid" property="typeid" javaType="int"
- jdbcType="int" />
- </table>
- </abatorContext>
- </abatorConfiguration>
abatorContext generatorSet="Java5" 這裏選擇得生成器是JAVA2的雖然不是推薦的,但個人覺得還停好用的,其他可以選擇Legacy,Java2,Java5,這兩個生成器生成的代碼在後面WHERE查詢條件的時候會有細微不同
<generatedKey>來告訴abator那些列需要自動返回值(當插入的時候可以返回插入記錄的主鍵,這對有外鍵的數據庫極其有用)identity默認爲false,則在sqlMap配置文件中<selectKey>的位置在sql命令前面,所以identity應該改爲true。sqlStatement屬性則填入各類數據庫的返回值函數:
Cloudscape This will translate to VALUES IDENTITY_VAL_LOCAL()
DB2 This will translate to VALUES IDENTITY_VAL_LOCAL()
Derby This will translate to VALUES IDENTITY_VAL_LOCAL()
HSQLDB This will translate to CALL IDENTITY()
MySql This will translate to SELECT LAST_INSERT_ID()
SqlServer This will translate to SELECT SCOPE_IDENTITY()
<columnOverride>告訴abator那些列是需要特殊處理的,對於date這種比較複雜的類型,可以用它來指定映射(用jdbctype和javatype屬性來指定)。
3) 配置完畢,右鍵點擊abatorConfig.xml文件,點擊generate iBatis artfacts就會在相應的配置目。
結構
- <abatorConfiguration>
- <!-- 普通的java資源文件 properties
- resource引用 resource="com/myproject/abatorConfig.properties" 必須在com.myproject包中
- url引用 例如:url="file://C:/myfolder/abatorConfig.properties"
- 以${property}的方式引用,屬性資源文件中的屬性值
- -->
- <properties resource=""? url=""? />*
- <!--
- defaultModelType 生成代碼的類型
- defaultModelType="conditional(默認值)|flat|hierarchical"
- conditional:類似hierarchical,但是隻有一個主鍵的時候,不會拆分類
- flat:一個table生成一個類
- hierarchical:如果有主鍵,生成一個類,BLOB生成一個類,其它的在生成一個類
- -->
- <!--
- generatorSet
- generatorSet="Legacy(默認值)|Java2|Java5"
- Legacy 兼容所有的iBatis版本和 Java2,會丟失一些特性,不建議使用
- Java2 支持iBatis2.2.0及以上,所有版本的Java2
- Java5 支持iBatis2.2.0及以上,所有版本的JSE5
- -->
- <abatorContext defaultModelType=””? generatorSet=””?>+
- <!--
- 支持的屬性
- suppressTypeWarnings=false, Java5不起早作用
- beginningDelimiter="\"" 分隔符
- endingDelimiter ="\"" 分隔符
- -->
- <propter name=”” value=”” />*
- <commentGenerator type=""?>?
- <propter name=”” value=”” />*
- </commentGenerator>
- <jdbcConnection driverClass=”” connectionURL=”” userId=””? password=””?>
- <!-- location 指的是jdbc連接器的位置,如果在classpath中就不用添加這項了 -->
- <classPathEntry location=””/>*
- <propter name=”” value=”” />*
- </jdbcConnection>
- <javaTypeResolver type="" ?>?
- <propter name=”” value=”” />*
- </javaTypeResolver>
- <javaModelGenerator type="" ? targetPackage="" targetProject="">
- <propter name=”” value=”” />*
- </javaModelGenerator>
- <sqlMapGenerator type="" ? targetPackage="" targetProject="">
- <propter name=”” value=”” />*
- </sqlMapGenerator>
- <daoGenerator type="" ? targetPackage="" targetProject="">?
- <propter name=”” value=”” />*
- </daoGenerator>
- <table catalog="" ? schema="" ? tableName="" alias="" ?
- domainObjectName="" ? enableInsert="" ? enableSelectByPrimaryKey="" ?
- enableSelectByExample="" ? enableUpdateByPrimaryKey="" ?
- enableDeleteByPrimaryKey="" ? enableDeleteByExample="" ?
- enableCountByExample="" ? enableUpdateByExample="" ?
- selectByPrimaryKeyQueryId="" ? selectByExampleQueryId="" ? modelType="" ?
- escapeWildcards="" ? delimitIdentifiers="" ?>+
- <propter name=”” value=”” />*
- <generatedKey column="" sqlStatement="" identity="" ? type="" ? />?
- <columnRenamingRule searchString="" replaceString="" ? />?
- <!-- columnOverride ignoreColumn不能同時出現 -->
- <columnOverride column="" property="" ? javaType="" ?
- jdbcType="" ? typeHandler="" ? delimitedColumnName="" ? />*
- <ignoreColumn column="" delimitedColumnName="" ? /> *
- </table>
- </abatorContext>
- </abatorConfiguration>
請參考eclipse/plugins/org.apache.ibatis.abator.doc_1.1.0/ doc.zip/core/htmldoc/index.html 文檔