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

📄 dboperate.java

📁 ShoppingOnline 是一个网上购物系统的源代码 由eclipse+jsp+tomcat+strust
💻 JAVA
字号:
package cn.com.shoppingonline;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class DbOperate {

	/**
	 * 根据用户名得到会员对象
	 */
	public Member getMember(String userId) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Member member = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
//			创建查询对象
			Query query = session.createQuery("from Member where username=:userId");
			query.setParameter("userId", userId);
			List list = query.list();
			if (!list.isEmpty())
				member = (Member) list.get(0);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}
		session.close();
		return member;
	}

	/**
	 * 根据商品ID得到商品对象
	 */
	public Product getProduct(int  Id) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Product  product = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
//			创建查询对象
			Query query = session.createQuery("from Product where id="+Id);
			List list = query.list();
			if (!list.isEmpty())
				product = (Product) list.get(0);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}
		session.close();
		return product;
	}

	/**
	 * 得到指定数量的顶端商品对象
	 */
	public List getTopProducts(int type) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		List list=null;
		String strSql;
		int dispNUm;
		try {
			if (type ==1) {
				strSql="from Product order by saledate desc";//创建一个查询语句,按商品上架时间排序
				dispNUm=4;
			}
			else{
				strSql="from Product  order by salecount desc";//创建一个查询语句,按商品销售数量排序
				dispNUm=10;
			}
			Query query = session.createQuery(strSql); //创建查询对象
			query.setMaxResults(dispNUm);								//记录集最大个数
			list = query.list(); //从数据库取出数据,并自动封装到List集合中
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}
		HibernateUtil.closeSession();
		return list; 
	}
	
	/**
	 * 得到所有符合条件商品对象
	 */
	public List getMatchProducts(int sortId,String keyword) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		List list=null;
		String strSql;
		try {
			if (sortId ==-1) 
					strSql="from Product where name like '%"  +  keyword + "%' ";//创建一个查询语句,按关键字查询
			else  
				strSql="from Product where sortid='" + sortId +"'";//创建一个查询语句,查询指定类别产品
			
			Query query = session.createQuery(strSql); //创建查询对象
			list = query.list(); //从数据库取出数据,并自动封装到List集合中
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}
		HibernateUtil.closeSession();
		return list; 
	}
	
	/**
	 * 得到所有商品类别对象
	 */
	public List getSorts() throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		List list=null;
		try {
			Query query = session.createQuery("from Sort"); //创建查询对象
			list = query.list(); 
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}
		HibernateUtil.closeSession();
		return list; 
	}

	/**
	 * 根据编号得到订单对象
	 */
	public Order  getOrder(String orderno) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Order  order = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
//			创建查询对象
			Query query = session.createQuery("from Order where orderno=:orderno");
			query.setParameter("orderno", orderno);
			List list = query.list();
			if (!list.isEmpty())
				order = (Order) list.get(0);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}
		session.close();
		return order;
	}

	/**
	 * 得到指定用户所有订单对象
	 */
	public List getOrders(int  userid) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		List list=null;
		try {
			Query query = session.createQuery("from Order where userid=" + userid); //创建查询对象
			list = query.list();
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}
		HibernateUtil.closeSession();
		return list; 
	}

	/**
	 * 插入实体对象所对应的记录
	 */
	public void save(Object obj) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		if (obj != null) {
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				session.save(obj);
				tx.commit();
			} catch (HibernateException e) {
				if (tx != null)
						tx.rollback();
				throw e;
			}
		}
		session.close();
	}

	/**
	 * 修改实体对象所对应的记录
	 */
	public void update(Object obj) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		
		if (obj != null) {
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				session.update(obj);
				tx.commit();
			} catch (HibernateException e) {
				if (tx != null)
						tx.rollback();
				throw e;
			}
		}
		session.close();
	}
	
	/**
	 * 删除对象所对应的记录
	 */
	public void delete(Object obj) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		if (obj != null) {
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				session.delete(obj);
				tx.commit();
			} catch (HibernateException e) {
				if (tx != null)
						tx.rollback();
				throw e;
			}
		}
		session.close();
	}
}

⌨️ 快捷键说明

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