SSM-Spring-21:Spring中事物的使用案例
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
股票买卖案例(我会用三种开启事物的方法 代理工厂bean版的,注解版的,aspectj xml版的)
简单的介绍一下这个小例子,就是俩个表,一个就是你的账户表,一张就是你的股票的表
一切从简,写的简单一点(你可以看成有很多不严谨的地方,只是一个Spring中的事务的简单使用,仅供理解事务,不用想太多)
之前的那些jar包就不说了,从案例开始
01.数据库准备:
01.1账户表Account

01.2股票表Stock

02.IDEA中分层开发:
02.1实体类(前面俩个实体类可以说在这个例子里没有用到,但是这是一种习惯)
StockInfo表
package cn.dawn.day22tx.entity; /**
* Created by Dawn on 2018/3/15.
*/
public class StockInfo {
private Integer sid;
private String sname;
private Integer scount; public Integer getSid() {
return sid;
} public void setSid(Integer sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public Integer getScount() {
return scount;
} public void setScount(Integer scount) {
this.scount = scount;
}
}
AccountInfo表
package cn.dawn.day22tx.entity; /**
* Created by Dawn on 2018/3/15.
*/
public class AccountInfo {
private Integer aid;
private String aname;
private Integer ablance; 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 Integer getAblance() {
return ablance;
} public void setAblance(Integer ablance) {
this.ablance = ablance;
}
}
StockException类
package cn.dawn.day22tx.entity; /**
* Created by Dawn on 2018/3/15.
*/
public class StockException extends ClassNotFoundException {
public StockException() {
} public StockException(String s) {
super(s);
}
}
这个类有点要说的,Spring的事务中执行一半遇到异常怎么操作?默认情况下是运行异常会回滚/受查异常会提交这执行过的代码,所以我做了个受查异常,一会可以测试一下
02.2dao层,一个对股票表操作,一个对账户表操作
IStockDAO股票接口
package cn.dawn.day22tx.dao; /**
* Created by Dawn on 2018/3/15.
*/
public interface IStockDAO {
public boolean updateStock(int sid,int scount,boolean isBuy);
}
StockDAOImpl股票接口的实现类
package cn.dawn.day22tx.dao; import org.springframework.jdbc.core.support.JdbcDaoSupport; /**
* Created by Dawn on 2018/3/15.
*/
public class StockDAOImpl extends JdbcDaoSupport implements IStockDAO {
public boolean updateStock(int sid, int scount, boolean isBuy) {
String sql="";
boolean flag=false;
if (isBuy){
//买股票
sql="update stock set scount=scount+? where sid=?";
}else {
sql="update stock set scount=scount-? where sid=?";
}
int result = this.getJdbcTemplate().update(sql,scount,sid);
if (result>) {
flag = true;
}
return flag;
}
}
这儿我将isBuy的布尔变量当等于true的时候,进行买股票操作,你的股票表上的股票股数增加
IAccountDAO账户接口
package cn.dawn.day22tx.dao; /**
* Created by Dawn on 2018/3/15.
*/
public interface IAccountDAO {
public boolean updateAccount(int aid,int ablance,boolean isBuy);
}
AccountDAOImpl账户的实现类
package cn.dawn.day22tx.dao; import org.springframework.jdbc.core.support.JdbcDaoSupport; /**
* Created by Dawn on 2018/3/15.
*/
public class AccountDAOImpl extends JdbcDaoSupport implements IAccountDAO {
public boolean updateAccount(int aid, int ablance, boolean isBuy) {
String sql="";
boolean flag=false;
if(isBuy){
//买股票
sql="update account set ablance=ablance-? where aid=?";
}else {
sql="update account set ablance=ablance+? where aid=?";
}
int count = this.getJdbcTemplate().update(sql, ablance, aid);
if(count>){
flag=true;
}
return flag;
}
}
02.3service层
股票的交易接口IStockPayService
package cn.dawn.day22tx.service; import cn.dawn.day22tx.entity.StockException; /**
* Created by Dawn on 2018/3/15.
*/
public interface IStockPayService {
public boolean buyStock(int aid,int ablance,int sid,int scount) throws StockException;
}
股票交易的实现类StockPayServiceImpl
package cn.dawn.day22tx.service; import cn.dawn.day22tx.dao.IAccountDAO;
import cn.dawn.day22tx.dao.IStockDAO;
import cn.dawn.day22tx.entity.StockException;
import org.springframework.transaction.annotation.Transactional; /**
* Created by Dawn on 2018/3/15.
*/
public class StockPayServiceImpl implements IStockPayService {
IStockDAO iStockDAO;
IAccountDAO iAccountDAO;
//@Transactional(rollbackFor = StockException.class)
public boolean buyStock(int aid, int ablance, int sid, int scount) throws StockException {
boolean isBuy=true;
boolean sflag = iStockDAO.updateStock(sid, scount, isBuy); if(true){
throw new StockException("网络被挂掉异常");
} boolean aflag = iAccountDAO.updateAccount(aid, ablance, isBuy); if(sflag&&aflag)
return true;
else
return false;
} public IStockDAO getiStockDAO() {
return iStockDAO;
} public void setiStockDAO(IStockDAO iStockDAO) {
this.iStockDAO = iStockDAO;
} public IAccountDAO getiAccountDAO() {
return iAccountDAO;
} public void setiAccountDAO(IAccountDAO iAccountDAO) {
this.iAccountDAO = iAccountDAO;
}
}
看到那个方法上面注释掉的那个注解了吗?那是第二种方式要用到,只有在第二种方式注解版的时候才用到,其他俩种方式就注释掉它,免得有干扰,切记,用的时候把注释消掉,不用就注释它
@Transactional里面我只写了遇到什么异常会回滚,它的传播行为和隔离级别有默认值,我就省略了
03.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--配置jdbc。properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--阿里的Druid-->
<bean id="dataSource" 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> <!--dao层-->
<bean id="stockDAO" class="cn.dawn.day22tx.dao.StockDAOImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="accountDAO" class="cn.dawn.day22tx.dao.AccountDAOImpl">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--service-->
<bean id="stockpPayService" class="cn.dawn.day22tx.service.StockPayServiceImpl">
<property name="iAccountDAO" ref="accountDAO"></property>
<property name="iStockDAO" ref="stockDAO"></property>
</bean> <!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--第三种方式 aspectj xml版-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="buyStock" isolation="DEFAULT" propagation="REQUIRED" rollback-for="StockException"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="mypointccut" expression="execution(* *..day22tx.service.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txadvice" pointcut-ref="mypointccut"></aop:advisor>
</aop:config> <!--第二种事务开启方式,注解版-->
<!--<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>--> <!--第一种事务开启方式代理工厂-->
<!--<bean id="txService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"></property>
<property name="target" ref="stockpPayService"></property>
<property name="transactionAttributes">
<props>
<prop key="buyStock">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-StockException</prop>
</props>
</property>
</bean>-->
</beans>
没什么需要说的,这儿像什么命名空间等需要注意一点,其他的就是照猫画虎
有什么不清楚的评论留言,我会帮忙解决
04.单测方法:
package cn.dawn.day22tx; import cn.dawn.day22tx.entity.StockException;
import cn.dawn.day22tx.service.IStockPayService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180315 {
@Test
/*aspectj xml方式*/
public void t03() {
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day22tx.xml");
IStockPayService service = (IStockPayService) context.getBean("stockpPayService");
boolean flag = false;
try {
flag = service.buyStock(, , , );
} catch (StockException e) {
e.printStackTrace();
}
if(flag){
System.out.println("购买成功");
} } @Test
/*注解方式*/
public void t02() {
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day22tx.xml");
IStockPayService service = (IStockPayService) context.getBean("stockpPayService");
boolean flag = false;
try {
flag = service.buyStock(, , , );
} catch (StockException e) {
e.printStackTrace();
}
if(flag){
System.out.println("购买成功");
} } @Test
/*代理工厂bean的方式*/
public void t01() {
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day22tx.xml");
IStockPayService service = (IStockPayService) context.getBean("txService");
boolean flag = false;
try {
flag = service.buyStock(, , , );
} catch (StockException e) {
e.printStackTrace();
}
if(flag){
System.out.println("购买成功");
} }
}
简述一下运行结果,单测成功,中途会打印异常信息,由于开启了事务,同生共死,俩张表在数据库都没有改变值,事务的出口被回滚了
SSM-Spring-21:Spring中事物的使用案例的更多相关文章
- mysql数据库隔离级别及其原理、Spring的7种事物传播行为
一.事务的基本要素(ACID) 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做,不可能停滞在中间环节.事务执行过程中出错,会回滚到事务开始前的状态,所有的操作就像没有 ...
- SSM(spring mvc+spring+mybatis)学习路径——2-1、spring MVC入门
目录 2-1 Spring MVC起步 一.回顾Servlet 二.SpringMVC简介 三.搭建SpringMVC第一个案例 四.简单流程及配置 五.使用注解开发Controller 六.参数绑定 ...
- [原创]java WEB学习笔记109:Spring学习---spring中事物管理
博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好 ...
- SSM(spring mvc+spring+mybatis)学习路径——2-2、spring MVC拦截器
目录 2-2 Spring MVC拦截器 第一章 概述 第二章 Spring mvc拦截器的实现 2-1 拦截器的工作原理 2-2 拦截器的实现 2-3 拦截器的方法介绍 2-4 多个拦截器应用 2- ...
- 浅谈IDEA集成SSM框架(SpringMVC+Spring+MyBatis)
前言 学习完MyBatis,Spring,SpringMVC之后,我们需要做的就是将这三者联系起来,Spring实现业务对象管理,Spring MVC负责请求的转发和视图管理, MyBatis作为数据 ...
- 一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程重要
前言 SSM(Spring+SpringMVC+Mybatis)是目前较为主流的企业级架构方案,不知道大家有没有留意,在我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教 ...
- ssm(spring,spring mvc,mybatis)框架
ssm框架各个技术的职责 spring :spring是一个IOC DI AOP的 容器类框架 spring mvc:spring mvc 是一个mvc框架 mybatis:是一个orm的持久层框架 ...
- 使用Eclipse搭建SSM框架(Spring + Spring MVC + Mybatis)
1.创建项目 1)打开Eclipse,点击File --> New --> Other 2)输入maven,找到Maven Project 3)然后一直按Next,直到出现一下界面: 4) ...
- java web后台开发SSM框架(Spring+SpringMVC+MyBaitis)搭建与优化
一.ssm框架搭建 1.1创建项目 新建项目后规划好各层的包. 1.2导入包 搭建SSM框架所需包百度云链接:http://pan.baidu.com/s/1cvKjL0 1.3整合spring与my ...
随机推荐
- Linux文件系统的简单操作 - df, du, ln
现在我们知道磁盘的整体数据是在 superblock 区块中,但是每个各别文件的容量则在 inode 当中记载的. 那在文字接口底下该如何叫出这几个数据呢?底下就让我们来谈一谈这两个命令: df:列出 ...
- SharePoint 2007 列表页定制--4个默认页定制
以"简单的领导简介"为例,欢迎大家指正 背景:项目中需要有领导简介的模块,就开始制作领导简介,本来很简单,有一个列表就可以,然后在前台展示出来,但是客户看到我们的效果,尤其输入领导 ...
- LeetCode(39)-Intersection of Two Linked Lists
听歌曲初爱有感: 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: ...
- ethtool确定网卡对应的物理网口
在配置有多个网络接口的设备时我们会犯难,eth0.eth1.--到底是那个接口? 我使用的机器是CentOS系统,打开终端,输入ethtool –help 显示帮助信息,下面我就简要介绍一下最常用的两 ...
- VS Code python初体验笔记
之前一直都是使用Notepad++来编写Python代码,后来想起来之前查资料的时候有个VS Code可以编写一些的脚本语言(js,node.js)甚至是高级编程语言(C#,PHP,JAVA,Pyth ...
- css左侧固定宽度右侧自适应
左侧固定宽,右侧自适应屏幕宽: 左右两列,等高布局: 左右两列要求有最小高度,例如:200px;(当内容超出200时,会自动以等高的方式增高) 要求不用JS或CSS行为实现: 仔细分析试题要求,要达到 ...
- JQuery(一)---- JQ的选择器,属性,节点,样式,函数等操作详解
JQuery的基本概念 JQuery是一个javascript库,JQuery凭借着简洁的语法和跨平台的兼容性,极大的简化了js操作DOM.处理事件.执行动画等操作.JQuery强调的理念是:'wri ...
- 大数据项目中的Oracle查询优化
今天发现自己之前写的一些SQL查询在执行效率方面非常不理想,于是尝试做了些改进. 需求为查询国地税表和税源表中,国税有而税源没有的条目数,之前的查询如下: SELECT COUNT(NAME) FRO ...
- 使用IntelliJ IDEA的小技巧快乐编程(2)
前言 本篇介绍的技巧为IntelliJ IDEA中自动代码生成相关的技巧,合理的使用这些技巧将大大提高的你的编码效率 :) Trick 6. 使用模板代码 idea默认的提供了许多模板代码,你可以使用 ...
- java——多态
多态定义:某一类事物的多种存在形态.对象的多态性.猫这类事物即具备猫的形态,又具备着动物的形态,这就是对象的多态性.简单说:就是一个对象对应着不同类型.多态在代码中的体现:父类或者接口的引用指向其子类 ...