Spring事务-三种实现方式
一、引入JAR文件:

二、开始搭建分层架构---创建账户(Account)和股票(Stock)实体类
Account:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/* * 账户 */public class Account { private int aid;//账户编号 private String aname;//账户名称 private double balance;//账户金额 public int getAid() { return aid; } public void setAid(int aid) { this.aid = aid; } public String getAname() { return aname; } public void setAname(String aname) { this.aname = aname; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } |
Stock:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/* * 股票 */public class Stock {private int sid;//股票编号private String sname;//名称private int count;//股数public int getSid() { return sid;}public void setSid(int sid) { this.sid = sid;}public String getSname() { return sname;}public void setSname(String sname) { this.sname = sname;}public int getCount() { return count;}public void setCount(int count) { this.count = count;}} |
三、创建Dao层,定义账户以及股票的接口,自定义新增和修改的方法,实现类实现该接口,重写方法
IAccountDao:
|
1
2
3
4
5
6
|
public interface IAccountDao { //添加账户 public int addAccount(Account account); //修改账户 public int updateAccount(int aid,int money,boolean isBuyOrNot);<br> |
//查询余额
public int selectMoney(int aid);
|
1
|
} |
IStockDao:
|
1
2
3
4
5
6
7
|
public interface IStockDao { //添加股票 public int addStock(Stock stock); //修改股票 public int updateStock(int aid,int num,boolean isBuyOrNot);} |
AccountDaoImpl:实现类。继承自JdbcDaoSupport并实现IAccountDao接口,在这里需要用到JDBC模板的getJdbcTemplate(),通过该方法实现对SQL语句增删改查。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao{ //添加 public int addAccount(Account account) { String sql="insert into account(aid,aname,balance) values(?,?,?)"; int count=this.getJdbcTemplate().update(sql, account.getAid(),account.getAname(),account.getBalance()); return count; } //修改 public int updateAccount(int aid, int money, boolean isBuyOrNot) { String sql=null; if(isBuyOrNot){ 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); return count; } |
StockDaoImpl:实现类同理
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class StockDaoImpl extends JdbcDaoSupport implements IStockDao{ //添加股票 public int addStock(Stock stock) { String sql="insert into stock(sid,sname,count) values(?,?,?)"; int count=this.getJdbcTemplate().update(sql, stock.getSid(),stock.getSname(),stock.getCount()); return count; } //修改 public int updateStock(int aid, int num, boolean isBuyOrNot) { String sql=null; if(isBuyOrNot){ sql="update stock set count=count+? where sid=?"; } else{ sql="update stock set count=count-? where sid=?"; } int count=this.getJdbcTemplate().update(sql, num,aid); return count; } |
四、业务逻辑层:service
定义接口IStockService,并实现添加账户,股票,以及购买股票的方法.购买股票需要传入账户的id,股票的id。以及金额,股数
|
1
2
3
4
5
6
7
8
9
|
public interface IStockService { //添加账户 public int addAccount(Account account); //添加股票 public int addStock(Stock stock); //购买股票 public void buyStock(int aid,int money,int sid,int num) throws StockException;} |
实现类:StockServiceImpl。重写方法。并植入Dao。以及自定义StockException异常,用于判定用户的余额小于0,抛出异常

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class StockServiceImpl implements IStockService{ //植入dao private IAccountDao accountDao; private IStockDao stockDao; //添加账户 public int addAccount(Account account) { return accountDao.addAccount(account); } //添加股票 public int addStock(Stock stock) { return stockDao.addStock(stock); } //购买一股票 public void buyStock(int aid, int money, int sid, int num) throws StockException { boolean isBuy=true; accountDao.updateAccount(aid, money, isBuy); if(accountDao.selectMoney(aid)<=0){ throw new StockException("捕获异常!!!"); } stockDao.updateStock(aid, num, isBuy); } |
五、Spring配置文件。[重点]
方式一:通过事务代理工厂bean进行配置[XML方式]
①引入一系列的约束头文件以及标签

②配置C3P0数据源以及DAO、Service

③配置事务管理器以及事务代理工厂Bean。测试类getBean获取的是代理工厂id

方式二:注解。测试类getBean获取的id是原始对象service
|
1
2
|
<!-- 注解 --> <tx:annotation-driven transaction-manager="mytx"/> |

方式三:Aspectj AOP配置事务 。同理 测试类getBean方法id获取的是原始对象

测试类:
|
1
2
3
4
5
6
7
8
9
|
public class Test01 {@Testpublic void addTest() throws StockException{ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); IStockService service = (IStockService)ctx.getBean("stockService"); service.buyStock(1, 800, 1, 2);} |
Spring事务-三种实现方式的更多相关文章
- spring ioc三种注入方式
spring ioc三种注入方式 IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转 什么是控制反转? 控制反转是一种将组件依赖关系的创建和管理置于程序外部的技术. 由容 ...
- Spring IOC 三种注入方式
1. 接口注入 2. setter注入 3. 构造器注入 对象与对象之间的关系可以简单的理解为对象之间的依赖关系:A类需要B类的一个实例来进行某些操作,比如在A类的方法中需要调用B类 ...
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)(摘抄)
IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转, Spring 框架的核心基于控制反转原理. 什么是控制反转?控制反转是一种将组件依赖关系的创建和管理置于程序外部的技 ...
- Spring的三种注入方式(Setter、构造函数和自动注入)
一.Setter注入 这里我是希望在Student.java中调用Course.java中的内容. public class Course { public String name = "数 ...
- Spring bean三种创建方式
spring共提供了三种实例化bean的方式:构造器实例化(全类名,反射).工厂方法(静态工厂实例化 动态工厂实例化)和FactoryBean ,下面一一详解: 1.构造器实例化 City.jav ...
- Spring IOC 三种注入方式(构造器,setter,接口)
Spring的依赖注入有三种方式: 1,构造器注入 2,setter注入 3,接口注入 下面我给大家介绍一下这三种注入 1.构造器注入 构造器注入主要是依赖于构造方法去实现,构造方法可以是有参也可以是 ...
- spring的三种注入方式
接口注入(不推荐) 构造器注入(死的应用) getter,setter方式注入(比较常用) Type1 接口注入 我们常常借助接口来将调用者与实现者分离.如: public class ClassA ...
- Spring MVC三种返回方式
spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. 下面一一进行说明: 1.ModelAndV ...
- spring Bean的三种配置方式
Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...
随机推荐
- HDU_1241 Oil Deposits(DFS深搜)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
- Angularjs中编写指令模版
angular.module('moduleName', []).directive( 'namespaceDirectiveName', [ function() { return { restri ...
- php中include文件夹分析
include是包含很多php文件的一种汇总:一般放在文件夹最外层. <?php header("content-type:text/html;charset=utf-8") ...
- 支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url.
支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url. 现支付宝的通知有两类. A服务器通知,对应的参数为notify_url,支付宝通知使用POST方式 B页面跳转通 ...
- Python 学习之urllib模块---用于发送网络请求,获取数据
1.urllib urllib是Python标准库的一部分,包含urllib.request,urllib.error,urllib.parse,urlli.robotparser四个子模块. (1) ...
- iOS: 属性列表介绍 Introduction to Property Lists
iOS: 属性列表介绍 Introduction to Property Lists 从本质上说, 属性列表就是苹果的对象数据序列化与反序列化方式 属性列表使用几种数据类型把数据组织为键值表和值表 P ...
- 对c++服务端进行覆盖率统计
(1)首先需要为每个被测程序的所有编译文件增加选项,如果文件太多,这无疑是灾难,可利用spec文件达到目的 sed -i '$ a\export LD_PRELOAD=/usr/local/bin/c ...
- ByteArrayInputStream 和 ByteArrayOutputStream
package java.io; /** * A <code>ByteArrayInputStream</code> contains * an internal buffer ...
- register 不允许 block 模式,而默认的是
Exception in thread "main" java.nio.channels.IllegalBlockingModeException at java.nio.chan ...
- 全连接的BP神经网络
<全连接的BP神经网络> 本文主要描述全连接的BP神经网络的前向传播和误差反向传播,所有的符号都用Ng的Machine learning的习惯.下图给出了某个全连接的神经网络图. 1前向传 ...