IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Spring >基于xml的spring声明式事物控制 -> 正文阅读

[Java知识库]Spring >基于xml的spring声明式事物控制

1 > POM文件需要的依赖

<?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">
    <parent>
        <artifactId>spring_dome</artifactId>
        <groupId>com.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring_transcation</artifactId>

    <packaging>jar</packaging>
    <dependencies>
        <!-- IOC容器 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!-- aop切面 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!-- spring事务 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.3.9</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
       	<!-- spring jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!-- aop切面 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!-- spring test -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- mysql 驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>
    </dependencies>

</project>

2 > 编写需要用到的类

在这里插入图片描述

2.1 Dao层接口及其实现类

2.1.1 > AccountDao 接口

package com.tran.dao;

import com.tran.domain.Account;

import java.util.List;

public interface AccountDao {
    // 查询所有
    List<Account> findAllAccount();

    // 保存账户
    Integer saveAccount(Account account);

    // 更新账户
    Integer updateAccount(Account account);

    // 转账
    Integer updateAccountToAccount(Account source , Account target,Float transferMoney);
}

2.1.2 > AccountDaoImpl 接口实现类,此实现类只负责逻辑代码的实现,事物控制由spring实现

package com.tran.dao.impl;

import com.tran.dao.AccountDao;
import com.tran.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.util.List;

public class AccountDaoImpl implements AccountDao {

    // 设置公用的数据源
    private DriverManagerDataSource dataSource;
    private JdbcTemplate jdbcTemplate;

    // 设置set方法对该对象进行赋值
    public void setDataSource(DriverManagerDataSource dataSource) {
        this.dataSource = dataSource;
    }

    // 设置set方法对该对象进行赋值
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    @Override // 查询所有用户
    public List<Account> findAllAccount() {
        return jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
    }

    @Override // 不好意思  保存账户没有用到
    public Integer saveAccount(Account account) {
        return null;
    }

    @Override
    public Integer updateAccount(Account account) {
        /*
        * 一次判断account对象中那些属性存在 哪些属性不存在
        * */
        StringBuilder sb = new StringBuilder("update account set ");
        // 判断 account 对象是否为空
        if (account == null || account.getId() == null) {
            // 对象为空 抛出异常 触发事务,进行回滚操作
            System.out.println("Id is null ~");
            throw new RuntimeException("Execute Fail ~~ : by ID Field is null ~~");
        } else {
            // account不为空
            if (account.getName() == null) {
                // name is null ---- name值为空,输出提示
                System.out.println("Name is null ~");

                // 并且判断money是否为空
                if (account.getMoney() != null) {
                    // money value not null  ----  不为空,进行sql语句的拼接
                    System.out.println("Money is : " + account.getMoney());
                    sb.append("money = " + account.getMoney() + " ");
                } else {
                    // money value is null --- 当name,金额都为空时,抛出异常,触发回滚操作
                    System.out.println("Money is null ~");
                    throw new RuntimeException("Need's Values is null ~ \ncannot update ~ \nBy id is : " + account.getId());
                }
            } else {
                // name is not null 名字不为空 ,拼接sql
                System.out.println("Name is : " + account.getName());
                sb.append("name = '" + account.getName() + "' ");
                if (account.getMoney() != null) {
                    // money value not null 金额不为空,拼接字符串
                    System.out.println("Money is : " + account.getMoney());
                    sb.append(", money = " + account.getMoney() + " ");
                } else {
                    // money value is null
                    System.out.println("Money is null ~");
                }

            }
            // 拼接为哪个id更新
            sb.append("where id = " + account.getId());
            String sql_update = sb.toString();
            System.out.println("SQL is : " + sql_update);
            int i = 0;
            i = jdbcTemplate.update(sql_update); // 返回更新的行数
            return i;
        }
    }

    @Override // 转账方法
    public Integer updateAccountToAccount(Account source, Account target, Float transferMoney) {

        // 设置影响的行数
        Integer update_line = 0;

        // 获取当前source账户的金额 这里可以写一个findAccountById的方法,然后进行调用(我忘了写了)
        String sql_findById = "select * from account where id = ?";
        Account account_source = jdbcTemplate.queryForObject(sql_findById, new BeanPropertyRowMapper<Account>(Account.class), source.getId());
        Account account_target = jdbcTemplate.queryForObject(sql_findById, new BeanPropertyRowMapper<Account>(Account.class), target.getId());

        // 判断获取到的数据是否为空
        if (account_source != null && account_target != null) {
            // 判断转账金额是否大于0
            if (transferMoney > 0.0f) {
                // 判断要转账用户内的金额是否大于 转账金额
                if (account_source.getMoney() > transferMoney) {
                	// 显示提示信息 source -> target : money( money )
                    System.out.println(account_source.getName() + " --> " + 
                    account_target.getName() + " : Money( "+ transferMoney +" )");
                    account_source.setMoney(account_source.getMoney() - transferMoney);
                    account_target.setMoney(account_target.getMoney() + transferMoney);
                    update_line += this.updateAccount(account_source);
                    update_line += this.updateAccount(account_target);
                } else {
                    throw new RuntimeException("余额不足!! 请重新输入!!");
                }
            } else {
                throw new RuntimeException("您输入的金额为负数!! 请重新输入!!");
            }
        } else {
            throw new RuntimeException("获取到的值是空的!!  请检查您输入的账户是否存在 !!");
        }

        System.out.println("Update line is : " + update_line);

        // 因为转账影响为两个账户,所以进行最后一次判断,判断转账是否出现异常
        if (update_line == 2) {
            // 影响账户数为2时,则证明转账成功 反之则失败
            System.out.println("Update success !!!");
        } else {
            System.out.println("Update fail !!");
            throw new RuntimeException();
        }

        return update_line;
    }
}

2.2 Service 层接口及其实现类

2.2.1 AccountService 接口

package com.tran.service;

import com.tran.domain.Account;

import java.util.List;

public interface AccountService {
    List<Account> findAllAccount();
    Integer transferAccount(Account source,Account target,Float money);
}

2.2.2 AccountServiceImpl 实现类

package com.tran.service.impl;

import com.tran.dao.AccountDao;
import com.tran.domain.Account;
import com.tran.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    @Override
    public Integer transferAccount(Account source, Account target, Float money) {
        return accountDao.updateAccountToAccount(source,target,money);
    }
}

3 > bean.xml (springIOC容器配置,配置需要加载进IOC容器的类)

同时配置需要加载的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:aop="http://www.springframework.org/schema/aop"
       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/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">

    <!-- 设置DataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://159.75.19.211:3306/dome02"/>
        <property name="username" value="root"/>
        <property name="password" value="111222333"/>
    </bean>

    <!-- JdbcTemplate设置数据源 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource"/>
    </bean>

    <!-- 设置Dao-->
    <bean id="accountDao" class="com.tran.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

    <!-- 配置Service-->
    <bean id="accountService" class="com.tran.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>



    <!-- 生成事务管理器 -->
    <bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<!-- 这快需要池,就很好理解了,事务肯定要对某一个或者多个链接产生一个约束,因此需要一个被管理的链接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 设置事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManger">
        <tx:attributes>
            <!--
            isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别。
            propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。
            read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写。
            timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
            rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
            no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚。没有默认值。表示任何异常都回滚。
            -->
            <!-- find*表示AccountDaoimpl下的所有方法 -->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="update*" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置Aop切面 -->
    <aop:config>
        <!-- 配置切入点 切入点为AccountDaoImpl中的一个方法 -->
        <aop:pointcut id="pointcut01" expression="execution(* com.tran.dao.impl.AccountDaoImpl.*(..))"/>
        <!-- 配置事务管理通知,txAdvice表示事务通知,与上面设置的事物通知相对应 -->
        <aop:advisor pointcut-ref="pointcut01" advice-ref="txAdvice"/>
    </aop:config>
</beans>

4 > 编写测试类

4.1 > Test 测试类(背景换成小姐姐,心情都变得愉悦了)

4.2 > Account表

在这里插入图片描述

4.2 > 测试账户1- 转给-账户2 100(无异常发生,模拟正常情况)

在这里插入图片描述

4.3 > 测试账户1- 转给-账户2 100(异常发生,模拟发生错误情况,此时需要回滚)

在这里插入图片描述
aaa转账给bbb ,100由于发生未知错误,发生回滚,账户内数额不变
在这里插入图片描述

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-08-18 12:34:18  更:2021-08-18 12:36:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 1:34:42-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码