事务_简单转账业务模拟mybatis、Spring框架
2021-04-13 00:28
阅读:494
标签:else manage 运行 type tac mamicode load rollback 文件信息
简单搭建一个mybatis环境实现转账的模拟操作
项目列表
pom.xml依赖
junit
junit
4.12 org.mybatis
mybatis
3.5.3 log4j
log4j
1.2.17 mysql
mysql-connector-java
5.1.47 org.springframework
spring-context
5.0.2.RELEASE org.springframework
spring-test
5.0.2.RELEASE
数据库sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`account_name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`money` float NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
INSERT INTO `account` VALUES (1, ‘aa‘, 1000);
INSERT INTO `account` VALUES (2, ‘bb‘, 1000);
INSERT INTO `account` VALUES (3, ‘cc‘, 1000);
SET FOREIGN_KEY_CHECKS = 1;
mybatis-config.xml
log4j.pro
log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
工具类
public class SessionFactoryUtils {
//声明一个工厂对象
private static SqlSessionFactory factory;
//在静态代码块中创建会话工厂
static {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//得到输入流
try (InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml")) {
factory = builder.build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSessionFactory() {
return factory;
}
public static SqlSession getSession() {
return factory.openSession(); //自动提交事务
}
}
Config
@Configuration
@ComponentScan("com.qiliang")
public class Config { }
实体类
public class Account {
private int id;
private String accountName;
private Float money;
//省略set、get、toString
}
dao
public interface AccountDao {
@Select("select * from account")
List findAll();
@Update("update account set money=money - #{money} where account_name = #{name}")
void out(@Param("name")String outName,@Param("money")float money);
@Update("update account set money=money + #{money} where account_name = #{name}")
void in(@Param("name")String inName,@Param("money")float money);
}
service
@Service
public class AccountService {
private SqlSession session = SessionFactoryUtils.getSession();
private AccountDao mapper =
session.getMapper(AccountDao.class);
//查询测试
public List findAll() {
return mapper.findAll();
}
/**
* @param outName 支出账户
* @param inName 收入账户
* @param money 转账金额
*/
public boolean transfer(String outName, String inName, float money) {
try {
//转出
mapper.out(outName, money);
int i=1/0; //模拟异常
//转入
mapper.in(inName, money);
} catch (Exception e) {
//事务回滚
session.rollback();
System.out.println("失败原因:"+e.getMessage());
return false;
}
//提交事务
session.commit();
return true;
}
}
controller
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class AccountWeb {
@Autowired
private AccountService accountService;
@Test
public void test01(){
List list = accountService.findAll();
for (Account account : list) { System.out.println(account); }
}
/**
* outName 支出账户
* inName 收入账户
* money 转账金额
*/
@Test
public void test02(){
boolean result = accountService.transfer("aa", "bb", 100);
if(result){
System.out.println("转账成功");
} else {
System.out.println("转账失败");
}
}
}
结果不做演示,可以多次把异常 和 回滚事务的代码注释掉,看运行的区别
事务_简单转账业务模拟mybatis、Spring框架
标签:else manage 运行 type tac mamicode load rollback 文件信息
原文地址:https://www.cnblogs.com/liqiliang1437/p/13346688.html
评论
亲,登录后才可以留言!