spring事务管理(xml配置)与spring自带连接数据库JdbcTemplate
Posted 谁为含愁独不见,更教明月照流黄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring事务管理(xml配置)与spring自带连接数据库JdbcTemplate相关的知识,希望对你有一定的参考价值。
什么是事务,很通俗的话来说就是,我们日常生活中总会出现在银行转账的业务,加入A向B转账100元,此时A的账户中应该减少100元,B的账户中增加100元,但是如果在A转完账B还没有接受的时候,服务器出现了故障,一般银行的系统都会给出交易失败的提示,并且A的账户中总金额也不会变,那么这就类似一个事务。
1.事务是单个逻辑单元工作单位执行的一系列操作
2.多个操作作为整体向系统提交,要么都执行,要么都不执行
3.事务时一个不可分割的逻辑单元
事务的4大特性:原子性、一致性、隔离性、持久性(ACID)
下边我就模拟一个银行转账的事务:
首先导入依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!--以上4个是spring的核心--> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.2</version> </dependency> <!--以上两个是aop依赖--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <!--spring自带的数据库连接依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <!--mysql数据库依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency>
dao层接口
1 public interface AccountDao { 2 void addmonkey();//向表中增加金额 3 void submonkey();//向表中减少金额 4 }
dao层实现类
1 //dao层实现类需要继承JdbcDaoSupport,这是spring自带的数据库操作 2 public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { 3 @Override 4 public void addmonkey() { 5 //id为1的用户总金额增加100 6 String sql="update account set accountmonkey=accountmonkey+100 where accountid=1"; 7 this.getJdbcTemplate().update(sql); 8 } 9 10 @Override 11 public void submonkey() { 12 //id为2的用户总金额减少100 13 String sql="update account set accountmonkey=accountmonkey-100 where accountid=2"; 14 this.getJdbcTemplate().update(sql); 15 } 16 }
service层接口
public interface AccountService { void changeMonkey(); }
service实现类
public class AccountServiceImpl implements AccountService { private AccountDao dao; @Override public void changeMonkey() { dao.submonkey();//A先转账给B int result=5/0;//模拟异常,再调用完加钱的时候。 dao.addmonkey();//B接收money } public AccountDao getDao() { return dao; } public void setDao(AccountDao dao) { this.dao = dao; } }
实体类
1 public class Account { 2 private int accountid; 3 private String accountname; 4 private Double accountmonkey; 5 //省略set,get方法 6 }
增强类
//增强类 public class BeforeAdvice implements MethodBeforeAdvice, AfterReturningAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("前置增强"); } @Override public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("后置增强"); } }
测试类
1 public class Test { 2 public static void main(String[] args) { 3 ApplicationContext context = new ClassPathXmlApplicationContext("/applictionContext.xml"); 4 AccountService bean = context.getBean(AccountService.class); 5 bean.changeMonkey(); 6 } 7 }
核心配置文件applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 7 <!--引入外部文件,将数据库配置文件引入到本文件中--> 8 <context:property-placeholder location="classpath:database.properties"/> 9 <!--dao层--> 10 <bean id="dao" class="dao.impl.AccountDaoImpl"> 11 <!--注入数据源--> 12 <property name="dataSource" ref="dataSource"/> 13 </bean> 14 <!--向容器中添加service层实现类--> 15 <bean id="service" class="service.impl.AccountServiceImpl"> 16 <property name="dao" ref="dao"/> 17 </bean> 18 <!--向容器中添加通知的实现类--> 19 <bean id="advice1" class="advice.BeforeAdvice"></bean> 20 <!--顾问包装通知--> 21 <bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> 22 <property name="advice" ref="advice1"/> 23 <property name="mappedName" value="say"/> 24 </bean> 25 <!--顾问代理生成器--> 26 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 27 <property name="beanNames" value="service"/> 28 <property name="interceptorNames" value="advisor"/> 29 </bean> 30 <!--数据源,使用的数据源是spring自带的,讲道理这个自带的数据源不怎么好--> 31 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 32 <property name="username" value="${jdbc.username}"/> 33 <property name="password" value="${jdbc.password}"/> 34 <property name="url" value="${jdbc.url}"/> 35 <property name="driverClassName" value="${jdbc.driver}"/> 36 </bean> 37 <!--配置事务管理器--> 38 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 39 <property name="dataSource" ref="dataSource"/> 40 </bean> 41 <!--通知,tx标签是配置事务用的。propagation:事物的管理级别,isolation:事务的传播行为--> 42 <tx:advice transaction-manager="transactionManager" id="ad" > 43 <tx:attributes> 44 <tx:method name="changeMonkey" propagation="REQUIRED" isolation="READ_COMMITTED"/> 45 </tx:attributes> 46 </tx:advice> 47 <aop:config> 48 <aop:pointcut id="pointcut" expression="execution(* service.*.*(..))"/> 49 <aop:advisor advice-ref="ad" pointcut-ref="pointcut"/> 50 </aop:config> 51 </beans>
这里呢一个简单的事务就已经配置好了。
以上是关于spring事务管理(xml配置)与spring自带连接数据库JdbcTemplate的主要内容,如果未能解决你的问题,请参考以下文章