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 ...
随机推荐
- typename在C++中的用法
. //在C++中typename一般用来声明模板的模板参数(template parameter): template<typename T> class X; //T是一个模板参数 . ...
- C3P0与DBUtil配合实现DAO层的开发
写在前面:菜鸟拙见,望请纠正 一:为什么需要连接池 普通的JDBC数据库连接使用 DriverManager 来获取,每次向数据库建立连接的时候都要将 Connection 加载到内存中,需要数据库连 ...
- #leetcode刷题之路33-搜索旋转排序数组
假设按照升序排序的数组在预先未知的某个点上进行了旋转.( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ).搜索一个给定的目标值,如果数组中存在这个目标值,则返 ...
- go语言的结构体指针
Go 语言结构体 Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型. 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合. 结构体表示一项记录,比 ...
- GoLang 命令
目录 查看可用命令 build 和 run 命令 go build编译时的附加参数 clent命令 fmt 和 doc 命令 get 命令 远程包的路径格式 go get+远程包 go get使用时的 ...
- Python学习之迭代器和生成器
那么首先什么是迭代器和生成器呢? 迭代器即迭代的工具,那么什么又是迭代呢?所谓迭代:迭代是一个重复的过程,每次重读即一次迭代,并且每次迭代的结果都是下一次迭代的初始值.例: l=[1,2,3] cou ...
- 如何查看PostgreSQL的checkpoint 活动
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页 作者:高健@博客园 luckyjackgao@g ...
- ISP与IAP
ISP:in system program 顾名思义,在系统编程,单片机不用从电路上拆下,直接用下载器或者串口即可完成程序的烧写.这个是用于工程师调试程序,或者出厂时烧写程序.本质上是芯片出厂时烧录到 ...
- 【LG3721】[HNOI2017]单旋
[LG3721][HNOI2017]单旋 题面 洛谷 题解 20pts 直接模拟\(spaly\)的过程即可. 100pts 可以发现单旋最大.最小值到根,手玩是有显然规律的,发现只需要几次\(lin ...
- 使用Fiddler模拟客户端http响应【转】
转自:使用Fiddler模拟客户端http响应 在客户端开发中,常常需要对一些特殊情况做处理,比如404.503等,又比如服务返回错误数据等.而测试这些情况会比较麻烦,往往都是找开发人员配合修改代码, ...