购买股票的使用案例:
数据库表的创建:
account表
stock表:
业务开发
实体类的创建:
public class Account { private Integer aid; private String aname; private String abalance; public Integer getAid() { return aid; } public void setAid(Integer aid) { this.aid = aid; } public String getAname() { return aname; } public void setAname(String aname) { this.aname = aname; } public String getAbalance() { return abalance; } public void setAbalance(String abalance) { this.abalance = abalance; } }
public class Stock { private Integer sid; private String scount; private String sname; public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public String getScount() { return scount; } public void setScount(String scount) { this.scount = scount; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } }
Dao层的开发: 账户
public interface IAccountDao { public boolean updateAccount(int aid,int abalance,boolean isBuy); }
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao { public boolean updateAccount(int aid, int abalance, boolean isBuy) { boolean flag=false; String sql=null; int count; if (isBuy){ sql="update account set abalance=abalance-? where aid=?"; count= this.getJdbcTemplate().update(sql,abalance,aid); }else{ sql="update account set abalance=abalance+? where aid=?"; count= this.getJdbcTemplate().update(sql,abalance,aid); } if (count>0){ flag=true; } return false; } }
股票:
public interface IStockDao { public boolean updateStock(int sid,int scount,boolean isBuy); }
public class IStockDaoImpl extends JdbcDaoSupport implements IStockDao { public boolean updateStock(int sid, int scount, boolean isBuy) { boolean flag=false; String sql=null; int count; if (isBuy){ sql="update stock set scount=scount+? where sid=?"; count= this.getJdbcTemplate().update(sql,scount,sid); }else{ sql="update stock set scount=scount+? where sid=?"; count= this.getJdbcTemplate().update(sql,scount,sid); } if (count>0){ flag=true; } return false; } }
Service开发:
public interface IAccountService { public boolean updateService(int aid,int abablance,int sid,int count) throws StockExcpetion; }
public class IAccountServiceImpl implements IAccountService { IAccountDao accountDao; IStockDao stockDao; 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; } //@Transactional public boolean updateService(int aid, int abablance, int sid, int count) throws StockExcpetion { boolean flag=true; if(1==1){ throw new StockExcpetion("股票购买失败!"); } boolean b = accountDao.updateAccount(aid,abablance,flag); boolean b1 = stockDao.updateStock(sid,count,flag); if (b&&b1){ return true; }else{ return false; } } }
异常类的书写:
public class StockExcpetion extends ClassNotFoundException { public StockExcpetion() { } public StockExcpetion(String s) { super(s); } }
xml配置文件的配置:
这里实现事务的提交一共有三种方法 都列举出来 咱们使用其中一种: 代码如下:
<bean id="dataSources" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--识别到properties文件 两种配置方式 --> <!--配置一--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--Dao --> <bean id="accountDaos" class="Dao.AccountDaoImpl"> <property name="dataSource" ref="dataSources"></property> </bean> <bean id="stockDaos" class="Dao.IStockDaoImpl"> <property name="dataSource" ref="dataSources"></property> </bean> <!--service--> <bean id="accountService" class="Service.IAccountServiceImpl"> <property name="accountDao" ref="accountDaos"></property> <property name="stockDao" ref="stockDaos"></property> </bean> <!--事务管理器--> <bean id="transactionManagers" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSources"></property> </bean> <!--事务代理工厂--> <!-- <bean class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManagers"></property> <property name="target" ref="accountService"></property> <property name="transactionAttributes"> <props > <prop key="addStock" >ISOLATION_DEFAULT,PROPAGATION_REQUIRED</prop> </props> </property> </bean>--> <!--注解事务--> <!-- <tx:annotation-driven transaction-manager="transactionManagers"></tx:annotation-driven>--> <!--AspectJ AOP 形成事务 --> <tx:advice id="txadvice" transaction-manager="transactionManagers"> <tx:attributes> <tx:method name="updateService" isolation="DEFAULT" propagation="REQUIRED" rollback-for="StockExcpetion"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="mypoint" expression="execution(* Service.IAccountServiceImpl.*(..))"></aop:pointcut> <aop:advisor advice-ref="txadvice" pointcut-ref="mypoint"></aop:advisor> </aop:config>
测试类代码书写:
public class Test20180315 { @Test public void Test01() throws StockExcpetion { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextTrancation.xml"); IAccountService accountService = (IAccountService)context.getBean("accountService"); accountService.updateService(1,500,1,20); } }
简述一下运行结果,单测成功,中途会打印异常信息,由于开启了事务,同生共死,俩张表在数据库都没有改变值,事务的出口被回滚了