Spring配置事务

本文介绍了一个使用Spring框架管理JDBC事务的例子,包括配置数据源、定义DAO接口及实现类,并通过Spring AOP实现了事务控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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();
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值