• spring框架学习(八)spring管理事务方式之注解配置


    1.DAO

    AccountDao.java

    package cn.mf.dao;
    
    public interface AccountDao {
        //加钱
        void increaseMoney(Integer id,Double money);
        //减钱
        void decreaseMoney(Integer id,Double money);
    }

    AccountDaoImpl.java

    package cn.mf.dao;
    
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao  {
    
        @Override
        public void increaseMoney(Integer id, Double money) {
            getJdbcTemplate().update("update account set money = money+? where id = ? ", money,id);
        }
    
        @Override
        public void decreaseMoney(Integer id, Double money) {
            getJdbcTemplate().update("update account set money = money-? where id = ? ", money,id);
        }
    
    }

    2.SERVICE
    AccountService.java

    package cn.mf.service;
    
    public interface AccountService {
        //转账方法
        void transfer(Integer from,Integer to,Double money);
    }

    AccountServiceImpl.java

    package cn.mf.service;
    
    import org.springframework.transaction.TransactionStatus;
    import org.springframework.transaction.annotation.Isolation;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.transaction.support.TransactionCallback;
    import org.springframework.transaction.support.TransactionCallbackWithoutResult;
    import org.springframework.transaction.support.TransactionTemplate;
    
    import cn.mf.dao.AccountDao;
    
    @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
    public class AccountServiceImpl implements AccountService {
    
        private AccountDao ad ;
        private TransactionTemplate tt;
        
        @Override
        @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
        public void transfer(final Integer from,final Integer to,final Double money) {
                    //减钱
                    ad.decreaseMoney(from, money);
                    //int i = 1/0;
                    //加钱
                    ad.increaseMoney(to, money);
        }
    
        public void setAd(AccountDao ad) {
            this.ad = ad;
        }
    
        public void setTt(TransactionTemplate tt) {
            this.tt = tt;
        }
    }

    3.db.properties

    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.user=root
    jdbc.password=root

    4.applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
    
    <!-- 指定spring读取db.properties配置 -->
    <context:property-placeholder location="classpath:db.properties"  />
    
    <!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource" ></property>
    </bean>
    <!-- 事务模板对象 -->
    <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
        <property name="transactionManager" ref="transactionManager" ></property>
    </bean>
    
    <!-- 开启使用注解管理aop事务 -->
    <tx:annotation-driven/>
    
    <!-- 1.将连接池 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
        <property name="driverClass" value="${jdbc.driverClass}" ></property>
        <property name="user" value="${jdbc.user}" ></property>
        <property name="password" value="${jdbc.password}" ></property>
    </bean>
    
    
    
    <!-- 2.Dao-->
    <bean name="accountDao" class="cn.mf.dao.AccountDaoImpl" >
        <property name="dataSource" ref="dataSource" ></property>
    </bean>
    <!-- 3.Service-->
    <bean name="accountService" class="cn.mf.service.AccountServiceImpl" >
        <property name="ad" ref="accountDao" ></property>
        <property name="tt" ref="transactionTemplate" ></property>
    </bean>  
    
    </beans>

    Junit

    package cn.mf.tx;
    
    import javax.annotation.Resource;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import cn.mf.service.AccountService;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext2.xml")
    public class Demo2 {
        @Resource(name="accountService")
        private AccountService as;
        
        @Test
        public void fun1(){
            as.transfer(1, 2, 100d);
        }
    }
  • 相关阅读:
    excel的变量
    PHP安装pthreads多线程扩展教程[windows篇]
    识别字符串中的外链图片,下载存到本地,并替换图片地址
    mysql的binlog
    20165327《Java程序设计》实验一 Java开发环境的熟悉 实验报告
    20165327 2017-2018-2 《JAVA程序设计》第5周学习总结
    20165327 2017-2018-2 《Java程序设计》第4周学习总结
    20165327 第三周学习总结
    20165327 预备作业3 Linux安装及学习
    20155339 2016-2017-2 《Java程序设计》第3周学习总结
  • 原文地址:https://www.cnblogs.com/cnki/p/6740536.html
Copyright © 2020-2023  润新知