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

📄 itemdaohibernate.java

📁 是一个基于java开发的拍卖系统 非常适合初学者看
💻 JAVA
字号:
package com.wish.jhk.dao.impl;

import java.util.List;

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

import com.wish.jhk.dao.ItemDao;
import com.wish.jhk.entity.Item;
import com.wish.jhk.util.HibernateSessionFactory;

public class ItemDaoHibernate implements ItemDao {

	// 删除物品 id 需要删除的物品id
	public void delete(Integer id) {
		Session session = null;
		Transaction trans = null;

		try {
			session = HibernateSessionFactory.getSession();
			trans = session.beginTransaction();
			session.delete(session.get(Item.class, id));
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			session.close();
		}

	}

	// 删除物品 item 需要删除的物品
	public void delete(Item item) {
		Session session = null;
		Transaction trans = null;

		try {
			session = HibernateSessionFactory.getSession();
			trans = session.beginTransaction();
			session.delete(item);
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			session.close();
		}

	}

	// 根据产品分类,获取当前拍卖的全部商品
	// kindId 种类id;
	// return 该类的全部产品

	@SuppressWarnings("unchecked")
	public List<Item> findItemByKind(Integer kindId) {
		Session session = null;
		Transaction trans = null;
		List<Item> items = null;
		try {
			session = HibernateSessionFactory.getSession();
			trans = session.beginTransaction();
			String hql = "from Item as item where item.kind.kindId = :kindId and item.state.stateId = 1";
			Query query = session.createQuery(hql);
			items = query.setInteger("kindId", kindId).list();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			session.close();
		}
		return items;
	}

	// 根据所有者查找拍卖中的物品
	// useId 所有者Id;
	// return 该人拍卖中的全部物品

	@SuppressWarnings("unchecked")
	public List<Item> findItemByOwner(Integer userId) {
		Session session = null;
		Transaction trans = null;
		List<Item> items = null;
		try {
			session = HibernateSessionFactory.getSession();
			trans = session.beginTransaction();
			String hql = "from Item as item where item.userByOwnerId.userId = :userId and item.state.stateId = 1";
			Query query = session.createQuery(hql);
			items = query.setInteger("userId", userId).list();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			session.close();
		}
		return items;
	}

	// 根据物品状态查找物品
	// stateId 状态Id;
	// return 该状态下的全部物品
	//    
	@SuppressWarnings("unchecked")
	public List<Item> findItemByState(Integer stateId) {
		Session session = null;
		Transaction trans = null;
		List<Item> items = null;
		try {
			//System.out.println("掉用");
			session = HibernateSessionFactory.getSession();
			trans = session.beginTransaction();
			String hql = "from Item as item where item.state.stateId = :stateId";
			Query query = session.createQuery(hql);
			items = query.setInteger("stateId", stateId).list();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			session.close();
		}
		return items;
	}

	// 根据赢取者查找物品
	// userId 赢取者Id;
	// return 该人赢取的全部物品

	@SuppressWarnings("unchecked")
	public List<Item> findItemByWiner(Integer userId) {
		Session session = null;
		Transaction trans = null;
		List<Item> items = null;
		try {
			session = HibernateSessionFactory.getSession();
			trans = session.beginTransaction();
			String hql = "from Item as item where item.userByWinerId.userId = :userId and item.state.stateId = 2";
			Query query = session.createQuery(hql);
			items = query.setInteger("userId", userId).list();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			session.close();
		}
		return items;
	}

	// 根据主键查找物品
	// itemId 物品id;
	// id对应的物品

	public Item get(Integer itemId) {
		Session session = null;
		Transaction trans = null;
		Item item = null;
		try {
			session = HibernateSessionFactory.getSession();
			trans = session.beginTransaction();
			item = (Item) session.get(Item.class, itemId);
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			session.close();
		}
		return item;
	}

	// 保存物品 item 需要保存的物品
	public void save(Item item) {
		Session session = null;
		Transaction trans = null;

		try {
			session = HibernateSessionFactory.getSession();
			trans = session.beginTransaction();
			session.save(item);
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			session.close();
		}

	}

	// 删除物品 item 需要删除的物品
	public void update(Item item) {
		Session session = null;
		Transaction trans = null;

		try {
			session = HibernateSessionFactory.getSession();
			trans = session.beginTransaction();
			session.update(item);
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			trans.rollback();
		} finally {
			session.close();
		}

	}

	// public static void main(String args[]) {
	// ItemDaoHibernate i = new ItemDaoHibernate();
	// List<Item> items = i.findItemByState(3);
	// System.out.println(items.size());
	// for(Item its : items){
	// System.out.println(its.getItemId());
	// }
	// if (i.get(1) == null) {
	// System.out.println("null");
	// }
	// }

}

⌨️ 快捷键说明

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