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); }
AccountDAOImplpackage 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
service层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; } }
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(); } }