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

📄 categorydao.java

📁 基于struts+hibernate的电子商务网站。可运行。数据库mysql
💻 JAVA
字号:
package tarena.dao;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import tarena.data.AbractCategory;
import tarena.data.CategorySecondAndThird;
import tarena.entity.Category;

/**
 * Data access object (DAO) for domain model class Category.
 * 
 * @see tarena.entity.Category
 * @author MyEclipse Persistence Tools
 */

public class CategoryDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(CategoryDAO.class);

	// property constants
	public static final String CNAME = "cname";

	public static final String PARENTID = "parentid";

	public static final String DESCRIPTION = "description";

	public static final String PHOTO = "photo";

	public static final String CTYPE = "ctype";
	
	private Session session = null;
	private Transaction transaction = null;
	
	public CategoryDAO() {
		session = getSession();
		transaction = session.beginTransaction();
	}

	/**
	 * 列出分类
	 * @param type -指定查询的分类类型 0:全部分类 1:查询1级分类
	 * @param id -指定查询的类型的ID
	 * @param listNum 是否类出3级分类下的商品数
	 * @return Vector<AbractCategory>
	 * @roseuid 49052A800109
	 */
	public Vector<AbractCategory> ListCategory(int type, int id,boolean listNum) {
		Vector<AbractCategory> categorys = new Vector<AbractCategory>();
		if(type==0)
			getCategory(type, categorys,true,listNum);
		else
			getCategory(id, categorys,true,listNum);
		return categorys;
	}

	/**
	 * 获取某个分类下的所有子分类。
	 * @param pid 父类id
	 * @param categorys 将分类添加到Vector的categroys
	 * @param isCursion 是否查询下级分类
	 * @param listNum 是否列出3级分类下的商品数
	 */
	private void getCategory(int pid,Vector<AbractCategory> categorys,boolean isCursion,boolean listNum){
		String hql = "select c.id,c.cname,c.ctype from Category as c";
		hql += " where c.parentid="+pid;
		Query query = session.createQuery(hql);
		if(query!=null){
			Iterator iterator = query.iterate();
			while(iterator!=null && iterator.hasNext()){
				Object[] obj = (Object[])iterator.next();
				AbractCategory category = new AbractCategory();
				Integer id = (Integer)obj[0];
				Short type = (Short)obj[2];
				category.setId(id);
				category.setName(obj[1].toString());
				category.setType(type);
				if(listNum && type==3)
					category.setProductnum(ListCategoryProductNum(id));
				categorys.add(category);
				if(isCursion && type<=3)
					getCategory(id, categorys,isCursion,listNum);
			}
		}
	}
	
	/**
	 * 列出3级分类下的商品数。
	 * @param id 3级分类id
	 * @return 返回商品数
	 */
	public int ListCategoryProductNum(Integer id){
		String hql="select c.products from Category c where c.id="+id;
		Query query = getSession().createQuery(hql);
		if(query!=null && query.list()!=null && query.list().size()>0){
			Set a = (Set)query.list().get(0);
			return a.size();
		}
		return 0;
			
	}
	
	/**
	 * 列出顶级目录,及显示目录的id,及类型
	 * @return Vector<AbractCategory>
	 * @roseuid 49055EB9007D
	 */
	public Vector<AbractCategory> ListTopCategory() {
		Vector<AbractCategory> topCategorys = new Vector<AbractCategory>();
		getCategory(0, topCategorys, false,false);
		return topCategorys;
	}

	/**
	 * 按品牌列出分类
	 * 
	 * @return Vector<AbractCategory>
	 * @roseuid 4907CECC034B
	 */
	public List<CategorySecondAndThird> ListCategoryByBrand() {
		return null;
	}
	
	/**
	 * 检测某类型的分类id是否存在
	 * @param categoryid 分类id
	 * @param type 该分类的类型
	 * @return 返回存在否。
	 */
	public boolean isExit(Integer categoryid,Integer type){
		StringBuffer hql = new StringBuffer();
		hql.append("select c.id from Category c where c.id=? and c.ctype=? ");
		List list = getSession()
		 					.createQuery(hql.toString())
							.setParameter(0, categoryid)
							.setParameter(1, type.shortValue())
							.list();
		return list!=null && list.size()>0;
	}
	
	public Integer getParentID(Integer id){
		StringBuffer hql = new StringBuffer();
		hql.append("select c.parentid from Category c where c.id=?");
		List list = getSession()
								.createQuery(hql.toString())
								.setParameter(0, id)
								.list();
		if(list!=null && list.size()>0)
			return (Integer)list.get(0);
		else
			return 0;
	}
	
	public void save(Category transientInstance) {
		log.debug("saving Category instance");
		try {
			transaction.begin();
			session.save(transientInstance);
			transaction.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Category persistentInstance) {
		log.debug("deleting Category instance");
		try {
			session.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Category findById(java.lang.Integer id) {
		log.debug("getting Category instance with id: " + id);
		try {
			Category instance = (Category) session.get(
					"tarena.entity.Category", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Category instance) {
		log.debug("finding Category instance by example");
		try {
			List results = session
					.createCriteria("tarena.entity.Category").add(
							Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Category instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Category as model where model."
					+ propertyName + "= ?";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCname(Object cname) {
		return findByProperty(CNAME, cname);
	}

	public List findByParentid(Object parentid) {
		return findByProperty(PARENTID, parentid);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findByPhoto(Object photo) {
		return findByProperty(PHOTO, photo);
	}

	public List findByCtype(Object ctype) {
		return findByProperty(CTYPE, ctype);
	}

	public List findAll() {
		log.debug("finding all Category instances");
		try {
			String queryString = "from Category";
			Query queryObject = session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Category merge(Category detachedInstance) {
		log.debug("merging Category instance");
		try {
			Category result = (Category) session.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Category instance) {
		log.debug("attaching dirty Category instance");
		try {
			session.saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Category instance) {
		log.debug("attaching clean Category instance");
		try {
			session.lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}

⌨️ 快捷键说明

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