Spring事务操作-事务

目录

 

Spring事务操作-事务

 1.什么是事务

 (1)典型场景

2.事务的四个特性(俗称ACID特性)

(1)原子性

(2)一致性

(3)隔离性

(4)持久性

3.搭建事务(搭建银行转账环境)

4.创建数据库表

5.创建service ,搭建dao

6.测试类:

7.测试结果:


 

Spring事务操作-事务

 

 1.什么是事务

什么是事务:事物是数据库操作的最基本单元,逻辑上的一组操作,要么都成功,如果有一个失败所有操作都失败

 (1)典型场景

银行转账:

A转100给B,

A少100,B多100,

其中任何一个环节出现错误或者异常,这么一组操作都会失败

 

2.事务的四个特性(俗称ACID特性)

(1)原子性

要么都成功,一个失败=全部失败

(2)一致性

操作之前和操作之后总量不变,100块钱怎么转都还是100块钱,不会多也不会少

(3)隔离性

多事务操作互不影响

(4)持久性

事务最终提交后,表的数据完成更新

 

3.搭建事务(搭建银行转账环境)

配置文件:

<?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="org.example">

    </context:component-scan>
<!--数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/user_db?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="sise"/>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    </bean>

<!--    创建jdbcTemplate对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--需要注入数据源信息-->
        <property name="dataSource" ref="dataSource">

        </property>
    </bean>
</beans>

 

4.创建数据库表

数据库名为user_db 数据库表为t_account 

添加两条记录

 

5.创建service ,搭建dao

(1)service注入dao,在dao注入jdbcTemplate,在jdbcTemplate 注入 DataSource

package org.example.spring.service;

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

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

//    转账方法调用
    public void accountMoney(){
        //lucy少100
        userDao.reduceMoney();

        //mary多100
        userDao.addMoney();
    }

}

 

(2)在dao创建两个方法:多钱和少钱的方法,在service创建方法(转账的方法)

抽象类:

package org.example.spring.dao;

public interface UserDao {
//多钱的方法
    public void addMoney();
//少钱的方法
    public void reduceMoney();
}

实现类:

package org.example.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{
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public void reduceMoney() {
        String sql="update t_account set money=money-? where username=?";
        jdbcTemplate.update(sql,100,"lucy");

    }

    @Override
    public void addMoney() {
        String sql="update t_account set money=money+? where username=?";
        jdbcTemplate.update(sql,100,"mary");
    }


}

 

6.测试类:

package org.example.spring.test;

import org.example.spring.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBook
{
@Test
    public void test(){

    ApplicationContext context=
            new ClassPathXmlApplicationContext("bean1.xml");

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

    userService.accountMoney();

    }

}

 

7.测试结果:

先:

后:

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值