首页 > 编程语言 >Spring编程式事务控制

Spring编程式事务控制

时间:2023-12-22 23:44:20浏览次数:44  
标签:事务 Spring 编程 Account springframework import zjw org public

目录

Spring编程式事务控制

实际中很少使用

代码实现

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zjw</groupId>
    <artifactId>day04_eesy_08account_tx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <spring.version>6.1.1</spring.version>
        <mysql.version>8.0.33</mysql.version>
        <junit.version>4.13.2</junit.version>
        <lombok.version>1.18.30</lombok.version>
        <aspectjweaver.version>1.9.21</aspectjweaver.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectjweaver.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--配置业务层-->
    <bean id="accountService" class="com.zjw.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="transactionTemplate" ref="transactionTemplate"/>
    </bean>
    <!--配置账户的持久层-->
    <bean id="accountDao" class="com.zjw.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/eesy_spring?useSSL=false&amp;serverTimeZone=Asia/Shanghai"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

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

    <!--配置事务模板对象-->
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>
</beans>

Service层

package com.zjw.service.impl;

import com.zjw.dao.IAccountDao;
import com.zjw.domain.Account;
import com.zjw.service.IAccountService;
import lombok.Setter;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * 账户的业务层实现类
 * <p>
 * 事务的控制应该都在业务层
 * @author zjw
 */
public class AccountServiceImpl implements IAccountService {

    @Setter
    private IAccountDao accountDao;

    @Setter
    private TransactionTemplate transactionTemplate ;

    @Override
    public Account findAccountById(Integer accountId) {
        return transactionTemplate.execute(new TransactionCallback<Account>() {
            @Override
            public Account doInTransaction(TransactionStatus status) {
                return accountDao.findAccountById(accountId);
            }
        });

    }

    /**
     * 声明式事务控制,代码太不优雅
     */
    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                //2、执行操作
                //2.1、根据名称查询转出账户
                Account source = accountDao.findAccountByName(sourceName);
                //2.2、根据名称查询转入账户
                Account target = accountDao.findAccountByName(targetName);
                //2.3、转出账户减钱
                source.setMoney(source.getMoney() - money);
                //2.4、转入账户加钱
                target.setMoney(target.getMoney() + money);
                //2.5、更新转出账户
                accountDao.updateAccount(source);

                int i = 1 / 0;

                //2.6、更新转入账户
                accountDao.updateAccount(target);
                return null;
            }
        });


    }
}

Dao层

package com.zjw.dao.impl;

import com.zjw.dao.IAccountDao;
import com.zjw.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

/**
 * @author zjw
 */
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {


    @Override
    public Account findAccountById(Integer accountId) {
        List<Account> accounts = getJdbcTemplate().query("SELECT * FROM account WHERE id=?", new BeanPropertyRowMapper<Account>(Account.class),accountId);
        return accounts.isEmpty()?null:accounts.get(0);
    }

    @Override
    public Account findAccountByName(String accountName) {
        List<Account> accounts = getJdbcTemplate().query("SELECT * FROM account WHERE name=?", new BeanPropertyRowMapper<Account>(Account.class),accountName);
        if (accounts.isEmpty()){
            return null;
        }
        if (accounts.size()>1){
            throw new RuntimeException("结果集不唯一");
        }
        return accounts.get(0);
    }

    @Override
    public void updateAccount(Account account) {
        getJdbcTemplate().update("UPDATE account SET name=?,money=? WHERE id=?",account.getName(),account.getMoney(),account.getId());
    }
}

测试

package com.zjw.test;

import com.zjw.domain.Account;
import com.zjw.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 * 使用Junit单元测试:测试我们的配置
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {

    @Autowired
    private IAccountService as;

    @Test
    public void testTransfer() {
        as.transfer("aaa", "bbb", 100f);
    }
}

标签:事务,Spring,编程,Account,springframework,import,zjw,org,public
From: https://www.cnblogs.com/zjw-blog/p/17922557.html

相关文章

  • Spring基于XML的事务管理器DataSourceTransactionManager
    Spring基于XML的事务管理器DataSourceTransactionManager源码代码测试pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan......
  • Springboot下PageHelper分页不生效问题
    今天在做一个小项目,引入PageHelper时踩了一个坑,记录一下。解决方案参考:SpringBoot+MyBatis使用pagehelper分页插件及其注意事项(含解决分页不生效问题)环境:SpringBoot3.2.0JDK17Postgresql15PageHelper1.2.12依赖<dependency><groupId>com.github.pagehelper</......
  • springboot_3.2_freemark_基础环境配置
    springboot_3.2_freemark_基础环境配置一、前言二、环境三、相关资料四、目标五、默认配置项六、构建springboot3.2项目6.1pom.xml内容:6.2启动类6.3添加ftlh模板6.4controller内容6.5bootstrap.yml配置七、总结一、前言FreeMarker是一款模板引擎:即一种基于模板和要改变的......
  • springboot1.x升级到springboot3.x中遇到的问题总结
    springboot1.x升级到springboot3.x中遇到的问题总结springboot1.x升级到springboot3.x中遇到的问题总结前言问题:无法创建DataSource的bean对象,提示url或driverclass未配置问题:引入freemark后页面总是报404问题:bootstrap.yml不生效,配置中的内容无法读取springboot1.x升级到spring......
  • Spring源码深度解析_源码构建
    源码构建1.下载下载Spring源码,这里以5.0.2.RELEASE为例,下载地址<https://github.com/spring-projects/spring-framework/archive/v5.0.2.RELEASE.zip>Spring源码中有很多module,在这里做出简单说明:spring-jcl:spring封装的日志框架。spring-core:spring核心依赖,包含Spring框......
  • golang快速入门:并发编程(一)
    进程、线程和协程进程:是操作系统中的一个执行实体,它拥有独立的内存空间和系统资源。每个进程都是独立运行的,它们之间相互隔离,通过进程间通信(IPC)来进行数据交换。每个进程都有自己的地址空间、堆栈和文件描述符等。进程之间的切换开销较大,因为需要保存和恢复整个进程的状态。线程:是......
  • Spring Framework Reference 5.3.27 - Core Technologies
    目录Chapter1.TheIoCContainerSpringIoC容器和bean介绍Chapter2.ResourcesChapter3.Validation,DataBinding,andTypeConversionChapter4.SpringExpressionLanguage(SpEL)Chapter5.AspectOrientedProgrammingwithSpringChapter6.SpringAOPAPIsChapter......
  • Spring Boot Reference 2.7.11
    目录UsingSpringBootBuildSystems代码风格配置类自动配置SpringBeansandDependencyInjection使用@SpringBootApplication注解RunningYourApplication开发者工具CoreFeatureCreatingYourOwnAuto-configuration加载自动配置候选项条件注解测试自动配置类创建自己的star......
  • spring中BeanPostProcessor介绍
    介绍BeanPostProcessor是Spring框架提供的一个接口,用于在Spring容器中对Bean进行后处理。工作原理在Spring容器启动时,会扫描所有BeanPostProcessor实现类。当Spring容器创建Bean时,会调用BeanPostProcessor的postProcessBeforeInitialization()方法。当S......
  • Spring基于注解的AOP事务控制
    Spring基于注解的AOP事务控制源码代码测试pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schem......