一、知识点总结
spring基于注解的 事务配置:
@Transactional注解 可以放在 类的上面
放在类的上面 代表当前类下的所有方法 都添加了事务
@Transactional注解 可以放在 方法上面
放在方法上面 代表当前这个方法 添加了事务 ,其它方法没有事务;
二、工程架构
(1)架构图
(2)案例Code
1.com.oracle.dao包:
package com.oracle.dao;
public interface AccountDao {
public void outer(String outer, double money);//汇款人
public void inner(String inner, double money);//收款人
}
2.com.oracle.daoImpl包:
package com.oracle.daoImpl;
import com.oracle.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
//AccountDaoImpl IOC
@Repository("accountDaoId")
public class AccountDaoImpl implements AccountDao {
/**
* jdbcTemplate
*
* @param outer
* @param money
*/
//依赖注入
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void outer(String outer, double money) {
String sql = "update account set balance = balance - ? where name = ?";
int a = jdbcTemplate.update(sql, money, outer);
}
@Override
public void inner(String inner, double money) {
String sql = "update account set balance = balance + ? where name = ?";
int a = jdbcTemplate.update(sql, money, inner);
}
}
3.com.oracle.service包:
package com.oracle.service;
public interface AccountService {
/**
*
* @param outer 汇款人
* @param inner 收款人
* @param money 转账金额
*/
public void transfer(String outer, String inner, int money);
}
4.com.oracle.serviceImpl包:
package com.oracle.serviceImpl;
import com.oracle.dao.AccountDao;
import com.oracle.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
//AccountServiceImpl IOC
@Service
/*@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)*/
public class AccountServiceImpl implements AccountService {
//依赖注入
@Autowired
@Qualifier("accountDaoId")
private AccountDao accountDao;
//为方法加事务,propagation传播方式,isolation隔离级别
@Override
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public void transfer(String outer, String inner, int money) {
//调用 转账
accountDao.outer(outer,money);
//转账的时候出现了异常
int i = 1/0;
//收款
accountDao.inner(inner,money);
}
}
5. com.oracle.test包:
package com.oracle.test;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.oracle.service.AccountService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.Connection;
import java.sql.SQLException;
public class Demo {
@Test
public void Testc3p0() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
dataSource.setUser("root");
dataSource.setPassword("123");
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
@Test
public void Testdruid() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("123");
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
}
@Test
public void Testtansfer(){
ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
AccountService accountService = c.getBean(AccountService.class);
accountService.transfer("张三","王五",200);
System.out.println("执行完毕!");
}
}
6.com.oracle.vo包:
package com.oracle.vo;
public class Account {
private Integer id;
private String name;
private Double balance;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", balance=" + balance +
'}';
}
}
7.config包:
1)applicationContext.xml
=========================
2)db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username=root
password=123