⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcartimpl.java

📁 购物车系统
💻 JAVA
字号:
package com.estore.dao.impl;

import java.util.*;

import org.hibernate.*;

import com.estore.dao.ShoppingCartDao;
import com.estore.entity.*;
import com.estore.util.*;

public class ShoppingCartImpl implements ShoppingCartDao{

	public Product findProductById(Integer id){
		Session s = null;
		Transaction tran = null;
		Product product=new Product();
		try{
			s = HibernateUtil.getSession();
			tran =s.beginTransaction();
			String hql="from  Product where id=?";
			product=(Product)s.createQuery(hql).setInteger(0, id).uniqueResult();
			tran.commit();
		}catch(Exception e){
			product=null;
			tran.rollback();
			throw new RuntimeException(e.getMessage());
		}finally {
			 HibernateUtil.releaseSession(s);
		}
		return product;
	}
	
	
	public List listProducts() {
		List<Product> pros = new ArrayList<Product>();
		Session s = null;
		Transaction tran = null;
		try {
			s = HibernateUtil.getSession();
			tran = s.beginTransaction();
			String hql = "from Product p";
			Query q = s.createQuery(hql);
			pros =(List<Product>) q.list();
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tran != null)
				tran.rollback();
		 }finally {
			 HibernateUtil.releaseSession(s);
		}
		return pros;
	}


	
	public Order findOrderByOrderId(Integer id){
		Session s = null;
		Transaction tran =null;
		Order order=new Order();
		try{
			s = HibernateUtil.getSession();
			tran =s.beginTransaction();
			String hql="from Order where id=?";
			order=(Order)s.createQuery(hql).setInteger(0,id).uniqueResult();
			tran.commit();
			return order;
		}catch(Exception e){
			tran.rollback();
			throw new RuntimeException(e.getMessage());
		}finally {
			 HibernateUtil.releaseSession(s);
		}
		
	}
	public User findUserByUserId(Integer id){
		Session s = null;
		Transaction tran = null;
		User user=new User();
		try{
			s = HibernateUtil.getSession();
			tran = s.beginTransaction();
			String hql="from User where id=?";
			user=(User)s.createQuery(hql).setInteger(0,id).uniqueResult();
			tran.commit();
			return user;
		}catch(Exception e){
			tran.rollback();
			throw new RuntimeException(e.getMessage());
		}finally {
			 HibernateUtil.releaseSession(s);
		}
		
	}
	
	
	public int generateOrder(Order order) {
		Session s = null;
		Transaction tran=null;
		try{
			s = HibernateUtil.getSession();
			tran=s.beginTransaction();
			s.save(order);
			tran.commit();
			return order.getOid(); 
			
		}catch(Exception e){
			tran.rollback();
			e.printStackTrace();
			throw new  RuntimeException(e.getMessage());
		}finally {
			 HibernateUtil.releaseSession(s);
		}
		
	}



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -