Spring -> 用户金钱交易(数据库)事务实现

1.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: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 
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置spring创建IOC容器时要扫描的包-->
    <context:component-scan base-package="test10month.test1016"/>

    <!--配置数据源-->
    <bean id="druidRuntimeException" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/rod"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="druidRuntimeException"/>
    </bean>

    <!--配置aop-->
    <aop:aspectj-autoproxy />
</beans>

2.操作数据库类-接口

public interface Affair {
  /**
   * 功能描述:对数据库内money的增加,减少
   * @param money :多少钱需要相互转移
   * @param idadd : 需要加钱的id
   * @param reduce :需要减钱的id
   */
  void update(int money,int idadd,int reduce);
}

@Repository
public class AffairImp implements Affair {
  @Autowired
  private JdbcTemplate jdbcTemplate;

  /**
   * 功能描述:对数据库内money的增加,减少
   */
  @Override
  public void update(int money, int idadd, int idreduce) {
    //对id用户增加?money
    String sqladd = "update customer set money=money+? where id=?";
    Object[] objadd = {money, idadd};
    jdbcTemplate.update(sqladd, objadd);
    //对id用户减少?money
    String sqlreduce = "update customer set money=money-? where id=?";
    Object[] objreduce = {money, idreduce};
    jdbcTemplate.update(sqlreduce, objreduce);
  }
}

3.Service----服务类:

@Service
public class AffirService {
  @Autowired
  private AffairImp affairImp;

  /**
   * Description: transaction交易,使得两个id用户互相交易金钱
   * @param money 交易的金钱数额
   * @param idadd 钱增加的人对应的id
   * @param idreduce 钱减少的人对应的id
   */
  public void transaction(int money, int idadd, int idreduce) {
    affairImp.update(money, idadd, idreduce);
  }
}

4.测试类

package test10month.test1016.affair;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 功能描述:
 * @ClassName AffairTest
 * @Author: 罗德
 * @Create: 2020-10-16 22:41
 * @Version 1.0
 */
public class AffairTest {
  public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test10month/test1016/jdbc.xml");
    AffirService affirService = context.getBean("affirService", AffirService.class);
    affirService.transaction(100,1,2);
  }
}

5.结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值