Spring事务实例:
entity实体类:
public class Accounts { private int accountid; private String accountname; private double balance; public int getAccountid() { return accountid; } public void setAccountid(int accountid) { this.accountid = accountid; } public String getAccountname() { return accountname; } public void setAccountname(String accountname) { this.accountname = accountname; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } }
dao层接口:
//加钱 void addMoney(double money); //减钱 void subMoney(double money);
daoimpl实现类接口:
@Override @Transactional(propagation = Propagation.REQUIRED) public void addMoney(double money) { String sql="UPDATE accounts SET balance=balance+? WHERE accountname='张三'"; this.getJdbcTemplate().update(sql,money); } @Override @Transactional(propagation = Propagation.REQUIRED) public void subMoney(double money) { String sql="UPDATE accounts SET balance=balance-? WHERE accountname='小红'"; this.getJdbcTemplate().update(sql,money); }
service业务层:
//转账 void transferMoney();
service业务实现层:
//转账的方法 /*@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)*/ @Transactional @Override public void transferMoney() { accountDao.subMoney(1000); //价钱 int num=5/0; //模拟一个异常 使用事务解决 accountDao.addMoney(1000); }
applicationContext.xml文件:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--添加jdbc--> <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${url}"></property> <property name="driverClassName" value="${driver}"></property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!--配置jdbcTemplate核心对象--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--Dao接口的实现类对象--> <bean id="accountDao" class="com.te.daoimpl.AccountDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!--service接口的实现类对象--> <bean id="accountService" class="com.te.serviceImpl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置事务-->
<!--方式一:工厂配置-->
<!--配置Spring事务增强的代理工厂Bean-->
<bean id="transactionProxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<--事务管理器-->
<property name="transactionManager" ref="transactionManager"></property>
<--加载主业务对象-->
<property name="target" ref="accountService"></property>
<--设置事务的隔离级别和传播行为-->
<property name="transactionAttributes">
<props>
<--键值key为具体的方法名value,可以为传播行为或隔离级别-->
<prop key="transferMoney">ISOLATION_READ_UNCOMMITTED,PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!--方式二:AOP配置--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="transferMoney" propagation="REQUIRED" isolation="READ_COMMITTED" /> </tx:attributes> </tx:advice> <!--定义切面--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.te.service.*.*(..))"></aop:advisor> </aop:config>
<!--方式三:注解式-->
<tx:annotation-driven/>
<context:component-scan base-package="com.te"></context:component-scan>
</beans>
测试:
@Test public void test03(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("application-del.xml"); AccountService accountService =(AccountService)ctx.getBean("accountService"); //转账 accountService.transferMoney(); }