spring-210729-01---了解事务&转账实例

spring-210729-01—了解事务&转账实例


了解事务

什么是事务:
	事务是数据库操作最基本的单元,逻辑上的一组操作,要么都成功,如果有一个失败所有操作都失败。
事务的特性(ACID):
	原子性
	一致性
	隔离性
	持久性

转账实例

people_A 和 people_B 之间进行转账
在service层(业务逻辑层)调用dao层方法
dao层(数据访问层)少钱和多钱的方法
这个转账实例如果发生了异常,那么会发生问题,一个sql执行了,而另一个没有执行

UserDao.java

package com.bgy.spring.dao;
public interface UserDao {

    // 多钱
    void addMoney();

    // 少钱
    void reduceMoney();
}

UserDaoImpl.java

package com.bgy.spring.dao;

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

@Repository
public class UserDaoImpl implements UserDao{

    // 注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 多钱
     */
    @Override
    public void addMoney() {
        String sql = "update t_account set money=money-? where id=?";

        int update = jdbcTemplate.update(sql, "100","1001");
        System.out.println(update);
    }

    /**
     * 少钱
     */
    @Override
    public void reduceMoney() {
        String sql = "update t_account set money=money+? where id=?";

        int update = jdbcTemplate.update(sql, "100", "1002");
        System.out.println(update);
    }
}

UserService.java

package com.bgy.spring.service;

import com.bgy.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    // 注入UserDao
    @Autowired
    private UserDao userDao;

    /**
     * 转账
     */
    public void accountMoney(){
        userDao.reduceMoney();
        userDao.addMoney();
    }
}

bean01.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">


    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.bgy.spring"></context:component-scan>

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring_test_210728_01?serverTimezone=UTC&amp;useSSL=false" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>

    <!-- 创建JdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

        <!-- 注入DataSource -->
        <property name="dataSource" ref="dataSource"></property>

    </bean>
</beans>

TestUser01.java

import com.bgy.spring.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser01 {

    /**
     * 测试转账功能
     */
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");

        UserService userService = context.getBean("userService", UserService.class);
        userService.accountMoney();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值