
01:Spring 事务控制 – 编程式事务控制相关对象
02:Spring 事务控制 – 基于XML的声明式事务控制:环境搭建
03:Spring 事务控制 – 基于XML的声明式事务控制:详细配置
04:Spring 事务控制 – 基于注解的声明式事务控制
1.1 什么是声明式事务控制
1.2 转账业务环境搭建
1.2.1 执行下面的sql语句 搭建mysql数据库环境
CREATE DATAbase test;
use test;
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '姓名',
`money` double(10, 2) NOT NULL COMMENT '余额'
);
INSERT INTO `account` VALUES ('tom', 5000.00);
INSERT INTO `account` VALUES ('lucy', 5000.00);
1.2.2 导入相关依赖
pom.xml
4.0.0 com.itheima itheima_spring_tx1.0-SNAPSHOT war org.springframework spring-context5.0.5.RELEASE org.aspectj aspectjweaver1.8.4 org.springframework spring-jdbc5.0.5.RELEASE org.springframework spring-test5.0.5.RELEASE c3p0 c3p00.9.1.1 mysql mysql-connector-java8.0.25 junit junit4.13.2
1.2.3 相关的代码
pojo层:
Account.java
package com.tian.domain;
public class Account {
private String name;
private double money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
Dao层:
AccountDao.java
package com.tian.dao;
public interface AccountDao {
// 取钱
public void out(String outMan, double money);
//存钱
public void in(String inMan, double money);
}
AccountDaoImpl.java
package com.tian.dao.impl;
import com.tian.dao.AccountDao;
import org.springframework.jdbc.core.JdbcTemplate;
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void out(String outMan, double money) {
jdbcTemplate.update("update account set money=money-? where name=?", money, outMan);
}
public void in(String inMan, double money) {
jdbcTemplate.update("update account set money=money+? where name=?", money, inMan);
}
}
Service层:
AccountService.java
package com.tian.service;
public interface AccountService {
public void transfer(String outMan, String inMan, double money);
}
AccountServiceImpl.java
package com.tian.service.impl;
import com.tian.dao.AccountDao;
import com.tian.service.AccountService;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan, money);
accountDao.in(inMan, money);
}
}
Controller层(模拟):
AccountController.java
package com.tian.controller;
import com.tian.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AccountController {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = app.getBean(AccountService.class);
accountService.transfer("tom", "lucy", 500);
}
}
1.2.4 配置applicationContext.xml
applicationContext.xml
1.2.5 测试环境搭建是否成功
现在的accout表:
运行controller方法:
运行成功:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)