productinfodao.java

来自「一套自己原先在学校作的CRM,大家指点下」· Java 代码 · 共 197 行

JAVA
197
字号
package com.crm.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.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.crm.pojo.ProductInfo;

/**
 * Data access object (DAO) for domain model class ProductInfo.
 * 
 * @see com.crm.pojo.ProductInfo
 * @author MyEclipse Persistence Tools
 */

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

	// property constants
	public static final String PRODUCT_NAME = "productName";

	public static final String PRODUCT_MODEL = "productModel";

	public static final String PRODUCT_QUANTITY = "productQuantity";

	public static final String PRODUCT_UNIT = "productUnit";

	public static final String PRODUCT_REMARK = "productRemark";

	public static final String PRODUCT_SALE_QUANTITY = "productSaleQuantity";

	protected void initDao() {
		// do nothing
	}

	public void save(ProductInfo transientInstance) {
		log.debug("saving ProductInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

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

	public ProductInfo findById(java.lang.Integer id) {
		log.debug("getting ProductInfo instance with id: " + id);
		try {
			ProductInfo instance = (ProductInfo) getHibernateTemplate().get(
					"com.crm.pojo.ProductInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ProductInfo instance) {
		log.debug("finding ProductInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			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 ProductInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ProductInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByProductName(Object productName) {
		return findByProperty(PRODUCT_NAME, productName);
	}

	public List findByProductModel(Object productModel) {
		return findByProperty(PRODUCT_MODEL, productModel);
	}

	public List findByProductQuantity(Object productQuantity) {
		return findByProperty(PRODUCT_QUANTITY, productQuantity);
	}

	public List findByProductUnit(Object productUnit) {
		return findByProperty(PRODUCT_UNIT, productUnit);
	}

	public List findByProductRemark(Object productRemark) {
		return findByProperty(PRODUCT_REMARK, productRemark);
	}

	public List findByProductSaleQuantity(Object productSaleQuantity) {
		return findByProperty(PRODUCT_SALE_QUANTITY, productSaleQuantity);
	}

	public List findAll() {
		log.debug("finding all ProductInfo instances");
		try {
			String queryString = "from ProductInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

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

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

	public void attachClean(ProductInfo instance) {
		log.debug("attaching clean ProductInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public List find(ProductInfo p,int pageNo,int pageSize){
		String sql="from ProductInfo p where 1=1 ";
		Session s=this.getSessionFactory().openSession();
		if(!(p.getProductName()==null || p.getProductName().equals(""))){
			sql+=" and p.productName like '%"+p.getProductName()+"%'";
		}
		if(!(p.getProductModel()==null || p.getProductModel().equals(""))){
			sql+=" and p.productModel like '%"+p.getProductModel()+"%'";
		}
		if(!(p.getProductGradeJob()==null || p.getProductGradeJob().equals(""))){
			sql+=" and p.productGradeJob like '%"+p.getProductGradeJob()+"%'";
		}
		sql+=" order by p.productId asc";
		Query query=s.createQuery(sql);
		int fri=pageSize*(pageNo-1);
		query.setFirstResult(fri);
		query.setMaxResults(pageSize);
		List list=query.list();
		s.close();
		return list;
	}

	public static ProductInfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (ProductInfoDAO) ctx.getBean("ProductInfoDAO");
	}
}

⌨️ 快捷键说明

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