Spring

spring 声明事务管理(注解)

2018-06-08  本文已影响9人  DouDouZH

一、步骤

1、配置事务管理器
image.png
2、配置事务注解
image.png
3、要使用事务方法所在类上添加注解
image.png

二、代码

Service层OrdersService.java

package work.zhangdoudou.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import work.zhangdoudou.Dao.OrdersDao;

//3、在要添加事务的类上添加注解
@Transactional
public class OrdersService {
    
    @Autowired
    private OrdersDao ordersDao;
    
    /*
     * 调用dao层
     * 业务逻辑,写转账业务
     */
    public void accountMoney(String name1,String name2,Integer money) {
        //张三少1000块钱
        ordersDao.lessMoney(name1,money);
        //int i=10/0;
        //李四多1000快钱
        ordersDao.moreMoney(name2,money);
    }
}

dao层OrdersDao.java

package work.zhangdoudou.Dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class OrdersDao {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /*
     * 对数据库操作,不屑业务
     */
    
    //张三少钱的方法
    public void lessMoney(String name,Integer money) {
        String sql="UPDATE account SET salary=salary-? WHERE u_name=?";
        jdbcTemplate.update(sql,money,name);
    }
    
    
    //李四多钱的方法
    public void moreMoney(String name,int money) {
        String sql="UPDATE account SET salary=salary+? WHERE u_name=?";
        jdbcTemplate.update(sql,money,name);
    }
}

配置文件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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
                        
    <!-- 开启器注解扫描 -->
    <context:component-scan base-package="work.zhangdoudou"></context:component-scan>
    
    <!--  value 从配置文件里面 db.properties中取值 -->
    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}" ></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
    </bean> 
    
    <bean id="ordersDao" class="work.zhangdoudou.Dao.OrdersDao"></bean>
    <bean id="ordersService" class="work.zhangdoudou.Service.OrdersService"></bean>
    
    <!-- 创建jdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- dataSource 传递到模板中 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 1、 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入dataSource -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
   <!-- 开启事务的注解 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
   
</beans>

测试类Test1.java

package work.zhangdoudou.Test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import work.zhangdoudou.Service.OrdersService;

public class Test1 {

    @Test
    public void test() {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        OrdersService ordersService=(OrdersService) context.getBean("ordersService");
        ordersService.accountMoney("张三","李四",1000);
    }

}

三、正常的运行结果

image.png

四、让其出现异常的运行结果

异常代码


异常代码

数据库结果没有改变


数据库结果
上一篇 下一篇

猜你喜欢

热点阅读