對于IBatis應(yīng)用最煩人的重復(fù)工作就是不停的寫DAO,DTO,和xml文件里面的sqlmap,其實(shí)很多時(shí)候這些工作是重復(fù)而且無聊的。好在我們還有abator (http://ibatis./abator.html ),一個(gè)IBatis自動(dòng)生成工具,他可以幫助你生成大量的DAO,DTO和sqlmap。 如果你使用eclipse作為IDE那么事情就很簡單了(相信沒有幾個(gè)人不在用): 安裝Eclipse插件 1. 要求eclipse3.1,jdk1.4以上 2. 在eclipse上安裝abator插件 eclipse菜單欄 --> help --> Software Updates --> Find And Install... 在彈出的對話框中選擇 “Search for new features to install” 然后點(diǎn)擊 “next” 在對話框中點(diǎn)擊按鈕 New Remort Site... 在彈出的對話框中的URL輸入框中填寫 http://ibatis./tools/abator ,Name輸入框就隨便寫些什么了。 點(diǎn)擊Finish,安裝插件,然后重新啟動(dòng)eclipse 下面測試一下功能 1. 新建一個(gè)java工程 2. 在eclipse菜單欄中興建一個(gè) abator 配置文件 File --> Abator for iBatis Configration File 好的,我們完成了創(chuàng)建配置文件,下面打abator配置文件,看看里面怎么去配置 打開后內(nèi)容基本上是這樣
xml 代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN"
- "http://ibatis./dtd/abator-config_1_0.dtd">
-
- <abatorConfiguration>
- <abatorContext> <!-- TODO: Add Database Connection Information -->
- <jdbcConnection driverClass="???"
- connectionURL="???"
- userId="???"
- password="???">
- <classPathEntry location="???" />
- </jdbcConnection>
-
- <javaModelGenerator targetPackage="???" targetProject="???" />
- <sqlMapGenerator targetPackage="???" targetProject="???" />
- <daoGenerator type="IBATIS" targetPackage="???" targetProject="???" />
-
- <table schema="???" tableName="???">
- <columnOverride column="???" property="???" />
- </table>
-
- </abatorContext>
- </abatorConfiguration>
你需要做的是替換一些???: 1. 填寫driverClass(jdbc驅(qū)動(dòng),例如oracle的就是oracle.jdbc.driver.OracleDriver) 2. 填寫connectionURL(連接字符串,例如oracle的就是jdbc:oracle:thin:@192.168.0.246:1521:test) 3. 填寫classPathEntry的location(jdbc驅(qū)動(dòng)jar包的位置,例如E:/project/ibatistest/WebContent/WEB-INF/lib/ojdbc14.jar) 4. 填寫javaModelGenerator,生成的DTO(java model 類) targetPackage:目標(biāo)包的位置,如 com.test.dto targetProject:目標(biāo)工程名稱,填寫配置文件所在的eclipse工程名 5. 填寫sqlMapGenerator ,生成的xml sqlmap的相關(guān)配置 targetPackage:目標(biāo)位置,如 com.test.sqlmap targetProject:目標(biāo)工程名稱,填寫配置文件所在的eclipse工程名 6. 填寫daoGenerator ,生成的DAO的相關(guān)配置 type:生成的dao實(shí)現(xiàn)的類型,如果你使用spring的話寫SPRING,否則寫IBATIS targetPackage:目標(biāo)位置,如 com.test.dao targetProject:目標(biāo)工程名稱,填寫配置文件所在的eclipse工程名 7. 配置相關(guān)數(shù)據(jù)庫的表 schema:數(shù)據(jù)庫schema,oracle就是填寫數(shù)據(jù)庫的用戶名 tableName:表名
xml 代碼
- <columnOverride column="???" property="???" />
可以先不用配置,刪除就可以了 下面開始生成: 在配置文件上點(diǎn)解右鍵,選擇 “Generate iBatis Artifacts” OK,看看生成了什么吧!
|