zoukankan      html  css  js  c++  java
  • Spring 框架下 事务的配置(复杂)

    //db.properties配置  src下的文件

    jdbc.jdbcUrl=jdbc:mysql:///day43
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.user=root
    jdbc.password=root

    //applicationContext.xml的配置

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

    <!--指定读取配置文件 -->
       <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="transactionTemplat" class="org.springframework.transaction.support.TransactionTemplate">
          <property name="transactionManager" ref="transactionManager"></property>
     </bean>
     <!-- 配置事物通知  -->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
     </tx:advice>
     <!-- 配置织入  -->
     <aop:config>
     <!-- 切点表达式 -->
         <aop:pointcut expression="execution(* cn.jy.service.*ServiceImp.*(..))" id="txPc"/>
     <!-- 配通知给 切点-->
     <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
     </aop:config>
       <!--1 将连接池放进spring容器 -->
       <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 将jdbc模板放进连接池 -->
       <!-- <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
             <property name="dataSource" ref="dataSource"></property>
       </bean> -->
       <!-- 3将AccountDaoImp放进连接池 -->
       <bean name="accountDaoImp" class="cn.jy.dao.AccountDaoImp">
               <property name="dataSource" ref="dataSource"></property>
       </bean>
       <!-- 4将userServiceImp放进连接池 -->
       <bean name="accountService" class="cn.jy.service.AccountServiceImp">
               <property name="ad" ref="accountDaoImp"></property>
       </bean>
     </beans>

    //AccountDaoImp层

    package cn.jy.dao;

    import org.springframework.jdbc.core.support.JdbcDaoSupport;

    public class AccountDaoImp extends JdbcDaoSupport  implements AccountDao {

        @Override
        public void addMoney(Integer id, Double money) {
            getJdbcTemplate().update("update account1 set money=money+? where id=?",money,id);
            

        }

        @Override
        public void decreaseMoney(Integer id, Double money) {
               getJdbcTemplate().update("update account1 set money=money-? where id=?",money,id);

        }

    }
    //AccountService层

    package cn.jy.service;

    import cn.jy.dao.AccountDao;

    public class AccountServiceImp implements AccountService {
    private AccountDao ad;

    public void setAd(AccountDao ad) {
        this.ad = ad;
    }

        @Override
        public void transfer(Integer from, Integer to, Double money) {
            ad.decreaseMoney(to, money);
            
            //int i=0/0;
            ad.addMoney(from, money);
            

        }

    }

    //测试

    package cn.jy.service;

    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;

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class Demo {
     @Resource(name="accountService")
     private AccountService aa;
     @Test
     public void fun1(){
         aa.transfer(1, 2,100d);
     }
     
    }

  • 相关阅读:
    HDU 3681 Prison Break 越狱(状压DP,变形)
    POJ 2411 Mondriaan's Dream (状压DP,骨牌覆盖,经典)
    ZOJ 3471 Most Powerful (状压DP,经典)
    POJ 2288 Islands and Bridges (状压DP,变形)
    HDU 3001 Travelling (状压DP,3进制)
    POJ 3311 Hie with the Pie (状压DP)
    POJ 1185 炮兵阵地 (状压DP,轮廓线DP)
    FZU 2204 7
    POJ 3254 Corn Fields (状压DP,轮廓线DP)
    ZOJ 3494 BCD Code (数位DP,AC自动机)
  • 原文地址:https://www.cnblogs.com/Fisherman13/p/10581007.html
Copyright © 2011-2022 走看看