spring-事务管理
xml注解:注解和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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:td="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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com"/>
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClassName}"/>
<property name="url" value="${prop.url}"/>
<property name="username" value="${prop.username}"/>
<property name="password" value="${prop.password}"/>
</bean>
<!--jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 注入datasource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 创建事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- 配置通知-->
<td:advice id="txadvice">
<tx:attributes>
<tx:method name="accountMoney" isolation="REPEATABLE_READ" timeout="-1"/>
<tx:method name="account*"/>
</tx:attributes>
</td:advice>
<!--配置切入点 -->
<aop:config>
<!-- 设置切入点-->
<aop:pointcut id="pt" expression="execution(* com.service.UserService.accountMoney(..))"/>
<!-- 设置切面-->
<aop:advisor advice-ref="txadvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beans>
只需要在service上写上这个注解即可:
@Transactional 里面可以设置参数类型 :
源码如下
@AliasFor("transactionManager")
String value() default "";
@AliasFor("value")
String transactionManager() default "";
Propagation propagation() default Propagation.REQUIRED;
Isolation isolation() default Isolation.DEFAULT;
int timeout() default -1;
boolean readOnly() default false;
Class<? extends Throwable>[] rollbackFor() default {};
String[] rollbackForClassName() default {};
Class<? extends Throwable>[] noRollbackFor() default {};
String[] noRollbackForClassName() default {};
基于全注解的开发:
config类如下:
package com.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@ComponentScan(basePackages = "com")
@EnableTransactionManagement//开启事务
public class Txconfig {
@Bean
public DruidDataSource getDruidDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=utf-8");
druidDataSource.setUsername("root");
druidDataSource.setPassword("110120");
return druidDataSource;
}
@Bean
public JdbcTemplate getJdbcTemplate(){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(getDruidDataSource());
return jdbcTemplate;
}
//创建事务管理器
public DataSourceTransactionManager getDataSourceTransactionManager(){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(getDruidDataSource());
return dataSourceTransactionManager;
}
}
测试类:
package com.test;
import com.config.Txconfig;
import com.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAcount {
/**
* 测试事务
* 案例:lucy向 mary转账100 lucy and mary 各自1000元
*/
@Test
public void testAcount(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.accountMoney();
}
/**
* 测试txconfig类的事务操作
* 案例:lucy向 mary转账100 lucy and mary 各自1000元
*/
@Test
public void testAcount2(){
ApplicationContext context = new AnnotationConfigApplicationContext(Txconfig.class);
UserService userService = context.getBean("userService", UserService.class);
userService.accountMoney();
}
}
数据库表格如下:
完结!
本文来自博客园,作者:wiselee/,转载请注明原文链接:https://www.cnblogs.com/wiseleer/p/16042168.html