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”/>
- </bean>
这就是所有需要注意的问题了,此后就可以在业务逻辑层调用DAO对象了!
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>mysql</value>
</property>
</bean>
<!-- Spring iBatis Template -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 需要引入aop的命名空间 -->
<aop:config>
<!-- 切入点指明了在所有方法产生事务拦截操作 -->
<aop:pointcut id="serviceMethods"
expression="execution(* com.angi.ibatis.service.*.*(..))" />
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>
<!-- 需要引入tx的命名空间 -->
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<bean id="userService" class="com.angi.ibatis.service.UserService">
<property name="userDao">
<ref bean="userDAO" />
</property>
</bean>
</beans>
Spring下由三种途径对事物进行管理:编程式事务管理、声明式事务管理和AOP事务管理。其中AOP事务管理又分AOP注解事务管理和AOP XML配置两种,这里记录下述其中的AOP XML配置管理,这也是spring最推荐的方式。
参照<spring高级程序设计>中的银行转账的例子。
1.Spring的数据源设置
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/test"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- </bean>
2.Spring对iBATIS的支持
Spring对ibatis主要提供org.springframework.orm.ibatis.SqlMapClientFactoryBean类来进行支持
- <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="configLocation" value="/config/sqlMapConfig.xml"/>
- </bean>
3.Spring对iBATIS DAO的支持
Spring提供org.springframework.orm.ibatis.support.SqlMapClientDaoSupport来对iBATIS DAO进行支持,通过调用该类的getSqlMapClientTemplate()方法来获得对iBATIS的控制访问。
- <bean id="accountDao" class="com.hj.dao.AccountDaoImp">
- <property name="sqlMapClient" ref="sqlMapClient"/>
- </bean>
- <bean id="bankService" class="com.hj.bankOps.DefaultBankService">
- <property name="accountDao" ref="bankAccountDao"/>
- </bean>
这里DefaultBankService类主要实现BankService接口(提供服务的方法定义),其内部引用一个BankAccountDao实例来对数据库进行访问。BankAccountDao类主要继承SqlMapClientDaoSupport。
4.Spring 配置事务
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
5.AOP XML配置事务管理
1).配置事务通知
- <tx:advice id="transactionManagerAdivice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="*"
- isolation="READ_COMMITTED"
- propagation="REQUIRED"
- rollback-for="java.lang.RuntionException" />
- </tx:attributes>
- </tx:advice>
2).配置切入点和方面
- <aop:config>
- <aop:pointcut expression="execution(* com.hj.bankOps.DefaultBankService.*(..))" id="bankServicePc"/>
- <aop:advisor advice-ref="transactionManagerAdivice" pointcut-ref="bankServicePc"/>
- </aop:config>
上述execution(* com.hj.bankOps.DefaultBankService.*(..))表达式表示切入点为该类中的任何方法。所以当DefaultBankService类中方法调用时就会进行事务管理,并且当抛出RuntimeException时,自动进行回滚操作。
6.遇到的问题
在<Spring高级程序设计>一书上,对AOP XML事务配置时,其通知部分并没有设置具体属性(缺少 rollback-for="java.lang.RuntionException")
- <tx:attributes>
- <tx:method name="*"
- isolation="READ_COMMITTED"
- propagation="REQUIRED"
- />
- </tx:attributes>
这样在DefaultBankService方法调用中如果有异常抛出,事务并不进行相应回滚操作。