Spring 使用XML文件的方式配置事务

dao:

package tx_XML;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author chenpeng
 * @date 2018/6/4 14:33
 */

public class BookShopDaoImpl implements BookShopDao {

    private JdbcTemplate jdbcTemplate;

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

    @Override
    public Integer findBookPriceByIsbn(String isbn) {
        String str = "select price from t_book where isbn = ? ";
        return jdbcTemplate.queryForObject(str,Integer.class,isbn);
    }

    @Override
    public void updateBookStock(String isbn) {
        String SQL = "select stock from t_book_stock where isbn =?";
        int stock = jdbcTemplate.queryForObject(SQL,Integer.class,isbn);

        if(stock <= 0){
            throw new BookStockException("库存不足");
        }

        String str = "update t_book_stock set stock = (stock - 1) where isbn = ? ";
        jdbcTemplate.update(str,isbn);
    }

    @Override
    public void updateUserAccount(String username, int price) {
        //验证余额
        String SQL = "select blance from t_account where user_name =?";
        int blance = jdbcTemplate.queryForObject(SQL,Integer.class,username);

        if(blance  <= price){
            throw new UserAccountException("余额不足");
        }
        String str = "update t_account set blance = (blance - ?)  where user_name = ?";
        jdbcTemplate.update(str,price,username);
    }
}

service:

package tx_XML;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author chenpeng
 * @date 2018/6/4 15:29
 */

public class BookShopServiceImpl implements BookShopService {


    private BookShopDao bookShopDao;

    public void setBookShopDao(BookShopDao bookShopDao) {
        this.bookShopDao = bookShopDao;
    }


    @Override
    public void purchase(String username, String isbn) {
        //1、获取书的单价
        int price = bookShopDao.findBookPriceByIsbn(isbn);
        //2、更新库存
        bookShopDao.updateBookStock(isbn);
        //3、更新用户余额
        bookShopDao.updateUserAccount(username,price);
    }
}
 
 
package tx_XML;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author chenpeng
 * @date 2018/6/4 21:09
 */

public class CashierImpl implements Cashier {


    private BookShopService bookShopService;

    public void setBookShopService(BookShopService bookShopService) {
        this.bookShopService = bookShopService;
    }


    @Override
    public void checkout(String username, List<String> isbns) {
        for (String isbn:isbns) {
            bookShopService.purchase(username,isbn);
        }
    }
}

配置文件:

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
       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="db.properties"/>

    <!--扫描-->
    <context:component-scan base-package="dao_tx"/>

    <!--配置c3p0数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"/>
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${user}"/>
        <property name="password" value="${password}"/>

        <property name="initialPoolSize" value="${initPoolSize}"/>
        <property name="maxPoolSize" value="${maxPoolSize}"/>
    </bean>

    <!--配置Spring的jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>


    <!--配置bean-->
    <bean id="bookShopDao" class="tx_XML.BookShopDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <bean id="bookShopService" class="tx_XML.BookShopServiceImpl">
        <property name="bookShopDao" ref="bookShopDao"/>
    </bean>
    <bean id="cashier" class="tx_XML.CashierImpl">
        <property name="bookShopService" ref="bookShopService"/>
    </bean>

    <!--1、配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
                p:dataSource-ref="dataSource"/>

    <!--2、配置事务属性-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--根据方法名指定事务的属性-->
            <tx:method name="purchase" propagation="REQUIRES_NEW"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!--3、配置事务切入点以及把事务属性和事务切入点关联起来-->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* tx_XML.BookShopServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

</beans>

测试:

import tx_XML.BookShopDao;
import tx_XML.BookShopService;
import tx_XML.Cashier;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

/**
 * @author chenpeng
 * @date 2018/6/4 14:40
 */
public class SpringTransacitionXMLTest {

    private ApplicationContext context = null;
    private BookShopDao bookShopDao;
    private BookShopService bookShopService;
    private Cashier cashier;
    {
        context = new ClassPathXmlApplicationContext("applicationContextXML.xml");
        bookShopDao = context.getBean(BookShopDao.class);
        bookShopService = context.getBean(BookShopService.class);
        cashier = context.getBean(Cashier.class);
    }

    //测试事务的传播行为
    @Test
    public void testTransactionPropagation(){
        cashier.checkout("AA", Arrays.asList("1001","1002"));
    }



    @Test
    public void testFindBookPriceByIsbn(){
        System.out.println(bookShopDao.findBookPriceByIsbn("1001"));
    }
    @Test
    public void testUpdateBookStock(){
        bookShopDao.updateBookStock("1001");
    }

    @Test
    public void testUpdateUserAccount(){
        bookShopDao.updateUserAccount("AA",100);
    }
}


猜你喜欢

转载自blog.csdn.net/qq_30604989/article/details/80574754