最近加入的項目,使用的ORM方案是iBATIS,以前用的都是hibernate,於是趕緊開始學習,上網google了下,發現ibatis是一個輕量級的o/r mapping解決方案,上手很快,於是決定不從教程看起了,在網上找了個例子,作爲入門用。這個例子講的簡單清楚,很適合快速上手用,如果有需要的朋友也可以看一下。
ibatis教程要從ibatis的概念開始,它是又一個o/r mapping解決方案,j2ee的o/r方案真是多,和hibernate相比,ibatis最大的特點就是小巧,上手很快。如果你不需要太多複雜的功能,ibatis是能滿足你的要求又足夠靈活的最簡單的解決方案。
ibatis最大的特點是簡單,最新版本2.0(下載),和1.0相比,主要改動在xml配置文件上,不過,只要有sql基礎,相信你不用教程也能看明白。下面我們看一個最簡單的例子入門。
我們先建一個表account,包括字段username, varchar(20), pk和password, varchar(20),隨便填入一些數據。然後編寫ibatis必須的配置文件sql-map-config.xml,文件名隨意,內容如下:
﹤?xml version="1.0" encoding="utf-8" ?﹥ ﹤!doctype sqlmapconfig public "-//ibatis.com//dtd sql map config 2.0//en" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"﹥ ﹤sqlmapconfig﹥ ﹤settings cachemodelsenabled="true" enhancementenabled="true" lazyloadingenabled="true" maxrequests="32" maxsessions="10" maxtransactions="5" usestatementnamespaces="false" /﹥ ﹤transactionmanager type="jdbc" ﹥ ﹤datasource type="simple"﹥ ﹤property name="jdbc.driver" value="sun.jdbc.odbc.jdbcodbcdriver"/﹥ ﹤property name="jdbc.connectionurl" value="jdbc:odbc:blog"/﹥ ﹤property name="jdbc.username" value="admin"/﹥ ﹤property name="jdbc.password" value=""/﹥ ﹤/datasource﹥ ﹤/transactionmanager﹥ ﹤sqlmap resource="account.xml" /﹥ ﹤/sqlmapconfig﹥ [/pre]
其他部分你不用管它,我是直接copy的示例配置,只需注意紅色部分,配置好數據源即可。我用的是access,所以用jdbcodbc驅動。如果你用mysql或其他數據庫,更改相應的屬性。
然後注意到這個配置文件還引用了一個account.xml,沒錯,ibatis把每個需要o/r mapping的java對象關聯到一個xml配置文件,我們需要把一個account表映射到一個account類:
package example;
public class account {
private string username;
private string password;
public string getusername() { return username; }
public void setusername(string username) { this.username=username; }
public string getpassword() { return password; }
public void setpassword(string password) { this.password=password; }
}
[/pre]
ibatis之編寫account.xml文件:
﹤?xml version="1.0" encoding="utf-8" ?﹥ ﹤!doctype sqlmap public "-//ibatis.com//dtd sql map 2.0//en" "http://www.ibatis.com/dtd/sql-map-2.dtd"﹥ ﹤sqlmap namespace="account"﹥ ﹤select id="getaccount" parameterclass="java.lang.string" resultclass="example.account"﹥ select * from account where username = #value# ﹤/select﹥ ﹤insert id="createaccount" parameterclass="example.account"﹥ insert into account (username, password) values ( #username#, #password# ) ﹤/insert﹥ ﹤/sqlmap﹥
[/pre]
我們主要關心以﹤select ...﹥爲主,這裏定義了一個查詢方法,名字爲getaccount,傳入參數爲string,返回類型resultclass就是 example.account類,select語句相信你已經很熟悉了,#value#是我們將要傳進去的string。
﹤insert...﹥ 與之類似,不過注意到#username#和#password#,由於參數是account類,它們將被account.getusername()和 account.getpassword()替換。所以,只要你會寫sql,就能非常容易地寫出配置文件。
最後便是如何使用ibatis實現o/r映射。首先初始化ibatis獲得一個sqlmapclient對象:
com.ibatis.sqlmap.client.sqlmapclient sqlmap = null;
try {
java.io.reader reader = om.ibatis.common.resources.resources.getresourceasreader ("sql-map-config.xml");
sqlmap = sqlmapclientbuilder.buildsqlmapclient(reader);
} catch (exception e) {
e.printstacktrace();
}
[/pre]
然後就可以異常方便地使用o/r mapping了,比如查詢username=admin的account:
try {
account accout = (account)sqlmap.queryforobject("getaccount", "admin");
...
}
[/pre]
或者創建一個新的account:
try {
account account = new account();
account.setusername("micheal");
account.setpassword("1234");
sqlmap.insert("createaccount", account);
}
[/pre]
運行時把兩個配置文件和ibatis的3個jar包放到classpath中,要求jdk1.4版本。
ibatis總 結:ibatis確實簡單靈活,上手容易,代碼很少,配置稍嫌複雜。不足之處一是沒有方便的工具來自動生成xml配置文件,二是不管是query還是 insert都只能傳入一個參數,有時不得不把兩個參數包裝成一個類傳進去。另外對常見的1:1,1:n關係的支持不如hibernate。不過,大多數 時候ibatis已經完全可以滿足我們的需求。spring很好的集成了ibatis,你可以參考spring的jpetstore示例。需要注意的是使 用ibatis 2.0和1.0有較大區別,主要體現在配置文件上,ibatis2.0新增了dao框架,不需要spring提供的dao也能很方便地實現dao模式。