Spring笔记⑤--整合hibernate代码测试
String整合hibernate代码测试
在上节生成的表中插入数据:


注意:使用myeclipse2014生成的整合项目可能
存在问题需要我们自己导入。
第一步 我们写dao接口
|
package com.ssh.spring_hibernate.dao;
public //根据书号获取数的单价 public
//更新书的库存,使书号对应的库存-1 public
//更新用户的账户余额:使username的balance-price public } |
写好其实现类
|
package com.ssh.spring_hibernate.dao;
import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class BookShopDaoImpl implements BookShopDao { /** * 怎么用hibernate * 从SessionFactory中拿到跟当前线程绑定的Session */
@Autowired private SessionFactory sessionFactory;
public Session getSession(){ return sessionFactory.getCurrentSession(); } @Override public int findBookPriceByIsbn(String isbn) { String hql="select b.price from Book b where b.isbn=?"; Query q=getSession().createQuery(hql).setString(0, isbn); return (Integer) q.uniqueResult(); }
@Override public void updataBookStock(String isbn) { //验证书的库存是否足够 String hql2="select b.stock from Book b where b.isbn=?"; int stock=(Integer) getSession().createQuery(hql2).setString(0, isbn).uniqueResult(); if (stock==0) { System.out.println("库存不足!"); } String hql="update Book b set b.stock=b.stock-1 where b.isbn=?"; getSession().createQuery(hql).setString(0, isbn).executeUpdate(); }
@Override public void updateUserAccount(String username, int price) { //验证余额是否足够 String hql2="select a.balance from Account a where a.username=?"; int balance=(Integer) getSession().createQuery(hql2).setString(0, username).uniqueResult(); System.out.println(balance); if (balance<price) { System.out.println("余额不足"); } int result=balance-price; String hql="update Account a set a.balance=? where a.username=?"; getSession().createQuery(hql).setInteger(0, result).setString(1, username).executeUpdate(); System.out.println("余额为"+result); }
} |
注意:需要在spring的配置文件中添加自动扫描的路径
|
<!-- 设置自动扫描的包--> <context:component-scan |
第二步写好service

public
interface BookShopService {
public
void
purchase(String username,String isbn);
}
public
interface
Cashier {
public
void checkout(String username,List<String>isbn);
}
其实现类
|
package com.ssh.spring_hibernate.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import com.ssh.spring_hibernate.dao.BookShopDao;
@Service public class BookShopServiceImpl implements BookShopService{ @Autowired private BookShopDao bookShopDao;
@Override public void purchase(String username, String isbn) { int price =bookShopDao.findBookPriceByIsbn(isbn); bookShopDao.updataBookStock(isbn); bookShopDao.updateUserAccount(username, price); }
} |
|
package com.ssh.spring_hibernate.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
@Service public @Autowired private BookShopService bookShopService;
@Override public for (String is : isbn) { bookShopService.purchase(username, is); }
}
} |
第三步写我们的测试类
|
public
private ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); private BookShopService bookShopService=null; { bookShopService=ctx.getBean(BookShopService.class); }
public DataSource d=ctx.getBean(DataSource.class); System.out.println(d.getConnection()); }
public bookShopService.purchase("aa", "1002"); } public new Go().testBookShopService(); } } |
控制台打印
|
Hibernate: select book0_.PRICE as col_0_0_ from SH_BOOK book0_ where book0_.ISBN=? Hibernate: select book0_.STOCK as col_0_0_ from SH_BOOK book0_ where book0_.ISBN=? Hibernate: update SH_BOOK set STOCK=STOCK-1 where ISBN=? Hibernate: select account0_.BALANCE as col_0_0_ from SH_ACCOUNT account0_ where account0_.USER_NAME=? Hibernate: update SH_ACCOUNT set BALANCE=? where USER_NAME=? |
Spring Hibernate 事务的流程
- 在方法之前
- 获取Session
- 把Session和当前线程绑定,这样就可以在Dao中使用SessionFactory的getCurrentSession()方法来获取Session了
- 开启事务
- 若方法正常结束,即没有出现异常,则
- 提交事务
- 使和当前线程绑定的Session解除绑定
- 关闭Session
3若方法出现异常,则
① 回滚事务
- 使和当前线程绑定的Session解除绑定
- 关闭Session
Spring笔记⑤--整合hibernate代码测试的更多相关文章
- 【Hibernate学习笔记-3】在Spring下整合Hibernate时, 关于sessionFactory的类型的说明
摘要 在Spring下整合Hibernate时,关于sessionFactory的配置方式主要有两种,分别为注解配置方式,和xml配置方式,下面将对这两种配置方式进行介绍. 1. sessionFac ...
- mybatis与spring的整合(代码实现)
mybatis与spring的整合(代码实现) 需要jar包: mybatis核心包:依赖包:log4j包:spring croe;beans;tx;aop;aspects;context;expre ...
- Spring Boot 整合Hibernate Validator
Spring Boot 整合Hibernate Validator 依赖 <dependencies> <dependency> <groupId>org.spri ...
- spring之整合Hibernate
spring整合Hibernate整合什么? 1.让IOC容器来管理Hibernate的SessionFactory. 2.让Hibernate使用上spring的声明式事务. 整合步骤: 1.加入H ...
- Spring Data-Spring整合Hibernate基于JPA规范
JPA:由 Sun 公司提供了一对对于持久层操作的标准(接口+文档) Hibernate:是 Gavin King 开发的一套对于持久层操作的自动的 ORM 框架. Hibernate JPA:是在 ...
- Java框架:spring框架整合hibernate框架的xml配置(使用注解的方式)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring框架整合hibernate框架简单操作数据库
1.配置文件: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http:/ ...
- Spring笔记——配置Hibernate框架事务
原文:http://www.iteye.com/topic/1123347 Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这 ...
- 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)
1.导入jar watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400 ...
随机推荐
- OO第一次博客作业总结反思
使用了masteruml插件来生成类图和metrics插件分析代码 第一次作业 1.UML类图 >在第一次作业中,使用了两个类,代码中有没有使用的变量与函数,为平衡两个类的内容,我将输出函数放在 ...
- pc端js常用方法
var common = {}; /** * [pageMask ajax统一请求] * @return {[type]} [description] */ common.pageMask = fun ...
- TMS Xdata Server
Xdata 在TMS中扮演的桥的角色,一年前仔细看过TMS 的源码,当时对流程很清晰,随着时间慢慢的过去,现在该忘记的都忘记了.所以用此文章来记录自己对Xdata还剩下的一点点的记忆... 光有xda ...
- Verilog 奇数分频
代码: module odd_div( ); ; //分频系数,3即3分频 ; reg clk, rstn, clk_div_pos, clk_div_neg; wire clk_div_out; : ...
- C++ 信号处理
原文:https://www.w3cschool.cn/cpp/cpp-signal-handling.html C++ 信号处理 信号是由操作系统传给进程的中断,会提早终止一个程序.在 UNIX.L ...
- 2015539平措卓玛课堂测试(ch06)
课堂测试(ch06) 1.下面代码中,对数组x填充后,采用直接映射高速缓存,所有对x和y引用的命中率为(D) A .1 B .1/4 C .1/2 D .3/4 解析:缓存命中:当程序需要第(k+1) ...
- spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除
转载: http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天 ...
- 利用BlockingCollection实现生产者和消费者队列,实现写文本
最近开发几个小项目,需要把结果写到txt文件里面,并且按照时间进行分文件,由于对于效率要求较高,所以采用 生产者和消费者 模型来进行写出文本,线程中只需要添加队列就立即返回,而不需要等待写文件的时间 ...
- c语言版本的coroutine
#include <stdio.h> int function(void) { ; switch (state) { : goto LABEL0; : goto LABEL1; } LAB ...
- 【PaPaPa】集成B/S主流技术的MVC5项目 - 实干派:说做就做,我们已经起航,你还在观望吗
我们是谁 我们是C#爱好者,互相分享技术,一起学习一起成长一起做一个项目. 我们是开源爱好者,从我们手上出来的代码都会托管在源代码管理平台(oschina),到目前为止不收费,将来也不会出现任何收费情 ...