当前位置: 首页 > news >正文

javaee spring 声明式事务管理方式2 注解方式

spring配置文件

<?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"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"><!-- 1.开启 注解 --><context:component-scan base-package="com.test" /><!-- 2.创建数据源对象--><context:property-placeholder location="db.properties" /><bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${driverClass}" /><property name="jdbcUrl" value="${url}" /><property name="user" value="${user}" /><property name="password" value="${password}" /></bean><!-- 3.创建JdbcTemplate对象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg name="dataSource" ref="comboPooledDataSource" /></bean><!-- 4 创建一个事务管理器 --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="comboPooledDataSource" /></bean><!-- 5.开启事务注解--><tx:annotation-driven transaction-manager="txManager" /></beans>

方法

package com.test.service.impl;import com.test.dao.ICardInfoDao;
import com.test.exception.MyException;
import com.test.service.ICardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;@Service
public class CardInfoService implements ICardInfoService {@Autowiredprivate ICardInfoDao cardInfoDao;public ICardInfoDao getCardInfoDao() {return cardInfoDao;}public void setCardInfoDao(ICardInfoDao cardInfoDao) {this.cardInfoDao = cardInfoDao;}//实现转账方法@Override//通过注解的方式 声明事务方法 (隔离级别  传播行为  回滚的条件)@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor =MyException.class )public void transfer(int from, int to, float money) throws Exception {//一方减钱cardInfoDao.decreaseMoney(from,money);if(true)throw  new MyException("转账异常");//一方加钱cardInfoDao.increaseMoney(to,money);}
}
http://www.lryc.cn/news/162614.html

相关文章:

  • 基于SpringBoot+微信小程序的智慧医疗线上预约问诊小程序
  • 注意力机制讲解与代码解析
  • 微调 TrOCR – 训练 TrOCR 识别弯曲文本
  • Jetsonnano B01 笔记7:Mediapipe与人脸手势识别
  • vue学习之v-if/v-else/v-else-if
  • ansible的安装和简单的块使用
  • Android 状态栏显示运营商名称
  • 10.Xaml ListBox控件
  • 基于vue3和element-plus的省市区级联组件
  • Paper: 利用RNN来提取恶意软件家族的API调用模式
  • sdkman 安装以及 graalvm安装
  • 如何正确使用 WEB 接口的 HTTP 状态码和业务状态码?
  • Spark【Spark SQL(三)DataSet】
  • 制作立体图像实用软件:3DMasterKit 10.7 Crack
  • 高校 Web 站点网络安全面临的主要的威胁
  • vue前端解决跨域
  • 【Cicadaplayer】解码线程及队列实现
  • 把文件上传到Gitee的详细步骤
  • 基于keras中Lenet对于mnist的处理
  • Python爬虫 教程:IP池的使用
  • Ansible之playbook剧本
  • unique_ptr的大小探讨
  • 人工智能TensorFlow PyTorch物体分类和目标检测合集【持续更新】
  • ElementPlus·面包屑导航实现
  • 【项目管理】PM vs PMO 18点区别
  • 13 Python使用Json
  • PDFBOX和ASPOSE.PDF
  • 第51节:cesium 范围查询(含源码+视频)
  • YOLOv5改进算法之添加CA注意力机制模块
  • Jmeter系列-阶梯加压线程组Stepping Thread Group详解(6)