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{
@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 {
@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&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<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();
}
}