0
点赞
收藏
分享

微信扫一扫

Spring用XML方式使用事务

西红柿上校 2022-12-06 阅读 121

项目开始前需要新建数据库(数据库使用mysql8.0以上的版本)

#创建数据库
CREATE database userdb;

#创建数据表
CREATE TABLE userdb.t_account (
id int NOT NULL,
username varchar(50) NULL DEFAULT NULL,
money decimal(16,3) NULL DEFAULT NULL,
PRIMARY KEY(id)
);

#添加测试数据
INSERT into userdb.t_account(id,username,money)VALUES(1,'lucy',1000);
INSERT into userdb.t_account(id,username,money)VALUES(2,'mary',1000);

项目需要用的的依赖

Spring用XML方式使用事务_bc

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.2.jar
druid-1.2.14.jar
mysql-connector-java-8.0.19.jar
spring-aop-5.2.6.RELEASE.jar
spring-aspects-5.2.6.RELEASE.jar
spring-beans-5.2.6.RELEASE.jar
spring-context-5.2.6.RELEASE.jar
spring-core-5.2.6.RELEASE.jar
spring-expression-5.2.6.RELEASE.jar
spring-jdbc-5.2.6.RELEASE.jar
spring-tx-5.2.6.RELEASE.jar

项目的结构

Spring用XML方式使用事务_jar_02

数据库配置文件jdbc.properties

shiwujdbc.driverClassName=com.mysql.cj.jdbc.Driver
shiwujdbc.url=jdbc:mysql://localhost:3306/userdb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
shiwujdbc.username=root
shiwujdbc.password=sa1234

XML配置文件 beans.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.mt.spring.shiwu"></context:component-scan>

<!-- 加载数据库配置文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>

<!--数据库连接池配置-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${shiwujdbc.driverClassName}"></property>
<property name="url" value="${shiwujdbc.url}"></property>
<property name="username" value="${shiwujdbc.username}"></property>
<property name="password" value="${shiwujdbc.password}"></property>
</bean>

<!-- 创建JdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>


<!-- 创建事务管理类-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!--配置通知-->
<tx:advice id="txadvice" transaction-manager="dataSourceTransactionManager">
<!--配置事务参数-->
<tx:attributes>
<!--指定那种规则的方法上面添加事务-->
<tx:method name="accountMoney" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<!--配置切入点和切面-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pt" expression="execution(* com.mt.spring.shiwu.service.UserService.*(..))"/>
<!--配置切面-->
<aop:advisor advice-ref="txadvice" pointcut-ref="pt"/>
</aop:config>
</beans>

数据操作接口UserDao

package com.mt.spring.shiwu.dao;

public interface UserDao {
public void addMoney();//多钱
public void reduceMoney();//少钱
}

数据操作实现类UserDaoImpl

package com.mt.spring.shiwu.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao{
//注入数据库连接池
@Autowired
private JdbcTemplate jdbcTemplate;

//少钱 lucy转账给mary lucy先减少100
@Override
public void reduceMoney() {
String sql = "update t_account set money=money-? where username=?";
jdbcTemplate.update(sql,100,"lucy");
}

//多钱钱 lucy转账给mary mary加100
@Override
public void addMoney() {
String sql = "update t_account set money=money+? where username=?";
jdbcTemplate.update(sql,100,"mary");
}

}

业务层UserService

package com.mt.spring.shiwu.service;

import com.mt.spring.shiwu.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
//注入Dao
@Autowired
private UserDao userDao;
public void accountMoney(){
//lucy少100
userDao.reduceMoney();
//模拟异常
int i = 10/0; //出现异常之后已经操作的会回滚
//mary加100
userDao.addMoney();

}
}

测试类

package com.mt.spring;

import com.mt.spring.shiwu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.accountMoney();
}

}


举报

相关推荐

0 条评论