日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ibatis与spring的整合

發布時間:2024/4/17 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ibatis与spring的整合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.blogjava.net/freeman1984/archive/2007/12/07/166112.html

Spring通過DAO模式,提供了對iBATIS的良好支持。SqlMapClient對象是iBATIS中的主要對象,我們可以通過配置讓spring來管理SqlMapClient對象的創建。

hibernate類似,Spring?提供了SqlMapClientDaoSupport對象,我們的DAO可以繼承這個類,通過它所提供的SqlMapClientTemplate對象來操縱數據庫??雌饋磉@些概念都與hibernate類似。

通過SqlMapClientTemplate來操縱數據庫的CRUD是沒有問題的,這里面關鍵的問題是事務處理。Spring提供了強大的聲明式事務處理的功能,我們已經清楚hibernate中如何配置聲明式的事務,那么在iBATIS中如何獲得聲明式事務的能力呢?

第一,我們需要了解的是spring通過AOP來攔截方法的調用,從而在這些方法上面添加聲明式事務處理的能力。典型配置如下: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>

這些事務都是聲明在業務邏輯層的對象上的。

第二,我們需要一個事務管理器,對事務進行管理。

????<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之后,數據源的配置移植到了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>

這就是所有需要注意的問題了,此后就可以在業務邏輯層調用DAO對象了!




http://blog.csdn.net/daryl715/article/details/1760793


總結

以上是生活随笔為你收集整理的ibatis与spring的整合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。