1.什么是事务
简单理解你:事务就是将一系列动作看成是一次工作单元,又涉及到套娃了
举个栗子:
转账操作,张三向李四转账100块钱,在转账途中,突然停电,但是账单已经发出去了,这个时候如果没有事务的话,那么张三向李四转账的100块钱将不翼而飞
事务的作用,将张三和李四的的转账看成一个动作,要么全部成功,要么全部不成功
2.spring如何实现事务
2.1 所需依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<!--spring事务依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--mybatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--mybatis和spring整合的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--druid的连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>
2.2 spring配置文件:
<?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:mvc="http://www.springframework.org/schema/mvc" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--springmvc的配置-->
<!--包扫描 扫描com.ykq以及该包下的子包-->
<context:component-scan base-package="com.xw"/>
<!--spring整合mybatis的配置-->
<!--数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!--mysql驱动为8.0以后必须使用时区-->
<property name="url" value="jdbc:mysql://localhost:3306/aaasql?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--spring封装了一个类SqlSessionFactoryBean类,可以把mybatis中的配置-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:/mapper/*.xml"/>
</bean>
<!--为指定dao包下的接口生产代理实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<!--它会为com.ykq.dao包下的所有接口生产代理实现类-->
<property name="basePackage" value="com.xw.dao"/>
</bean>
<!-- --================以下内容是关于事务的配置===================------>
<!--事务切面管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务管理注解的驱动-->
<tx:annotation-driven/>
</beans>
2.3 创建dao层:
public interface StudentDao {
int Thansfer(@Param("id") int id, @Param("money") double money);
}
2.4 创建mapper映射文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace必须和dao接口的名称一模一样-->
<mapper namespace="com.xw.dao.StudentDao">
<update id="Thansfer">
update tbl_student set money=money+#{money} where id=#{id}
</update>
</mapper>
2.5 创建service接口与实现类:
2.5.1 接口:
public interface Studentim {
int zhuangzhang(int id,int uid,double money);
}
2.5.2 实现类:
@Service
public class StudentimService implements Studentim {
@Autowired
private StudentDao studentDao;
@Transactional //spring操作事务
@Override
public int zhuangzhang(int id, int uid, double money) {
studentDao.Thansfer(id,-money);
int c=10/0; //故意让他报错,以便演示
studentDao.Thansfer(uid,money);
return 0;
}
}
2.6 创建测试类:这里不在用接口测试
public class test {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("classpath:spring.xml");
Studentim userServieImpl = (Studentim) app.getBean("studentimService");
userServieImpl.zhuangzhang(4,5,100);
}
}
2.7 测试运行:
2.7.1 无异常测试:
2.7.2 异常测试:
当异常出现,异常前与异常后金额并无变化,证明事务已启用成功
以上便是javaSpring操作事务中的部分内容,如有漏缺请在下方留言告知,我会及时补充
标签:事务,javaSpring,int,spring,org,public,money,操作,id From: https://www.cnblogs.com/9--1/p/17636871.html