1、SSM 事务
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.8</version>
</dependency>
src/main/resources/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 https://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.yscyber.ssm.service"/>
<!-- 引入 jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源(交予 Spring 容器管理) -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc_driver}"/>
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_username}"/>
<property name="password" value="${jdbc_password}"/>
</bean>
<!-- 配置 SqlSessionFactory -->
<!-- id 一般为 sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!-- 配置 Mapper 扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yscyber.ssm.repo"/>
</bean>
<!-- 事务注解扫描 -->
<tx:annotation-driven/>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
2、测试
CREATE TABLE xxx(
id INT NOT NULL,
name VARCHAR(3) NOT NULL
);
package com.yscyber.ssm.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class MyObj {
private Integer id;
private String name;
}
package com.yscyber.ssm.repo;
import com.yscyber.ssm.pojo.MyObj;
import java.util.List;
public interface MyObjRepo {
List<MyObj> listAllMyObjs();
int insertMyObj(MyObj myObj);
}
package com.yscyber.ssm.service;
import com.yscyber.ssm.pojo.MyObj;
import java.util.List;
public interface MyObjService {
List<MyObj> listAllObjs();
/**
* 测试 事务 专用方法,无实际意义
*/
int insertTwoMyObjs(MyObj myObj1, MyObj myObj2);
}
package com.yscyber.ssm.service.impl;
import com.yscyber.ssm.pojo.MyObj;
import com.yscyber.ssm.repo.MyObjRepo;
import com.yscyber.ssm.service.MyObjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class MyObjServiceImpl implements MyObjService {
@Autowired
private MyObjRepo myObjRepo;
@Override
public List<MyObj> listAllObjs() {
return myObjRepo.listAllMyObjs();
}
@Override
@Transactional
public int insertTwoMyObjs(MyObj myObj1, MyObj myObj2) {
myObjRepo.insertMyObj(myObj1);
int i = 1/0; // 故意设置异常
return myObjRepo.insertMyObj(myObj2);
}
}
package com.yscyber.ssm;
import com.yscyber.ssm.pojo.MyObj;
import com.yscyber.ssm.service.MyObjService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MyTest {
@Autowired
private MyObjService myObjService;
@Test
public void test1() {
MyObj myObj1 = new MyObj(99, "PRC");
MyObj myObj2 = new MyObj(98, "USA");
// 如果事务有效,两条数据都无法插入至数据库
myObjService.insertTwoMyObjs(myObj1, myObj2);
}
}
网友评论