首页 > 其他分享 >Spring_2023_11_24_2 Spring整合mybatis--Spring中的事务管理(注解形式)

Spring_2023_11_24_2 Spring整合mybatis--Spring中的事务管理(注解形式)

时间:2023-11-24 12:11:22浏览次数:37  
标签:11 24 int Spring org public money import id

Spring整合mybatis--Spring中的事务管理(注解形式)

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.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
         http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.bboy.service.impl"/>
    <context:component-scan base-package="com.bboy.mapper"/>

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/qinmanage"/>
        <property name="username" value="root"/>
        <property name="password" value="127003"/>
    </bean>
    <!--事务的配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--将事务管理器交给spring容器进行管理-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.bboy.pojo"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.bboy.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

</beans>

service

AccountService

package com.bboy.service;

public interface AccountService {
    public int pay(int id_1,int id_2,int money);

}

AccountServiceImpl

package com.bboy.service.impl;

import com.bboy.mapper.AccountMapper;
import com.bboy.service.AccountService;
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;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/24 0024 11:49:08
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper ;

    public void setAccountMapper(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
    @Override
    public int pay(int id_1, int id_2, int money) {

        //-:转出 id_1
        accountMapper.updateId1(id_1, money);
        //-停电了
        //  int res = 1/0 ;
        //-:转入 id_2
        accountMapper.updateId2(id_2, money);
        return 0;
    }
}

pojo(Account)

package com.bboy.pojo;

import lombok.Data;

@Data
/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/24 0024 10:45:51
 */
public class Account {
    private int id ;
    private String name ;
    private  int money ;
}

AccountMapper

package com.bboy.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface AccountMapper {
    //- 转出
    @Update("update t_account set money=money-${money} where id=${id}")
    public int updateId1(@Param("id") int id, @Param("money") int money);
    //- 转入
    @Update("update t_account set money=money+${money} where id=${id}")
    public int updateId2(@Param("id") int id,@Param("money") int money);
}

demo

package com.bboy.demo;

import com.bboy.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = (AccountService) context.getBean("accountService");
        accountService.pay(1,2,100);
    }
}

标签:11,24,int,Spring,org,public,money,import,id
From: https://www.cnblogs.com/Kbaor/p/17853458.html

相关文章

  • RTL8211EG硬件设计要点
    RTL8211EG硬件问题解决办法问题一:RTL8211在RGMII模式下跑千兆失败的问题1、 连接网线后与PC自协商到100M。原因:RTL8211EG芯片内部自带DCDCBUCK降压电路,需要外置电感电容才能输出稳定的1.05V电压提供给芯片的内核工作。对于电感和电容,要选择手册推荐的型号,如图1.1所示。V2板由于使......
  • 11.24每日总结
    今天上课完成了大数据的测试。王S聪想要在海外开拓万D电影的市场,这次他在考虑:怎么拍商业电影才能赚钱?毕竟一些制作成本超过1亿美元的大型电影也会失败。这个问题对电影业来说比以往任何时候都更加重要。所以,他就请来了你(数据分析师)来帮他解决问题,给出一些建议,根据数据......
  • Spring Boot应用中Tomcat建议配置
    在SpringBoot框架中,我们使用最多的是Tomcat,这是SpringBoot默认的容器技术,而且是内嵌式的Tomcat。Tomcat是Apache基金下的一个轻量级的Servlet容器,支持Servlet和JSP。Tomcat服务器本身具有Web服务器的功能,可以作为独立的Web服务器来使用。一、SpringBoot应用......
  • 20211314王艺达学习笔记11
    TCP/IP和网络编程TCP/IP协议互联网的基础。TCP代表传输控制协议,IP代表互联网协议。TCP/IP的组织结构分为几个层级,通常称为TCP/IP堆栈。如图所示为TCP/IP的各个层级以及每一层级的代表性组件及其功能。进程与主机之间的传输层或其上方的数据传输只是逻辑传输。实际数据传输......
  • Spring Boot 3.2发布:大量Java 21的支持上线,改进可观测性
    就在今天凌晨,SpringBoot3.2正式发布了!该版本是在Java21正式发布之后的重要支持版本,所以在该版本中包含大量对Java21支持的优化。下面,我们分别通过Spring官方发布的博文和JoshLong长达80+分钟的介绍视频,一起认识一下SpringBoot3.2最新版本所带来的全新内容。官方博文:http......
  • 【Spring】Spring5+Spring5MVC+Mybatis+H2の環境構築(高)
    参考URL:(初)https://www.cnblogs.com/lnsylt/p/10258457.html(中)https://www.cnblogs.com/lnsylt/p/10315455.html①pom.xml1<?xmlversion="1.0"encoding="UTF-8"?>2<projectxmlns="http://maven.apache.org/POM/4.0.0"......
  • 【2023-11-23】难得知己
    20:00发生在这个世界上的事情,只有通过我们自己对事件的诠释才能影响到我们,所以只要我们能控制自己对事件的诠释,就能控制自己的世界。                                            ......
  • java实现大文件的分片上传与下载(springboot+vue3)
    1.1项目背景对于超大文件上传我们可能遇到以下问题•大文件直接上传,占用过多内存,可能导致内存溢出甚至系统崩溃•受网络环境影响,可能导致传输中断,只能重新传输•传输时间长,用户无法知道传输进度,用户体验不佳1.2项目目标对于上述问题,我们需要对文件做分片传输。分片传输就是......
  • Ubuntu 24.04 LTS “Noble Numbat” 预计于 2024 年 4 月 25 日发布
    导读Canonical今天宣布了下一个Ubuntu版本的代号,Ubuntu24.04LTS,代号“NobleNumbat”,计划于明年的4月25日发布。这个即将发布的版本预计将搭载GNOME46桌面环境。Ubuntu24.04LTS“NobleNumbat”将是Canonical的第40个Ubuntu发行版和第10个LTS(长......
  • #2023-2024-1 20231308 《计算机基础与程序设计》第九周学习总结
    2023-2024-120231308《计算机基础与程序设计》第九周学习总结作业信息这个作业属于哪个课程2023-2024-1-计算机基础与程序设计这个作业要求在哪里2023-2024-1计算机基础与程序设计第九周作业这个作业的目标计算机科学概论第10,11章《C语言程序设计》第8章并完成......