zoukankan      html  css  js  c++  java
  • spring基于注解的声明式事务控制

    一、spring的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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx
            https://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <context:component-scan base-package="com.wuxi"></context:component-scan>
    
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
            <property name="url"
                      value="jdbc:mysql://192.168.2.105:3306/ssm?characterEncoding=utf8&amp;useSSL=false"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
        </bean>
    
    <!--
    spring中基于注解的声明式事务控制配置步骤
        1、配置事务管理器
        2、开启spring对注解事务的支持
        3、在需要事务支持的地方使用@Transactional注解
    -->
        <!--事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!--开启spring对注解事务的支持-->
        <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    
    </beans>

    二、dao实现类

    package com.wuxi.daos.impl;
    
    import com.wuxi.beans.Account;
    import com.wuxi.daos.AccountDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Repository
    public class AccountDaoImpl implements AccountDao {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @Override
        public Account findAccountById(Integer accountId) {
            List<Account> accounts = jdbcTemplate.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 = jdbcTemplate.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) {
            jdbcTemplate.update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId());
        }
    }

    三、service实现类

    package com.wuxi.services.impl;
    
    import com.wuxi.beans.Account;
    import com.wuxi.daos.AccountDao;
    import com.wuxi.services.AccountService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    @Service
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)//
    public class AccountServiceImpl implements AccountService {
        @Autowired
        private AccountDao accountDao;
    
        @Override
        public Account findAccounById(Integer accountId) {
            return accountDao.findAccountById(accountId);
        }
    
        @Override
        @Transactional(propagation = Propagation.REQUIRED, readOnly = false)//增删改
        public void transfer(String sourceName, String targetName, Float money) {
            Account source = accountDao.findAccountByName(sourceName);
            Account target = accountDao.findAccountByName(targetName);
    
            source.setMoney(source.getMoney() - money);
            target.setMoney(target.getMoney() + money);
    
            accountDao.updateAccount(source);
            int i = 1 / 0;
            accountDao.updateAccount(target);
        }
    }
  • 相关阅读:
    技术笔记3
    技术笔记2 jetty jboss
    技术笔记1前台
    日常笔记4
    日常笔记3
    日常笔记2
    日常笔记
    C语言——结构体
    用Java原子变量的CAS方法实现一个自旋锁
    Java中处理Linux信号量
  • 原文地址:https://www.cnblogs.com/linding/p/13670072.html
Copyright © 2011-2022 走看看