ibatis与spring的整合
http://www.blogjava.net/freeman1984/archive/2007/12/07/166112.html
Spring通過DAO模式,提供了對iBATIS的良好支持。SqlMapClient對象是iBATIS中的主要對象,我們可以通過配置讓spring來管理SqlMapClient對象的創(chuàng)建。
與hibernate類似,Spring?提供了SqlMapClientDaoSupport對象,我們的DAO可以繼承這個類,通過它所提供的SqlMapClientTemplate對象來操縱數(shù)據(jù)庫。看起來這些概念都與hibernate類似。
通過SqlMapClientTemplate來操縱數(shù)據(jù)庫的CRUD是沒有問題的,這里面關鍵的問題是事務處理。Spring提供了強大的聲明式事務處理的功能,我們已經(jīng)清楚hibernate中如何配置聲明式的事務,那么在iBATIS中如何獲得聲明式事務的能力呢?
第一,我們需要了解的是spring通過AOP來攔截方法的調(diào)用,從而在這些方法上面添加聲明式事務處理的能力。典型配置如下:applicationContext-common.xml
| ????<!--?配置事務特性?--> ????<tx:advice?id="txAdvice"?transaction-manager="事務管理器名稱"> ??? ????<tx:attributes> ???????????<tx:method?name="add*"?propagation="REQUIRED"/> ???????????<tx:method?name="del*"?propagation="REQUIRED"/> ???????????<tx:method?name="update*"?propagation="REQUIRED"/> ???????????<tx:method?name="*"?read-only="true"/> ???????</tx:attributes> ????</tx:advice> ??? ????<!--?配置哪些類的方法需要進行事務管理?--> ????<aop:config> ???????<aop:pointcut?id="allManagerMethod"?expression="execution(* com.ibatis.manager.*.*(..))"/> ???????<aop:advisor?advice-ref="txAdvice"?pointcut-ref="allManagerMethod"/> ????</aop:config> | 
這些事務都是聲明在業(yè)務邏輯層的對象上的。
第二,我們需要一個事務管理器,對事務進行管理。
| ????<bean?id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> ????<property?name="dataSource"?ref="dataSource"/> ????</bean> ????<bean?id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"> ????????<property?name="driverClassName"?value="com.mysql.jdbc.Driver"/> ????????<property?name="url"?value="jdbc:mysql://127.0.0.1/ibatis"/> ????????<property?name="username"?value="root"/> ????????<property?name="password"?value="mysql"/> ????</bean> | 
此后,我們需要讓spring來管理SqlMapClient對象:
| ????<bean?id="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> ???????<property?name="configLocation"><value>classpath:sqlMapConfig.xml</value></property> ????</bean> | 
我們的sqlMapConfig.xml就可以簡寫為:
<?xml?version="1.0"?encoding="UTF-8"??>
<!DOCTYPE?sqlMapConfig?????
????PUBLIC?"-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"?????
????"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
????<settings
???????lazyLoadingEnabled="true"
??? ????useStatementNamespaces="true"?/>
????<!--?使用spring之后,數(shù)據(jù)源的配置移植到了spring上,所以iBATIS本身的配置可以取消?-->
??<sqlMap?resource="com/ibatis/dao/impl/ibatis/User.xml"/>
</sqlMapConfig>
User.xml:如下
<?xml?version="1.0"?encoding="UTF-8"??>
<!DOCTYPE?sqlMap?????
????PUBLIC?"-//ibatis.apache.org//DTD SQL Map 2.0//EN"?????
????"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap?namespace="User">
?<!-- Use type aliases to avoid typing the full classname every time. -->
?<typeAlias?alias="User"?type="com.ibatis.User"/>
?<!-- Select with no parameters using the result map for Account class. -->
?<select?id="selectAllUsers"?resultClass="User">
??? select * from t_user
?</select>
?
?<select?id="selectUser"?resultClass="User"?parameterClass="int">
??select * from t_user where id=#id#
?</select>
?
?<insert?id="insertUser"?parameterClass="User">
??insert into t_user values (
?????? null,#username#,#password#
??)
?</insert>
?
?<update?id="updateUser"?parameterClass="User">
??update t_user set username = #username#,password=#password#
??where id=#id#
??</update>
?
?<delete?id="deleteUser"?parameterClass="int">
??delete from t_user where id=#id#
?</delete>
</sqlMap>
我們的DAO的編寫:
| package com.iabtis.dao.impl.ibatis; import java.util.List; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.ibatis.dao.UserDAO; import com.ibatis.crm.model.User; public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO { ??? public void select(User user) { ????????????? getSqlMapClientTemplate().delete("selectUser?",user.getId()); ?????? } ???public List findAll() { ????????????? return getSqlMapClientTemplate().queryForList("selectAllUsers?"); ?????? } ?????? public void delete(User user) { ????????????? getSqlMapClientTemplate().delete("deleteUser?",user.getId()); ?????? } ?????? public void save(User user) { ????????????? getSqlMapClientTemplate().insert("insertUser?",user); ?????? } ?????? public void update(User user) { ????????????? getSqlMapClientTemplate().update("updateUser?",user); ?????? } } | 
繼承SqlMapClientDaoSupport,要求我們注入SqlMapClient對象,因此,需要有如下的DAO配置:
| <bean id="userDAO" class="com.ibatils.dao.impl.ibatis.UserDAOImpl"> ???? <property name=”sqlMapClient” ref=”sqlMapClient”/> <property name="dataSource" ref="dataSource"></property> </bean> | 
這就是所有需要注意的問題了,此后就可以在業(yè)務邏輯層調(diào)用DAO對象了!
 
 
http://blog.csdn.net/daryl715/article/details/1760793
 
 
總結(jié)
以上是生活随笔為你收集整理的ibatis与spring的整合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: The type org.springf
- 下一篇: struts2下面如何同时使用servl
