package spring.hibernate.plugin.dao.impl;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import spring.hibernate.plugin.dao.RestaurantDao;
import spring.hibernate.plugin.exception.*;
/**
* 用hql进行数据库操作
* @author carson0408
*
*/
//dao组件用@Repository注释
@Repository
public class RestaurantDaoImpl implements RestaurantDao {
//自动装配,省去了setter方法
@Autowired
private SessionFactory sessionfactory;
/**
* 连接当前线程,使用SessionFactory的getCurrentSession()方法。
* @return
*/
private Session getSession(){
return sessionfactory.openSession();
}
@Override
public float findPriceByIdenti(String identi) {
// TODO Auto-generated method stub
String hql="select f.price from Food f where f.identi=:a";
float price=(float) getSession().createQuery(hql).setString("a", identi).uniqueResult();
return price;
}
@Override
public void updateFoodStock(String identi, int n) {
// TODO Auto-generated method stub
String hq="Select f.stock from Food f where f.identi=:identi";
int stock=(int) getSession().createQuery(hq).setString("identi", identi).uniqueResult();
if(stock < n){
throw new OutofStockException("so sorry,this food is sold out");
}
String hql="update Food f set f.stock=f.stock-:a where f.identi=:b";
getSession().createQuery(hql).setInteger("a", n).setString("b", identi).executeUpdate(); //更新事物剩余量
}
@Override
public void updateAccount(String customername, float price, int n) {
// TODO Auto-generated method stub
//验证客户所持有的饭店支付卡余额是否足够
float cost=n*price;
String hql="select c.balance from Customer c where c.customername=:a";
float balance=(float) getSession().createQuery(hql).setString("a", customername).uniqueResult();
if(balance < cost){
throw new OutofBalanceException("sorry,your card's money is not enough to pay for this food");
}
String hql1="update Customer c set c.balance=c.balance-:a where c.customername=:b";
getSession().createQuery(hql1).setFloat("a", cost).setString("b", customername).executeUpdate();
}
}
Spring整合Hibernate示例完整代码
需积分: 9 36 浏览量
2018-01-17
20:54:51
上传
评论
收藏 8KB 7Z 举报
carson0408
- 粉丝: 232
- 资源: 4
最新资源
- 1040g0cg310ravpiu6ibg5pg00tsipsln3ju2d0g 2
- 基于Python的SAR图像去噪CNN-NLM设计源码
- redhat6升级到redhat7,过程redhat6.x-> redhat6.10->rehat7.9 主版本最高版本
- 基于Django的流程引擎设计源码
- 基于Node.js的Express框架与MySQL的后台管理系统设计源码
- 基于Java的Flink流批一体数据处理快速集成开发框架设计源码
- FirstFilterOrderCompare
- Screenshot_2024-03-28-19-17-25-020_com.ss.android.lark.jpg
- 基于Java的车辆违章信息查询系统设计源码
- wqeAFSDADWDAESD
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈