當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring事务操作-事务
生活随笔
收集整理的這篇文章主要介紹了
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&useUnicode=true&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 {@Autowiredprivate UserDao userDao;// 轉賬方法調用public void accountMoney(){//lucy少100userDao.reduceMoney();//mary多100userDao.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{@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.測試類:
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.測試結果:
先:
后:
?
?
總結
以上是生活随笔為你收集整理的Spring事务操作-事务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javaee 中文帮助文档_从中游公司跳
- 下一篇: 如何在jieba分词中加自定义词典_Py