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

📄 productdao.java

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

import java.util.List;

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.AbractProduct;
import tarena.entity.Product;

/**
 * Data access object (DAO) for domain model class Product.
 * 
 * @see tarena.entity.Product
 * @author MyEclipse Persistence Tools
 */
@SuppressWarnings("unchecked")
public class ProductDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ProductDAO.class);

	// property constants
	public static final String PNAME = "pname";

	public static final String RECOMMEND = "recommend";

	public static final String PTYPE = "ptype";

	public static final String PRICE = "price";

	public static final String PRISENTATION = "prisentation";

	public static final String PROMOTION = "promotion";

	public static final String PROMOTIONALPRICE = "promotionalprice";

	public static final String ALLOWITEM = "allowitem";

	public static final String WARRANTY = "warranty";

	public static final String REMARK = "remark";

	public static final String SUMMARY = "summary";

	public static final String NORM = "norm";

	public static final String SALEPOPLUARITY = "salepopluarity";

	public static final String VIEWPOPLUARITY = "viewpopluarity";

	public static final String TOTALSCORE = "totalscore";

	public static final String SHOWSCORE = "showscore";

	public static final String PRICESCORE = "pricescore";

	public static final String PERFORMANCESCORE = "performancescore";

	public static final String MARKUSER = "markuser";

	public static final String COLLECTUSER = "collectuser";
	
	private Session session = null;
	private Transaction transaction = null;
	
	public ProductDAO() {
		session = getSession();
		transaction = session.beginTransaction();
	}
	
	/**
	 * 列出是否存在该商品
	 * @param id
	 * @return
	 */
	public boolean isExit(Integer id){
		StringBuffer hql = new StringBuffer();
		hql.append("select p.id from Product p where p.id=?");
		List list = getSession()
		 					.createQuery(hql.toString())
							.setParameter(0, id)
							.list();
		return list!=null && list.size()>0;
	}
	
	/**
	 * 列出促销商品。
	 * @return
	 */
	public List<Product> listPromotionProduct(){
		return getSession()
				.createQuery("from Product p where p.promotion=1 ")
				.list();
	}
	
	/**
	 * 
	 * @param id 商品id
	 * @return 返回AbractProduct类型商品
	 */
	public AbractProduct listProductByID(Integer id){
		if(isExit(id)){
			StringBuffer hql = new StringBuffer();
			hql.append("select new tarena.data.AbractProduct(p.id,p.pname,p.price) from Product p ");
			hql.append("where p.id=?");
			return (AbractProduct)getSession()
					.createQuery(hql.toString())
					.setParameter(0, id)
					.list().get(0);
		}else
			return null;
	}
	
	public void save(Product transientInstance) {
		log.debug("saving Product 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(Product persistentInstance) {
		log.debug("deleting Product instance");
		try {
			session.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

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

	public List findByExample(Product instance) {
		log.debug("finding Product instance by example");
		try {
			List results = session.createCriteria("tarena.entity.Product")
					.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 Product instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Product 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 findByPname(Object pname) {
		return findByProperty(PNAME, pname);
	}

	public List findByRecommend(Object recommend) {
		return findByProperty(RECOMMEND, recommend);
	}

	public List findByPtype(Object ptype) {
		return findByProperty(PTYPE, ptype);
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findByPrisentation(Object prisentation) {
		return findByProperty(PRISENTATION, prisentation);
	}

	public List findByPromotion(Object promotion) {
		return findByProperty(PROMOTION, promotion);
	}

	public List findByPromotionalprice(Object promotionalprice) {
		return findByProperty(PROMOTIONALPRICE, promotionalprice);
	}

	public List findByAllowitem(Object allowitem) {
		return findByProperty(ALLOWITEM, allowitem);
	}

	public List findByWarranty(Object warranty) {
		return findByProperty(WARRANTY, warranty);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findBySummary(Object summary) {
		return findByProperty(SUMMARY, summary);
	}

	public List findByNorm(Object norm) {
		return findByProperty(NORM, norm);
	}

	public List findBySalepopluarity(Object salepopluarity) {
		return findByProperty(SALEPOPLUARITY, salepopluarity);
	}

	public List findByViewpopluarity(Object viewpopluarity) {
		return findByProperty(VIEWPOPLUARITY, viewpopluarity);
	}

	public List findByTotalscore(Object totalscore) {
		return findByProperty(TOTALSCORE, totalscore);
	}

	public List findByShowscore(Object showscore) {
		return findByProperty(SHOWSCORE, showscore);
	}

	public List findByPricescore(Object pricescore) {
		return findByProperty(PRICESCORE, pricescore);
	}

	public List findByPerformancescore(Object performancescore) {
		return findByProperty(PERFORMANCESCORE, performancescore);
	}

	public List findByMarkuser(Object markuser) {
		return findByProperty(MARKUSER, markuser);
	}

	public List findByCollectuser(Object collectuser) {
		return findByProperty(COLLECTUSER, collectuser);
	}

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

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

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

	public void attachClean(Product instance) {
		log.debug("attaching clean Product 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 + -