(1)事务是数据库操作最基本单元,逻辑上一组操作,要么都成功,如果有一个失败所有操作都失败
(2)典型场景:银行转账
lucy转账100元给mary
lucy少100 ,mary多100
(1)原子性
(2)一致性
(3)隔离性
(4)持久性
.
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.22version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.28version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.3.22version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>5.3.22version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.8version>
dependency>
<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"
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
">
<context:component-scan base-package="com.jin">context:component-scan>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">property>
bean>
<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="123456"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
bean>
beans>
CREATE TABLE `t_account` (
`id` varchar(20) NOT NULL ,
`username` varchar(50) NULL ,
`money` int(255) NULL ,
PRIMARY KEY (`id`)
)
INSERT INTO `t_account` (`id`, `username`, `money`) VALUES
('1', 'lucy', '1000')
('2', 'mary', '1000')
.
public class User {
private String id;
private String username;
private int money;
//Getter和Setter方法
//toString方法
...
}
(1)在dao创建两个方法:多钱和少钱的方法,在service创建方法(转账的方法)
//Dao接口
public interface UserDao {
//多钱
public void addMoney();
//少钱
public void reduceMoney();
}
//业务逻辑层
@Service
public class UserService implements UserDao {
@Autowired
private UserDao userDao;
@Override
public void addMoney() {
userDao.addMoney();
}
@Override
public void reduceMoney() {
userDao.reduceMoney();
}
}
(2)在Dao接口实现类中调用JdbcTemplate对象里面的方法实现添加操作
//Dao接口实现类
@Repository
@Primary //@Primary 告诉spring 在犹豫的时候优先选择哪一个具体的实现。
public class UserDaoImpl implements UserDao{
@Autowired
private JdbcTemplate jdbcTemplate;
//少钱的方法
//lucy转账100给mary
@Override
public void addMoney() {
String sql = "update user_db.t_account set money=money-? where username=?";
int i = jdbcTemplate.update(sql, 100, "lucy");
System.out.println(i);
}
//多钱的方法
//mary得到lucy转账的100
@Override
public void reduceMoney() {
String sql = "update user_db.t_account set money=money+? where username=?";
jdbcTemplate.update(sql,100,"mary");
}
}
public class test {
@Test
public void MyTest(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.addMoney();
userService.reduceMoney();
}
}
.
.
(1)上面的问题如何解决?
答:使用事务进行解决
(2)事务操作过程
管理操作有两种方式:
(1)基于注解方式(推荐使用)
(2)基于xml配置文件方式
(1)提供一个接口,代表事务管理器,这个接口针对不同的框架提供不同的实现类
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
(1)在spring配置文件引入名称空间 tx
<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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
(2)开启注解事务
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true">tx:annotation-driven>
(1)@Transactional :这个注解添加到类上面,也可以添加到方法上面
(2)如果把这个注解添加到类上面,这个类里面所有的方法都添加事务
(3)如果把这个注解添加到方法上面,为这个方法添加事务【常用】
@Service
@Transactional
public class UserService implements UserDao {
}
.