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

📄 orderdao.java

📁 本系统是struts+hibernate完成的,主要功能是一个网上购物管理系统
💻 JAVA
字号:
package Entity.Dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import Entity.Pojo.OrderDetail;
import Entity.Pojo.OrderInfo;

public class OrderDao {
	private static Session session = null;

	private static Transaction tx = null;

	/***************************************************************************
	 * 得到当前用户所有账单情况
	 * 
	 * @param userid
	 * @return
	 */
	public static List getOrderInfo(Integer userid) {
		List list = null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			String hql = "From UserInfo u inner join u.order der where u.userId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, userid);
			list = query.list();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return list;
	}

	/***************************************************************************
	 * 记录当前用户此时的定单
	 */
	public void saveOrder(OrderInfo order) {
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			session.save(order);
			session.flush();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	/***************************************************************************
	 * 得到最新的定单ID
	 */

	public static int getOrderID() {
		int order_id = 0;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			List list = null;
			String hql = "select orderId from OrderInfo";
			Query query = session.createQuery(hql);
			list = query.list();
			Iterator iter = list.iterator();
			while (iter.hasNext()) {
				order_id = (Integer) iter.next();
			}
			System.out.println("orderid :" + order_id);
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return order_id;
	}

	/***************************************************************************
	 * 
	 * @param orderid
	 * @return
	 */

	public void saveDetail(OrderDetail detail) {
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			session.save(detail);
			session.flush();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	/***************************************************************************
	 * 提取当前表当中所有价格
	 * 
	 * @param orderid
	 * @return
	 */
	public static double getTotalPrice(int orderid) {
		double total = 0;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			String hql = "select sum(singlePrice) from OrderDetail  where orderId =? group by orderId";
			Query query = session.createQuery(hql);
			query.setInteger(0, orderid);
			List list = query.list();
			Iterator iter = list.iterator();
			while (iter.hasNext()) {
				total = (Double) iter.next();
			}
			System.out.println("total price :" + total);
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return total;
	}

	public static boolean updateOrder(Integer orderid, Double total) {
		int count = 0;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			String hql = "update OrderInfo  set orderTotalPrice=? where orderId=?";
			Query query = session.createQuery(hql);
			query.setDouble(0, total);
			query.setInteger(1, orderid);
			count = query.executeUpdate();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
		if (count == 0) {
			return false;
		} else {
			return true;
		}
	}

	/***************************************************************************
	 * 通过ID得出ORDERdETAL当中的信息
	 * 
	 * @param orderid
	 * @return
	 */

	public static List getOrders(Integer orderid) {
		List list = null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			String hql = "From OrderDetail where orderId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, orderid);
			list = query.list();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return list;
	}

	public static List getInof(Integer id) {
		List list = null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			String hql = "From OrderDetail where id=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, id);
			list = query.list();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return list;
	}

	public static boolean deleteCurrentOrder(Integer id) {
		int count = 0;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			String hql = "DELETE OrderDetail where id=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, id);
			count = query.executeUpdate();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
		if (count == 0) {
			return false;
		} else {
			return true;
		}
	}

	public static boolean updateOrderDetail(Integer id, String goods,
			double singlePrice) {
		int count = 0;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			String hql = "update OrderDetail  set goods=?,singlePrice=? where id=?";
			Query query = session.createQuery(hql);
			query.setString(0, goods);
			query.setDouble(1, singlePrice);
			query.setInteger(2, id);
			count = query.executeUpdate();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
		if (count == 0) {
			return false;
		} else {
			return true;
		}
	}

	public static boolean delOrderInfo(Integer orderId) {
		int count = 0;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			OrderInfo of = (OrderInfo) session.load(OrderInfo.class, orderId);
			session.delete(of);
			session.flush();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			count = 1;
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
		if (count == 0) {
			return true;
		} else {
			return false;
		}
	}

	private static OrderDao dao = new OrderDao();

	public static OrderDao getInstance() {
		return dao;
	}
}

⌨️ 快捷键说明

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