shoppingcartimpl.java

来自「购物车系统」· Java 代码 · 共 120 行

JAVA
120
字号
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 + =
减小字号Ctrl + -
显示快捷键?