如有一代表用户的域对象User:
package com.domain;
import java.io.Serializable;
public class User implements Serializable{
private int user_id;
private String user_name;
private String user_password;
private String user_desc;
....//省略set、get方法
}
User的数据库操作接口:
package com.dao;
import com.domain.User;
public interface UserDao {
public void addUser(User user);
}
有一继承Spring JDBC支持类的UserDao接口实现类,实现添加一个User的方法。它需要注入一个Spring JDBC模板类JdbcTemplate:
package com.dao.jdbc;
import com.domain.User;
import com.dao.UserDao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class UserJdbcDao extends JdbcDaoSupport implements UserDao{
public void addUser(User user){
String SQL =
"INSERT INTO user(user_name,user_password,user_desc) VALUES(?,?,?)";
Object[] params = new Object[]{
user.getUser_name(),
user.getUser_password(),
user.getUser_desc()
} ;
this.getJdbcTemplate().update(SQL, params);
}
}
以上Dao层的类对应的Bean的基本配置文件app_dao.xml如下(数据源的属性放入了外部的资源文件"prop.properties"):
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:prop.properties"/>
</bean>
<!--数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--Spring JDBC模板Bean,它注入了上面的数据源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--User数据操作的Bean声明,它注入了上面的Spring JDBC模板Bean:jdbcTemplate-->
<bean id="userJdbcDao"
class="com.dao.jdbc.UserJdbcDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
这里我简单地模拟业务类(业务接口UserService省略):
package com.service.impl;
import com.dao.UserDao;
import com.domain.User;
import com.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
public void addUser(User user){
this.userDao.addUser(user);
}
}
为了在业务类中使用事务管理功能,有如下几个方法:
1、用原始的TransactionFactoryBean的app.xml基本配置:
<import resource="classpath:app_dao.xml"/><!--导入dao层的配置-->
<!--Spring JDBC的事务管理Bean,引入了DBCP数据源-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--业务类Bean-->
<bean id="userServiceImplTarget" class="com.service.impl.UserServiceImpl">
<property name="userDao" ref="userJdbcDao"/>
</bean>
<!--应用原始的TransactionFactoryBean进行事务管理Bean的声明-->
<bean id="userServiceImpl"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="txManager"/><!--指定事务管理bean-->
<property name="target" ref="userServiceImplTarget"/><!--指定业务bean-->
<property name="transactionAttributes"><!--事务的属性设置列表-->
<props>
<prop key="add*">PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE</prop>
<!--设置事务为只读时,添加数据将会产生异常-->
<!--<prop key="add*">PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE,readOnly</prop>-->
</props>
</property>
</bean>
测试:
......
UserServiceImpl usi = (UserServiceImpl)ctx.getBean("userServiceImpl");
......
2、用tx/aop命名空间配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans .....
xmlns:tx="http://www.springframework.org/schema/tx"
xsp:schemaLocation="http://www.springframework.org/schema/beans
...........
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<import resource="classpath:app_dao.xml"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userServiceImplTarget" class="com.service.impl.UserServiceImpl">
<property name="userDao" ref="userJdbcDao"/>
</bean>
<!--应用tx/aop命名空间进行事务声明-->
<!--用tx:advice声明具体方法的事务属性,及应用到的事务管理器-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--用aop:config声明要进行事务增强的切面-->
<aop:config>
<aop:pointcut id="serviceMethod"
expression="execution(* com.service..add*(..))"/>
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>
测试:
.......
UserService usi = (UserService)ctx.getBean("userServiceImplTarget");
..........
3、使用@Transactional注解配置声明事务(最简单实用的方法):
在需要事务管理增强的业务类加入@Transactional注解标记,如:
......
import org.springframework.transaction.annotation.Transactional; //注解式事务
@Transactional(readOnly=false) //对业务类进行事务增强的标注
public class UserServiceImpl implements UserService {
...........
}
再在配置文件中用<tx:annotation-driven>驱动自动为标记@Transactional注解的类织入事务管理增强:
<import resource="classpath:app_dao.xml"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注解式事务配置驱动-->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
<!--业务类bean的实现类标注了@Transactional注解,所以会被
tx:annotation-driven注解驱动自动织入事务增强-->
<bean id="userService" class="com.service.impl.UserServiceImpl">
<property name="userDao" ref="userJdbcDao"/>
</bean>
测试:
.........
UserServiceImpl usi = (UserServiceImpl)ctx.getBean("userService");
.........