Service: 业务操作 创建转账的方法,调用dao两个方法
Dao:数据库操作,不写主要代码, 创建两个方法(少钱、多钱)
Spring事务管理API:提供一个接口,代表事物管理器,这个接口针对不同的矿建提供不同的实现类
我们此处要用的实现类:DataSourceTransationManager
第一步:XML配置文件进行配置:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启扫描-->
<context:component-scan base-package="com.company.Spring5.Shiwu"> </context:component-scan>
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/user_db" />
<property name="username" value="root" />
<property name="password" value="admin" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
<!--用set方法注入 dataSource属性-->
</bean>
<!--创建事物管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启事物注解-->
<!-- transaction-manager后面的值,就是我们要开启的书屋管理器的id值 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
第二步:创建service、搭建dao,完成对象创建和注入关系
package com.company.Spring5.Shiwu.dao;
public interface UserDao {
//多钱的方法
void addMoney();
// 少钱的方法
void reduceMoney();
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao{
//注入JdbcTemplate对象
@Autowired //根据类型注入 此类型在配置文件中写过
private JdbcTemplate jdbcTemplate;
//实现Lucy转账100给Mary
@Override//多钱的方法
public void addMoney() {
String sql = "update t_account set money=money+? where username=? ";
jdbcTemplate.update(sql,100,"mary");
}
@Override//少钱的方法
public void reduceMoney() {
String sql = "update t_account set money=money-? where username=?";
jdbcTemplate.update(sql,100,"lucy");
}
}
import com.company.Spring5.Shiwu.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
//如果把这个注解加到类上面,表示类中的所有方法都添加事物
//如果把这个注解添加到方法上面,仅仅是为这个方法添加事物
@Service
@Transactional
public class UserService {
// 注入dao对象
@Autowired // 根据类型注入
private UserDao userDao;
// 转账的操作
public void accountMoney(){
// lucy要少一百
userDao. reduceMoney();
// 手动模拟异常
// int i = 10/0;
// Mary要多一百
userDao.addMoney();
}
}
测试类:
import com.company.Spring5.Shiwu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean13Shiwu.xml");
UserService userService = context.getBean("userService",UserService.class);
userService.accountMoney();
}
}
运行结果:
当我们把手动异常添加进去之后,运行结果:
出现异常,数据库中的数据没有改变