Spring事务

dao层

IAccountDAO接口
package cn.happy.spring21.cn.happy.dao;


import cn.happy.spring21.cn.happy.entity.Account;

/**
 * Created by Happy on 2017-08-04.
 * 账户类
 */
public interface IAccountDAO {
    //开户的方法
    public boolean addAccount(Account account);
    //修改账户余额
    public boolean updateAccount(int aid, double money, boolean isBuy);
}
IStockDAO接口
package cn.happy.spring21.cn.happy.dao;


import cn.happy.spring21.cn.happy.entity.Stock;

/**
 * Created by Happy on 2017-08-04.
 */
public interface IStockDAO {
    public boolean addStock(Stock stock);
    public boolean updateStock(int sid, int count, boolean isBuy);
}
AccountDAOImpl
package cn.happy.spring21.cn.happy.dao;


import cn.happy.spring21.cn.happy.entity.Account;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 * Created by Happy on 2017-08-04.
 */
public class AccountDAOImpl extends JdbcDaoSupport implements IAccountDAO {

    public boolean addAccount(Account account) {
        return false;
    }

    public boolean updateAccount(int aid, double money, boolean isBuy) {
        boolean flag=false;
        String sql=null;
        if (isBuy){  //购买股票
            sql="update account set balance=balance-? where aid=?";
        }else{
             sql="update account set balance=balance+? where aid=?";
        }
        int count = this.getJdbcTemplate().update(sql, money, aid);
        if (count>0){
            flag=true;
        }
        return flag;
    }
}
StockDAOImpl
package cn.happy.spring21.cn.happy.dao;
import cn.happy.spring21.cn.happy.entity.Stock;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
 * Created by Happy on 2017-08-04.
 */
public class StockDAOImpl extends JdbcDaoSupport implements IStockDAO {

    public boolean addStock(Stock stock) {
        return false;
    }

    public boolean updateStock(int sid, int count, 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 str = this.getJdbcTemplate().update(sql,count,sid);
        if (str>0){
            flag=true;
        }
        return flag;
    }
}
service层
IAccountService接口 
package cn.happy.spring21.cn.happy.service;

import cn.happy.spring21.cn.happy.entity.StockException;
import cn.happy.spring21.cn.happy.entity.Account;
import cn.happy.spring21.cn.happy.entity.Stock;
import cn.happy.spring21.cn.happy.entity.StockException;

/**
 * Created by Happy on 2017-08-04.
 */
public interface IAccountService {
    //开户的方法
    public boolean addAccount(Account account);
    //股票初始化的方法
    public boolean addStock(Stock account);


    /**
     * @param sid
     * @param count
     * @param aid
     * @param money
     */
    public void buyStock(int sid, int count, int aid, double money) throws StockException;

}
AccountServiceImpl
package cn.happy.spring21.cn.happy.service;

import cn.happy.spring21.cn.happy.dao.IAccountDAO;
import cn.happy.spring21.cn.happy.dao.IStockDAO;
import cn.happy.spring21.cn.happy.entity.Account;
import cn.happy.spring21.cn.happy.entity.Stock;
import cn.happy.spring21.cn.happy.entity.StockException;

/**
 * Created by Happy on 2017-08-04.
 */
public class AccountServiceImpl implements IAccountService {

    //植入AccountDao对象
    private IAccountDAO accountDAO;
    //植入StockDAO对象
    private IStockDAO  stockDAO;

    public boolean addAccount(Account account) {
        return false;
    }

    public boolean addStock(Stock account) {
        return false;
    }

    public void buyStock(int sid, int count, int aid, double money) throws StockException {
        //默认是购买股票
        boolean isBuy=true;

         //01.账户
         accountDAO.updateAccount(aid,money,isBuy);

           if(1==1) {
               throw new StockException();
           }
        //02.股票表
        stockDAO.updateStock(sid,count,isBuy);
    }

    public IAccountDAO getAccountDAO() {
        return accountDAO;
    }

    public void setAccountDAO(IAccountDAO accountDAO) {
        this.accountDAO = accountDAO;
    }

    public IStockDAO getStockDAO() {
        return stockDAO;
    }

    public void setStockDAO(IStockDAO stockDAO) {
        this.stockDAO = stockDAO;
    }
}

applicationContextspring18.xml
 <context:property-placeholder location="jdbc.properties"></context:property-placeholder>


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>


    <bean id="accountDAO" class="cn.happy.spring21.cn.happy.dao.AccountDAOImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="stockDAO" class="cn.happy.spring21.cn.happy.dao.StockDAOImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="accountService" class="cn.happy.spring21.cn.happy.service.AccountServiceImpl">
        <property name="accountDAO" ref="accountDAO"></property>
        <property name="stockDAO" ref="stockDAO"/>
    </bean>
测试
 @Test
    public void test01() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring18.xml");
        IAccountService service = (IAccountService) ctx.getBean("accountService");
        try{
            service.buyStock(1,3,1,200);
        }catch (StockException e){
            e.printStackTrace();
       
   
 applicationContextspring18.xml  事务

<!--事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--事务代理工厂-->
    <bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!--引用事务管理器-->
        <property name="transactionManager" ref="transactionManager"></property>

    <!--目标对象-->
        <property name="target" ref="accountService"></property>
        <!--增强 -->
        <property name="transactionAttributes">
            <props>
                <prop key="add*">ISOLATION_DEFAULT,PROPAGATION_REQUIRED</prop>
                <prop key="buy*">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-StockException</prop>
            </props>
        </property>
    </bean>
测试
 @Test
    public void test02(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextspring18.xml");
        IAccountService service = (IAccountService) ctx.getBean("accountServiceProxy");
        try{
            service.buyStock(1,3,1,200);
        }catch (StockException e){
            e.printStackTrace();
        }
    }













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值