环境搭建
数据源:c3p0
数据库:Mysql8
数据表:任意两张表即可,sql是修改操作的
创建Maven项目
导入相关依赖
<!--导入Spring的核心jar包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--c3p0管理数据库连接池-->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--数据库-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--spring的jdbc-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--spring的orm包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--spring的事务相关-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--与junit整合-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
编写dao层
两个dao方法操作两张表
package com.xzy.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class ScoreDao {
@Autowired
JdbcTemplate jdbcTemplate;
public void updateScore(){
String sql = "UPDATE score SET scoreid=1,scorenum=88 WHERE scoreid=1";
jdbcTemplate.update(sql);
}
}
import org.springframework.stereotype.Repository;
@Repository
public class StuDao {
@Autowired
JdbcTemplate jdbcTemplate;
public void updateStu(){
String sql = "UPDATE stu SET stuname ='hello',stuage=15 WHERE stuid=1";
jdbcTemplate.update(sql);
}
}
编写配置文件
jdbcConfig配置文件
user=root
password=*******
jdbcUrl=jdbc:mysql://localhost:3306/XXX?characterEncoding=UTF8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
driverClass=com.mysql.cj.jdbc.Driver
spring 的applicationContext.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.xzy"></context:component-scan>
<!--引入外部的配置文件-->
<context:property-placeholder location="classpath:jdbcConfig.properties"/>
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="driverClass" value="${driverClass}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="pooledDataSource"/>
</bean>
<!--配置事务-->
<!--1:配置事务管理器让其管理事务-->
<bean id="sourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--控制数据源-->
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
<!--2:开启基于注解的事务模式-->
<tx:annotation-driven transaction-manager="sourceTransactionManager"/>
<!--3:给事务方法添加注解-->
</beans>
编写Service层
一个service方法
package com.xzy.service;
import com.xzy.dao.ScoreDao;
import com.xzy.dao.StuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class Transactions {
@Autowired
StuDao stuDao;
@Autowired
ScoreDao scoreDao;
//注解 @Transactional 标注这个方法是一个事务方法
/**@Transactional 的属性
* isolation 事务的隔离级别
* propagation 事务的传播行为
*
* rollbackFor 哪些异常事务可以不回滚
* rollbackForClassName
*
* noRollbackFor 哪些异常事务需要回滚
* noRollbackForClassName
*
* readOnly-boolean 设置只读
* timeOut 超出指定时长停止事务并回滚
*/
@Transactional
public void updateTwoTable(){
stuDao.updateStu();
scoreDao.updateScore();
}
}
此时,service中标注了注解的方法已经加上了事务,要么都成功,要么都失败。