搭建环境:利用下边环境即可!!!
需要修改的文件:
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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--ioc,扫描包,将其加入ioc工厂-->
<context:component-scan base-package="com.by.service, com.by.advice"></context:component-scan>
<!--加载db.properties-->
<context:property-placeholder location="db.properties"></context:property-placeholder>
<!--配置数据源
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName();
dataSource.setUrl();
dataSource.setUsername();
dataSource.setPassword();
-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--
SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sessionFactory = sessionFactoryBuilder.build("");
SqlSession sqlSession = sessionFactory.openSession();
Object mapper = sqlSession.getMapper();
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--扫描basePackage所指定的包下的所有接口,生成代理类并交给spring管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--mapper所在的包-->
<property name="basePackage" value="com.by.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--aop自动注入-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--配置事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--<property name="dataSource" ref="dataSource"></property>-->
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
<!--配置事务的相关属性-->
<bean class="org.springframework.transaction.support.DefaultTransactionDefinition">
<!--
1)隔离级别【有默认值】
DEFAULT:默认值,等价于REPEATABLE_READ
READ_UNCOMMITTED
READ_COMMITTED 脏读 x x
REPEATABLE_READ 脏读 不可重复度
SERIALIZABLE 脏读 不可重复度 幻读
2)事务的传播行为【有默认值】
REQUIRED:默认值,methodB()会加入到methodA()事务中
3)事务超时【有默认值】
默认-1:永不超时
4)是否只读【有默认值】
readOnly = false:默认值,适用于增删改
readOnly = true:不记录log(效率快),适用于查询
5)回滚规则
可省略:运行时异常回滚,编译时异常不回滚
-->
</bean>
</beans>
UserService:
package com.by.service;
import com.by.pojo.User;
/**
* <p>Project: Spring - UserService</p>
* <p>Powered by scl On 2024-01-05 21:24:07</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public interface UserService {
void addUser(User user);
void updateUser(String source, String target, float money);
}
UserServiceImpl:
package com.by.service;
import com.by.mapper.UserMapper;
import com.by.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private PlatformTransactionManager platformTransactionManager;
@Autowired
private TransactionDefinition transactionDefinition;
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
@Override
public void updateUser(String source, String target, float money) {
TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
try {
userMapper.subUserMoney(source,money);
System.out.println(6/0);
userMapper.addUserMoney(target,money);
platformTransactionManager.commit(transaction);
} catch (Exception e) {
e.printStackTrace();
platformTransactionManager.rollback(transaction);
}
}
}
UserMapper:
package com.by.mapper;
import com.by.pojo.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
public void addUser(User user);
void addUserMoney(@Param("target") String target, @Param("money") float money);
void subUserMoney(@Param("source") String source, @Param("money") float money);
}
UserMapper.xml:
<?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">
<mapper namespace="com.by.mapper.UserMapper">
<insert id="addUser" parameterType="com.by.pojo.User">
insert into t_user(name, money)
values (#{name}, #{money})
</insert>
<update id="addUserMoney">
update t_user set money=money+#{money} where name=#{target}
</update>
<update id="subUserMoney">
update t_user set money=money-#{money} where name=#{source}
</update>
</mapper>
测试类:
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.web;
import com.by.service.UserService;
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;
/**
* <p>Project: Spring - ClientUserMoney</p>
* <p>Powered by scl On 2024-01-06 11:08:15</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class ClientUserMoney {
@Autowired
private UserService userService;
@Test
public void updateUser(){
userService.updateUser("宋远桥","李星云",5f);
}
}
注意:
要实现事务需要将事务交给ioc容器进行管理。配置bean的思路,用接口的实现类去配置
1、PlatformTransactionManager
作用:是一个事务管理器,负责开启、提交或回滚事务
实现类:DataSourceTransactionManager(sqlSession)
2、TransactionDefinition
作用:定义事务的属性
实现类:DefaultTransactionDefinition
属性:
1)隔离级别【有默认值】
DEFAULT:默认值,等价于REPEATABLE_READ
READ_UNCOMMITTED
READ_COMMITTED 脏读 x x
REPEATABLE_READ 脏读 不可重复度
SERIALIZABLE 脏读 不可重复度 幻读
2)事务的传播行为【有默认值】
REQUIRED:默认值,methodB()会加入到methodA()事务中
3)事务超时【有默认值】
默认-1:永不超时
4)是否只读【有默认值】
readOnly = false:默认值,适用于增删改
readOnly = true:不记录log(效率快),适用于查询
5)回滚规则
可省略:运行时异常回滚,编译时异常不回滚