首页 > 其他分享 >spring中配置事务管理器

spring中配置事务管理器

时间:2023-09-14 15:35:41浏览次数:38  
标签:事务 管理器 spring bookDao hh springframework Integer import

2023-09-14

spring-jdbc.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"
       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">

    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <bean class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <context:component-scan base-package="com.hh.spring"></context:component-scan>

    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--开启事务的注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

BookServiceImpl

package com.hh.spring.service.impl;

import com.hh.spring.dao.BookDao;
import com.hh.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author hh
 * @version 1.0
 * @DATE 2023-09-14 14:42:10
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;
    @Override
    @Transactional
    public void buyBook(Integer userId, Integer bookId) {
        //查询图书的价格
        Integer price = bookDao.getPriceByBookId(bookId);
        //更新图书的库存
        bookDao.updateStore(bookId);
        //更新用户的余额
        bookDao.updateBalance(userId,price);
    }
}

 

标签:事务,管理器,spring,bookDao,hh,springframework,Integer,import
From: https://www.cnblogs.com/isDaHua/p/17702613.html

相关文章

  • MySQL Node.js mysql 事务
    MySQLNode.jsmysql事务在MySQL数据库中,事务(transaction)是一组原子性操作,要么全部执行成功,要么全部回滚回去。在多用户并发环境中,事务可以保护数据的一致性和完整性。MySQL提供了ACID属性,并且支持事务。Node.js中,可以通过使用mysql模块的连接来实现事务。什么是事务在MySQL数......
  • SpringBoot中@ConfigurationProperties和@PropertySource的区别
    @ConfigurationProperties用于将主配置文件(application.properties或者“application.yml”)中的属性,映射到实体类中对应的属性。意思就是把主配置文件中配置属性设置到对应的Bean属性上。只需要写明prefix,如下,prefix是sever,就会自动将application文件中以sever开头的配置一一注......
  • springboot智能3D人体导医系统源码
    智能3D人体导医系统源码医院智能导诊系统是在医疗中使用的引导患者自助就诊挂号,在就诊的过程中有许多患者不知道需要挂什么号,要看什么病,通过智能导诊系统,可输入自身疾病的症状表现,或选择身体部位,在经由智慧导诊系统多维度计算,精准推荐科室,引导患者挂号就诊,实现科学就诊,不再担心挂......
  • mysql事务回滚
    前几天发现程序有个Bug:使用JPA已经设置了回滚,但抛出异常后,提交的事务并没有回滚。刚开始以为是JPA使用问题,debug了近2个多小时竟然找不到原因。后来上网查了一下,才发现不是程序问题(坑爹啊,看来自己对mysql还是不熟),是数据库表问题(JPA自动建表)。原因如下:mysql建表时如果指定ENG......
  • CommandLineRunner - Spring Boot应用程序启动后执行
    在springboot启动的时候,有的时候需要做一些初始化或者预加载的事情。springboot给我们提供了这样一个接口CommandLineRunnerCommandLineRunner是一个接口,用于在SpringBoot应用程序启动后执行一些特定的任务或代码块。当应用程序启动完成后,SpringBoot会查找并执行实现了Comma......
  • SpringBoot单体用户登录校验
    一、概述要做一个有私有空间的单体的SpringBoot项目,用户的权限校验是必须得。需要指定哪些接口需要权限才能访问,哪些接口不需要权限就能访问。目标:1.用户登录、注册不需要权限校验,获取用户信息需要权限校验2.获取用户信息通过token来获取(从token中取......
  • 【Spring Bean的生命周期】
    SpringBean的生命周期SpringBean的生命周期分为四个阶段:实例化、属性赋值、初始化和销毁。实例化构造器实例化通过Java类的构造函数实例化Bean,利用Java反射机制,调用bean对应类的构造方法进行实例化。在XML文件中,可以使用标签的class属性指定要实例化的Bean类。当容器启动时,......
  • 关于 windows 操作系统任务管理器里的 mcshield.exe
    mcshield.exe是McAfee防病毒软件的一个重要组成部分,用于实时扫描和监控计算机上的文件和进程,以确保系统的安全性。在本文中,我将详细介绍mcshield.exe的功能、工作原理以及如何管理它。mcshield.exe概述mcshield.exe是McAfee防病毒软件中的一个主要执行文件,通常位于C:......
  • 【Spring boot】数据库依赖
    如果依赖库不存在,maven安装依赖库JDBC的依赖项:spring-jdbc<dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.14</version></dependency>  MySQL连接的依赖项:mysql......
  • Node.js vs. Spring Boot:Hello World 性能对决,谁更快一点?
    前言:SpringBoot在Java生态中备受欢迎,它是一款基于Java构建的轻量级服务端框架,主要用于Web服务。SpringBoot的应用使得创建各类基于Spring的企业级应用变得异常简单。Node.js作为一种基于ChromeV8引擎的JavaScript运行时环境,在服务端上运行JavaScript代码。它以其独......