Spring配置事务

来源:互联网 发布:淘宝无线端分类链接 编辑:程序博客网 时间:2024/06/16 14:03
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd"><!--00.识别jdbc.properties文件--><context:property-placeholder location="jdbc.properties"></context:property-placeholder><!--01.建立数据源  ${} Spring 内置的一个数据源 DriverManager--><!--<bean id="dataSoure" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="driverClassName" value="${driver}"></property>    <property name="url" value="${url}"></property>    <property name="username" value="${user}"></property>    <property name="password" value="${password}"></property></bean>--><!-- c3p0 <bean id="dataSoure" class="com.mchange.v2.c3p0.ComboPooledDataSource">     <property name="driverClass" value="${driver}"></property>     <property name="jdbcUrl" value="${url}"></property>     <property name="user" value="${user}"></property>     <property name="password" value="${password}"></property> </bean>--><!--dpcb--><!--<bean id="dataSoure" class="org.apache.commons.dbcp.BasicDataSource">    <property name="driverClassName" value="${driver}"></property>    <property name="url" value="${url}"></property>    <property name="username" value="${user}"></property>    <property name="password" value="${password}"></property></bean>--><!--alibaba --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">    <property name="driverClassName" value="${driver}"></property>    <property name="url" value="${url}"></property>    <property name="username" value="${user}"></property>    <property name="password" value="${password}"></property></bean><!-- <!–02.jdbcTemplate 配置–><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">    <property name="dataSource" ref="dataSoure"></property></bean>--><!--03.dao配置--><bean id="account" class="cn.springJDBC_tx.cn.cn.dao.ShowAccont">    <property name="dataSource" ref="dataSource"></property></bean><bean id="stock" class="cn.springJDBC_tx.cn.cn.dao.ShowStock">    <property name="dataSource" ref="dataSource"></property></bean><!--04.cn.cn.service   bookService--><bean id="show" class="cn.springJDBC_tx.cn.cn.service.ShowServiceAccount">    <property name="accDao" ref="account"></property>    <property name="stockDao" ref="stock"></property></bean><!--事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <property name="dataSource" ref="dataSource"></property></bean><!--配置事务 拦截方法--><!--<bean id="serviceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">    <!–目标类型–>    <property name="target" ref="show"></property>    <property name="transactionManager" ref="transactionManager"></property>    <!–增强–>    <property name="transactionAttributes">        <props>            <prop key="buy*">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-ShockException            </prop>        </props>    </property></bean>--><!--用注解 配置事务--><!-- <tx:annotation-driven transaction-manager="transactionManager"/>--><!--第三种  用Aspect --><tx:advice id="txAdvice" transaction-manager="transactionManager">    <tx:attributes>        <tx:method name="buy*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="ShockException"/>    </tx:attributes></tx:advice><aop:config>    <!--配置切点-->    <aop:pointcut id="mypoint" expression="execution(* *..cn.cn.service.*.*(..))"></aop:pointcut>    <!--顾问-->    <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor></aop:config></beans>


public interface IAccount {    public  boolean updateAccount(int id,double money,boolean isBuy);}

public interface IStock {    public  boolean updateStock(int id,int money,boolean isBuy);}

public class ShowAccont extends JdbcDaoSupport implements IAccount {    public boolean updateAccount(int id, double money, boolean isBuy) {        boolean flag=false;        String sql=null;        if (isBuy){            sql="update account set balace=balace-? where aid=?";        }else {            sql="update account set balace=balace+? where aid=?";        }        int count = this.getJdbcTemplate().update(sql, money, id);        if(count>0){            flag=true;        }        return flag;    }}

public class ShowStock extends JdbcDaoSupport implements  IStock {    public boolean updateStock(int id, int money, boolean isBuy) {        boolean flag=false;        String sql=null;        if (isBuy){            sql="update stock set count=count+? where sid=?";        }else {            sql="update stock set count=count-? where sid=?";        }        int count = this.getJdbcTemplate().update(sql, money, id);        if(count>0){            flag=true;        }        return flag;    }}

 // 测试 JDBC  连接    //测试 事务    @Test    public  void  jdbc(){        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext11JDBCj.xml");        IServiceAccount serviceAccount = (IServiceAccount)ctx.getBean("show");        try {           serviceAccount.buyStock1(1,10,1,1000);        } catch (ShockException e) {            e.printStackTrace();        }    }


原创粉丝点击