javascript
Spring事务操作-事务
目錄
?
Spring事務(wù)操作-事務(wù)
?1.什么是事務(wù)
?(1)典型場(chǎng)景
2.事務(wù)的四個(gè)特性(俗稱ACID特性)
(1)原子性
(2)一致性
(3)隔離性
(4)持久性
3.搭建事務(wù)(搭建銀行轉(zhuǎn)賬環(huán)境)
4.創(chuàng)建數(shù)據(jù)庫(kù)表
5.創(chuàng)建service ,搭建dao
6.測(cè)試類:
7.測(cè)試結(jié)果:
?
Spring事務(wù)操作-事務(wù)
?
?1.什么是事務(wù)
什么是事務(wù):事物是數(shù)據(jù)庫(kù)操作的最基本單元,邏輯上的一組操作,要么都成功,如果有一個(gè)失敗所有操作都失敗
?(1)典型場(chǎng)景
銀行轉(zhuǎn)賬:
A轉(zhuǎn)100給B,
A少100,B多100,
其中任何一個(gè)環(huán)節(jié)出現(xiàn)錯(cuò)誤或者異常,這么一組操作都會(huì)失敗
?
2.事務(wù)的四個(gè)特性(俗稱ACID特性)
(1)原子性
要么都成功,一個(gè)失敗=全部失敗
(2)一致性
操作之前和操作之后總量不變,100塊錢怎么轉(zhuǎn)都還是100塊錢,不會(huì)多也不會(huì)少
(3)隔離性
多事務(wù)操作互不影響
(4)持久性
事務(wù)最終提交后,表的數(shù)據(jù)完成更新
?
3.搭建事務(wù)(搭建銀行轉(zhuǎn)賬環(huán)境)
配置文件:
<?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"><!-- 開(kāi)啟組件掃描--><context:component-scan base-package="org.example"></context:component-scan> <!--數(shù)據(jù)庫(kù)連接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"><property name="url" value="jdbc:mysql://localhost:3306/user_db?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="sise"/><property name="driverClassName" value="com.mysql.jdbc.Driver"/></bean><!-- 創(chuàng)建jdbcTemplate對(duì)象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--需要注入數(shù)據(jù)源信息--><property name="dataSource" ref="dataSource"></property></bean> </beans>?
4.創(chuàng)建數(shù)據(jù)庫(kù)表
數(shù)據(jù)庫(kù)名為user_db 數(shù)據(jù)庫(kù)表為t_account?
添加兩條記錄
?
5.創(chuàng)建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 {@Autowiredprivate UserDao userDao;// 轉(zhuǎn)賬方法調(diào)用public void accountMoney(){//lucy少100userDao.reduceMoney();//mary多100userDao.addMoney();}}?
(2)在dao創(chuàng)建兩個(gè)方法:多錢和少錢的方法,在service創(chuàng)建方法(轉(zhuǎn)賬的方法)
抽象類:
package org.example.spring.dao;public interface UserDao { //多錢的方法public void addMoney(); //少錢的方法public void reduceMoney(); }實(shí)現(xiàn)類:
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{@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic void reduceMoney() {String sql="update t_account set money=money-? where username=?";jdbcTemplate.update(sql,100,"lucy");}@Overridepublic void addMoney() {String sql="update t_account set money=money+? where username=?";jdbcTemplate.update(sql,100,"mary");}}?
6.測(cè)試類:
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 { @Testpublic void test(){ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");UserService userService = context.getBean("userService", UserService.class);userService.accountMoney();}}?
7.測(cè)試結(jié)果:
先:
后:
?
?
總結(jié)
以上是生活随笔為你收集整理的Spring事务操作-事务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: javaee 中文帮助文档_从中游公司跳
- 下一篇: Spring的AOP-AspectJ注解