基于 XML
application.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="cn.lichenghao.tx"></context:component-scan><bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url"value="jdbc:mysql://192.168.1.240:3306/abc?useUnicode=true&characterEncoding=utf-8"/><property name="username" value="root"/><property name="password" value="Root@123456"/></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:tx/mybatis-config.xml"/><property name="mapperLocations" value="classpath:tx/mappers/*.xml"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.lichenghao.tx.dao"/></bean><bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"><property name="dataSource" ref="dataSource"/></bean><tx:advice id="interceptor" transaction-manager="transactionManager"><tx:attributes><tx:method name="*add*" propagation="REQUIRED"/><tx:method name="*delete*" propagation="REQUIRED"/><tx:method name="*update*" propagation="REQUIRED"/><tx:method name="*query*" read-only="true"/></tx:attributes></tx:advice><aop:config><aop:pointcut id="myPointcut" expression="execution(* cn.lichenghao.tx.dao.*.*(..))"/><aop:pointcut id="myPointcut2" expression="execution(* cn.lichenghao.tx.service.*.*(..))"/><aop:advisor advice-ref="interceptor" pointcut-ref="myPointcut"/><aop:advisor advice-ref="interceptor" pointcut-ref="myPointcut2"/></aop:config></beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings><typeAliases><package name="cn.lichenghao.tx.entity"/></typeAliases></configuration>
准备 dao、servcie、mapper 文件
@Componentpublic interface UserDao {List<UserInfo> listUser();int addUser(UserInfo userInfo);}public interface UserService {List<UserInfo> listUser();int addUser(UserInfo userInfo);}@Servicepublic class UserServiceImpl implements UserService {@Resourceprivate UserDao userDao;@Overridepublic List<UserInfo> listUser() {return userDao.listUser();}@Overridepublic int addUser(UserInfo userInfo) {int added = userDao.addUser(userInfo);if (added > 0) {throw new RuntimeException("测试回滚!");}return added;}}
mapper
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.lichenghao.tx.dao.UserDao"><select id="listUser" resultType="cn.lichenghao.tx.entity.UserInfo">SELECT * FROM user;</select><insert id="addUser" parameterType="cn.lichenghao.tx.entity.UserInfo">insert into user(`id`,`name`,`desc`) values (#{id},#{name},#{desc})</insert></mapper>
单元测试
@Testpublic void addUser() {ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("tx/application.xml");UserService userService = context.getBean(UserService.class);UserInfo user = new UserInfo(3, "3333", "3333");System.out.println(userService.addUser(user));}
基于注解
调整 application.xml
开启注解事务支持 
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="cn.lichenghao.tx"></context:component-scan><tx:annotation-driven/><bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url"value="jdbc:mysql://192.168.1.240:3306/abc?useUnicode=true&characterEncoding=utf-8"/><property name="username" value="root"/><property name="password" value="Root@123456"/></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:tx/mybatis-config.xml"/><property name="mapperLocations" value="classpath:tx/mappers/*.xml"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.lichenghao.tx.dao"/></bean><bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"><property name="dataSource" ref="dataSource"/></bean></beans>
接口使用注解即可
@Transactional(propagation = Propagation.REQUIRED)@Overridepublic int addUser(UserInfo userInfo) {int added = userDao.addUser(userInfo);if (added > 0) {throw new RuntimeException("测试回滚!");}return added;}
