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

📄 dboperate.java

📁 eclipse开发入门与提高的全部源码
💻 JAVA
字号:
package cn.com.shopadmin;

import java.util.List;

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

	/**
	 * 根据用户名得到管理员对象
	 */
	public AdminUser getAdminUser(String userId) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		AdminUser adminuser = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
//			创建查询对象
			Query query = session.createQuery("from AdminUser where username=:userId");
			query.setParameter("userId", userId);
			List list = query.list();
			if (!list.isEmpty())
				adminuser = (AdminUser) list.get(0);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}
		session.close();
		return adminuser;
	}
	/**
	 * 根据类别ID得到类别对象
	 */
	public Sort getSort(int  Id) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Sort  sort = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
//			创建查询对象
			Query query = session.createQuery("from Sort where id="+Id);
			List list = query.list();
			if (!list.isEmpty())
				sort = (Sort) list.get(0);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}
		session.close();
		return sort;
	}

	/**
	 * 根据类别ID得到会员对象
	 */
	public Member getMember(int  Id) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Member  member = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
//			创建查询对象
			Query query = session.createQuery("from Member where id="+Id);
			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 getMatchProducts(String keyword) throws HibernateException {
//		创建一个查询语句,按关键字查询
		String strSql="from Product where name like '%"  +  keyword + "%' ";
		List list=getList(strSql);
		return list; //返回数据集
	}
	
	/**
	 * 得到所有符合条件未处理订单对象
	 */
	public List getMatchOrders(String keyword) throws HibernateException {
//		创建一个查询语句,按关键字查询
		String strSql = "from Order where tag =0 and orderno like '%"  +  keyword + "%' ";
		List list=getList(strSql);
		return list; //返回数据集
	}
	
	/**
	 * 得到所有符合条件已处理订单对象
	 */
	public List getMatchProcessOrders(String keyword) throws HibernateException {
//		创建一个查询语句,按关键字查询
		String strSql = "from Order where tag=1 and orderno like '%"  +  keyword + "%' ";
		List list=getList(strSql);
 		return list; //返回数据集
	}
	/**
	 * �õ����з�����������
	 */
	public List getMatchSorts(String keyword) throws HibernateException {
//		创建一个查询语句,按关键字查询
		String strSql = "from Sort where name like '%"  +  keyword + "%' ";
		List list=getList(strSql);
		return list; //返回数据集
	}
	/**
	 * 得到所有符合条件会员对象
	 */
	public List getMatchMembers(String keyword) throws HibernateException {
//		创建一个查询语句,按关键字查询
		String strSql = "from Member where username like '%"  +  keyword + "%' ";
		List list=getList(strSql);
		return list; //返回数据集
	}
	/**
	 *得到所有符合条件对象
	 */
	public List getList(String strSql) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();
		List list=null;
		try {
			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 Order  getOrder(int orderId) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		Order  order = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
//			创建查询对象
			Query query = session.createQuery("from Order where id="+orderId);
			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 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();
	}
	/**
	 * 插入或修改实体对象所对应的记录
	 */
	public void saveOrUpdate(Object obj) throws HibernateException {
		Session session = HibernateUtil.currentSession();
		if (obj != null) {
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				session.saveOrUpdate(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 + -