代码改变世界

spring框架 事务 xml配置方式

2017-02-02 15:16  甘雨路  阅读(246)  评论(0编辑  收藏  举报
user=LF
password=LF
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
driverClass=oracle.jdbc.driver.OracleDriver

initialPoolSize=15
maxPoolSize=25
minPoolSize=5
<?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: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.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.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">
        
    <!-- 引入外部文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置dataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        
        <property name="initialPoolSize" value="${initialPoolSize}"></property>
        <property name="maxPoolSize" value="${maxPoolSize}"></property>
        <property name="minPoolSize" value="${minPoolSize}"></property>
    </bean>
    
    <!-- 配置jdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <bean id="amountDaoImpl" class="com.zr.trasaction.AmountDaoImpl.AmountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    
    <bean id="amountServiceImpl" class="com.zr.trasaction.AmountServiceImpl.AmountServiceImpl">
        <property name="amountDaoImpl" ref="amountDaoImpl"></property>
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置事务的属性 -->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="transferMoneyService" propagation="REQUIRED"/>
            <!-- read-only="true"只是为了数据库维护方便 -->
            <tx:method name="transferMoneyService" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面,让切面和事务关联起来 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.zr.trasaction.AmountServiceImpl.*.*(..))" id="pointcut"/>
        <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"/>
    </aop:config>

</beans>
package com.zr.trasaction.entity;



public class Amount {
    
    private String user;//用户名
    private double money;//金额
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    public Amount() {
        super();
    }
    public Amount(String user, double money) {
        super();
        this.user = user;
        this.money = money;
    }
    @Override
    public String toString() {
        return "Amount [user=" + user + ", money=" + money + "]";
    }
    
    
    

}
package com.zr.trasaction.entity;

public class BalanceException extends RuntimeException {

    private static final long serialVersionUID = 1L;


    public BalanceException() {
        super();
    }

    public BalanceException(String message) {
        super(message);
    }

    public BalanceException(String message, Throwable cause) {
        super(message, cause);
    }

    
    public BalanceException(Throwable cause) {
        super(cause);
    }

   
    protected BalanceException(String message, Throwable cause,
                               boolean enableSuppression,
                               boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
package com.zr.trasaction.AmountDao;

public interface AmountDao {
    
    /**
     * 转出钱
     * @return
     */
    public boolean decreaseMoney(String username,double money) ;
    
    /**
     * 转入钱
     * @return
     */
    public boolean increaseMoney(String username,double money);
    
}
package com.zr.trasaction.AmountDaoImpl;

import org.springframework.jdbc.core.JdbcTemplate;

import com.zr.trasaction.AmountDao.AmountDao;


public class AmountDaoImpl implements AmountDao {

    private JdbcTemplate jdbcTemplate;
    
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public boolean decreaseMoney(String username, double money) {
        
        String sql = "UPDATE TEST SET MONEY=MONEY-? WHERE USERNAME=?";
        int i = -1;
        i = jdbcTemplate.update(sql,money, username);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean increaseMoney(String username, double money) {
        String sql = "UPDATE TEST SET MONEY=MONEY+? WHERE USERNAME=?";
        int i = -1;
        i = jdbcTemplate.update(sql,money, username);
        if(i>0){
            return true;
        }
        return false;
    }

}
package com.zr.trasaction.AmountService;

import com.zr.trasaction.entity.Amount;

public interface AmountService {
    
    /**
     * 转账业务
     * @param desAmount 扣钱的账户
     * @param incAmount    增加的账户
     * @param money 转账的金额
     * @return
     */
    public boolean transferMoneyService(Amount desAmount,Amount incAmount,double money);
    
}
package com.zr.trasaction.AmountServiceImpl;

import org.springframework.jdbc.core.JdbcTemplate;

import com.zr.trasaction.AmountDaoImpl.AmountDaoImpl;
import com.zr.trasaction.AmountService.AmountService;
import com.zr.trasaction.entity.Amount;
import com.zr.trasaction.entity.BalanceException;

public class AmountServiceImpl implements AmountService {

    private AmountDaoImpl amountDaoImpl;
    private JdbcTemplate jdbcTemplate;

    public void setAmountDaoImpl(AmountDaoImpl amountDaoImpl) {
        this.amountDaoImpl = amountDaoImpl;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    @Override
    public boolean transferMoneyService(Amount desAmount, Amount incAmount,
            double money) {
        String sql = "SELECT MONEY FROM TEST WHERE USERNAME=?";
        double balance = jdbcTemplate.queryForObject(sql, Double.class, desAmount.getUser());

        boolean success1 = amountDaoImpl.decreaseMoney(desAmount.getUser(), money);
        boolean success2 = amountDaoImpl.increaseMoney(incAmount.getUser(), money);
        
        if(balance < money){
            throw new BalanceException("余额不足");
        }
        return success1 && success2;
    }

}
package com.zr.trasaction.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zr.trasaction.AmountService.AmountService;
import com.zr.trasaction.entity.Amount;

public class TestA {

    public static void main(String[] args) {
        
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

        Amount desAmount = new Amount("lf", 0);
        Amount incAmount = new Amount("tl", 0);
        AmountService impl = (AmountService)ac.getBean("amountServiceImpl");
        boolean isSuccess = impl.transferMoneyService(desAmount, incAmount, 10);
        System.out.println("==:"+isSuccess);
    }
    
    
}