配置业务层-
<bean id="accountService" class="com.wyc.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
配置账户的持久层-
<bean id="accountDao" class="com.wyc.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
配置数据源-
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
配置事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
配置事务的通知
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--配置事务的属性-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
配置aop
<aop:config>
配置切入点表达式
<aop:pointcut id="pt1" expression="execution(* com.wyc.service.impl.*.*(..))"></aop:pointcut>
建立切入点表达式和事务通知的对应关系
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
配置事务的属性
1. isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别。
2.propagation:默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。
3. read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写。
4. timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
5.rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
6.no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚。没有默认值。表示任何异常都回滚。
注解型
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true) 只读型
public class AccountServiceImpl implements IAccountService{
@Autowired
private IAccountDao accountDao;
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
@Transactional(propagation = Propagation.REQUIRED,readOnly = false) 只读型
public void transfer(String sourceName, String targetName, Float money) {
System.out.println("transfer....");
Account source = accountDao.findAccountByName(sourceName);
Account target = accountDao.findAccountByName(targetName);
source.setMoney(source.getMoney()-money);
target.setMoney(target.getMoney()+money);
accountDao.updateAccount(source);
// int i=1/0;
accountDao.updateAccount(target);
}}
<context:component-scan base-package="com.wyc"></context:component-scan>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置账户的持久层-->
<bean id="accountDao" class="com.wyc.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启Spring 开启事务的支持-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>