搭建环境:利用下边环境即可!!!
Spring整合MyBatis框架!!!-CSDN博客
需要修改的文件:
applicationContext.xml:
<!---->
UserService:
package com.by.service;import com.by.pojo.User;/** * Project: Spring - UserService
* Powered by scl On 2024-01-05 21:24:07
* 描述:
* * @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;@Servicepublic class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Autowiredprivate PlatformTransactionManager platformTransactionManager;@Autowiredprivate TransactionDefinition transactionDefinition;@Overridepublic void addUser(User user) {userMapper.addUser(user);}@Overridepublic 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:
insert into t_user(name, money)values (#{name}, #{money})update t_user set money=money+#{money} where name=#{target}update t_user set money=money-#{money} where name=#{source}
测试类:
/* * 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;/** * Project: Spring - ClientUserMoney
* Powered by scl On 2024-01-06 11:08:15
* 描述:
* * @author 孙臣龙 [1846080280@qq.com] * @version 1.0 * @since 17 */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class ClientUserMoney {@Autowiredprivate UserService userService;@Testpublic 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)回滚规则
可省略:运行时异常回滚,编译时异常不回滚