我们新建一个Maven项目名,设置好Maven版本、配置文件以及Maven仓库
准备数据
数据库
# 删除spring_aop数据库
drop database if exists spring_aop;
# 创建spring_aop数据库
create database spring_aop;
# 使用spring_aop数据库
use spring_aop;
# 创建account表
create table account (
id int(11) auto_increment primary key,
accountNum varchar(20) default NULL,
money int(8) default 0
);
# 新增数据
insert into account (accountNum, money) values
("622200001",1000),("622200002",1000);
导包
导入Spring基础包
导入操作数据库、连接数据库、测试需要的包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.13.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
核心配置文件
配置自动扫包
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- bean definitions here -->
<context:component-scan base-package="dao"/>
<context:component-scan base-package="services"/>
<context:component-scan base-package="utils"/>
</beans>
配置数据源
<!--配置QueryRunner-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_aop"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
代码结构
代码编写
数据库连接工具类:ConnectionUtils.java
package utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* @ClassName ConnectionUtils
* @Description 数据库连接工具类
* @Author GengRui
* @Date 2021/3/3 16:05
*/
@Component
public class ConnectionUtils {
private ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
@Autowired
private ComboPooledDataSource dataSource;
/**
* 获得当前线程绑定的连接
*
* @return
*/
public Connection getThreadConnection() {
try {
// 看线程是否绑了连接
Connection conn = tl.get();
if (conn == null) {
// 从数据源获取一个连接
conn = dataSource.getConnection();
// 和线程局部变量 绑定
tl.set(conn);
}
// 返回线程连接
return tl.get();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 把连接和当前线程进行解绑
*/
public void removeConnection() {
tl.remove();
}
}
Account模块实体类:Account.java
package entity;
/**
* @ClassName Account
* @Description Account模块实体类
* @Author GengRui
* @Date 2021/3/3 15:41
*/
public class Account {
private Integer id;
private String accountNum;
private Integer money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccountNum() {
return accountNum;
}
public void setAccountNum(String accountNum) {
this.accountNum = accountNum;
}
public Integer getMoney() {
return money;
}
public void setMoney(Integer money) {
this.money = money;
}
}
Account模块Dao层:AccountDao.java
package dao;
import entity.Account;
/**
* @InterfaceName AccountDao
* @Description Account模块Dao层
* @Author GengRui
* @Date 2021/3/3 15:48
*/
public interface AccountDao {
/**
* 更新
*
* @param account
*/
void updateAccount(Account account);
/**
* 根据编号查询账户
*
* @param accountNum
* @return 如果没有结果就返回null,如果结果集超过一个就抛异常,如果有唯一的一个结果就返回
*/
Account findAccountByNum(String accountNum);
}
Account模块Dao层实现类:AccountDaoImpl.java
package dao.impl;
import dao.AccountDao;
import entity.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import utils.ConnectionUtils;
import java.sql.SQLException;
import java.util.List;
/**
* @ClassName AccountDaoImpl
* @Description Account模块Dao层实现类
* @Author GengRui
* @Date 2021/3/3 15:49
*/
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
// 数据库查询工具类
@Autowired
private QueryRunner runner;
// 数据库连接工具类
@Autowired
private ConnectionUtils connectionUtils;
/**
* 更新
*
* @param account
*/
public void updateAccount(Account account) {
try {
runner.update(connectionUtils.getThreadConnection(),
"update account set accountNum=?,money=? where id=?",
account.getAccountNum(), account.getMoney(), account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 根据编号查询账户
*
* @param accountNum
* @return 如果没有结果就返回null,如果结果集超过一个就抛异常,如果有唯一的一个结果就返回
*/
public Account findAccountByNum(String accountNum) {
List<Account> accounts = null;
try {
accounts = runner.query(connectionUtils.getThreadConnection(),
"select * from account where accountNum = ? ",
new BeanListHandler<Account>(Account.class),
accountNum);
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (accounts == null || accounts.size() == 0) {
// 如果没有结果就返回null
return null;
} else if (accounts.size() > 1) {
// 如果结果集超过一个就抛异常
throw new RuntimeException("结果集不唯一,数据有问题");
} else {
// 如果有唯一的一个结果就返回
return accounts.get(0);
}
}
}
Account模块Service层:AccountService.java
package services;
public interface AccountService {
/**
* 转账
*
* @param sourceAccount 转出账户
* @param targetAccount 转入账户
* @param money 转账金额
*/
void transfer(String sourceAccount, String targetAccount, Integer money);
}
Account模块Service层实现类:AccountServiceImpl.java
package services.impl;
import dao.AccountDao;
import entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import services.AccountService;
/**
* @ClassName AccountServiceImpl
* @Description Account模块Service层实现类
* @Author GengRui
* @Date 2021/3/3 16:25
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
/**
* 转账
*
* @param sourceAccount 转出账户
* @param targetAccount 转入账户
* @param money 转账金额
*/
public void transfer(String sourceAccount, String targetAccount, Integer money) {
// 查询原始账户
Account source = accountDao.findAccountByNum(sourceAccount);
// 查询目标账户
Account target = accountDao.findAccountByNum(targetAccount);
// 原始账号减钱
source.setMoney(source.getMoney() - money);
// 目标账号加钱
target.setMoney(target.getMoney() + money);
// 更新原始账号
accountDao.updateAccount(source);
// 造异常
// int i = 1 / 0;
// 更新目标账号
accountDao.updateAccount(target);
System.out.println("转账完毕");
}
}
Account模块测试类:AccountTest.java
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;
import services.AccountService;
/**
* @ClassName AccountTest
* @Description Account模块测试类
* @Author GengRui
* @Date 2021/3/3 16:29
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AccountTest {
@Autowired
private AccountService accountService;
@Test
public void testTransfer() {
accountService.transfer("622200001", "622200002", 100);
}
}
执行结果