spring 事务管理(转账的例子)

这里通过转账来对spring 例子进行说明
小明是个程序员,七夕节到了,小明想着给老婆发红包,用自己的开发的某个支付平台,给老婆发了1314的红包,但是程序出现了问题,发生了异常,结果不仅老婆没收到钱,小明的1314元钱也丢失了,这把小明气坏了,决心更改自己的支付平台,通过学习spring ,他想到了事务管理,通过回滚来解决这个问题

1 首先整理下配置管理需要的jar包

在这里插入图片描述

2 创建数据库

在这里插入图片描述

3 创建业务逻辑层和Dao层
业务逻辑代码
package tx;

/**
 * @Author: Han Yu
 * @Description:
 * @Date: Create in 19:17 2019/1/13
 */
public class OrdersService {
    private OrdersDao ordersDao;

    public void setOrdersDao(OrdersDao ordersDao) {
        this.ordersDao = ordersDao;
    }
    public void accountMoney(){
        //张三少钱
        ordersDao.lessMoney();
        //但是如果中间出现异常  需要回滚
        int i=10/0;
        //李四多钱
        ordersDao.moreMoney();
    }
}

dao层代码
package tx;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @Author: Han Yu
 * @Description:
 * @Date: Create in 19:17 2019/1/13
 */
public class OrdersDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    //少钱的方法
    public void lessMoney(){
        String sql="update account set salary=salary-? where username=?";
        jdbcTemplate.update(sql,1000,"张三");
    }
    //多钱的方法
    public void moreMoney(){
        String sql="update account set salary=salary+? where username=?";
        jdbcTemplate.update(sql,1000,"李四");
    }
}

4 基于配置文件实现事务管理
<?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: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/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"> <!-- bean definitions here -->

        <!--创建对象-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!--注入属性值-->
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///springjdbc"></property>
            <property name="user" value="root"></property>
            <property name="password" value="mysql"></property>
        </bean>
        <!--创建jdbcTemplate对象-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <!--dataSource传入jdbcTemplate对象中-->
            <property name="dataSource" ref="dataSource"></property>
        </bean>

        <bean id="ordersDao" class="tx.OrdersDao">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        <bean id="ordersService" class="tx.OrdersService">
            <property name="ordersDao" ref="ordersDao"></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="account*"/>-->
                <tx:method name="accountMoney"/>
            </tx:attributes>

        </tx:advice>
        <!--第三步配置切面 -->
        <aop:config>
            <!--切入点-->
            <aop:pointcut id="pointcut1" expression="execution(* tx.OrdersService*(..))"></aop:pointcut>
            <!--切面-->
            <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"></aop:advisor>
        </aop:config>
</beans>
5 注解的方式开发

1 配置事务管理器

 <!--第一步 配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>

2 开启事务注解扫描

  <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

3 在使用事务的方法上所在类上面添加注解

package txaspecj;

import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Han Yu
 * @Description:
 * @Date: Create in 19:17 2019/1/13
 */
@Transactional
public class OrdersService {
    private OrdersDao ordersDao;

    public void setOrdersDao(OrdersDao ordersDao) {
        this.ordersDao = ordersDao;
    }
    public void accountMoney(){
        //张三少钱
        ordersDao.lessMoney();
        //但是如果中间出现异常  需要回滚
        int i=10/0;
        //李四多钱
        ordersDao.moreMoney();
    }
}

注解开发总的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: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/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"> <!-- bean definitions here -->

        <!--创建对象-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!--注入属性值-->
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///springjdbc"></property>
            <property name="user" value="root"></property>
            <property name="password" value="mysql"></property>
        </bean>
        <!--创建jdbcTemplate对象-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <!--dataSource传入jdbcTemplate对象中-->
            <property name="dataSource" ref="dataSource"></property>
        </bean>

        <bean id="ordersDao" class="tx.OrdersDao">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        <bean id="ordersService" class="tx.OrdersService">
            <property name="ordersDao" ref="ordersDao"></property>
        </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>
</beans>

6 测试类
package tx;

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

/**
 * @Author: Han Yu
 * @Description:
 * @Date: Create in 19:28 2019/1/13
 */
public class OrdersTest {
    @Test
    public void TestDemo(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        OrdersService ordersService = (OrdersService)context.getBean("ordersService");
        ordersService.accountMoney();

    }
}

package txaspecj;

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

/**
 * @Author: Han Yu
 * @Description:
 * @Date: Create in 19:28 2019/1/13
 */
public class OrdersTest {
    @Test
    public void TestDemo(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        OrdersService ordersService = (OrdersService)context.getBean("ordersService");
        ordersService.accountMoney();

    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值