📄 productdao.java
字号:
package crm.dao.other;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import crm.entity.other.ProductEntity;
/**
* Data access object (DAO) for domain model class ProductEntity.
*
* @see crm.entity.other.ProductEntity
* @author MyEclipse Persistence Tools
*/
public class ProductDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(ProductDAO.class);
// property constants
public static final String PROD_NAME = "prodName";
public static final String PROD_TYPE = "prodType";
public static final String PROD_BATCH = "prodBatch";
public static final String PROD_UNIT = "prodUnit";
public static final String PROD_PRICE = "prodPrice";
public static final String PROD_MEMO = "prodMemo";
protected void initDao() {
// do nothing
}
public void save(ProductEntity transientInstance) {
log.debug("saving ProductEntity instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(ProductEntity persistentInstance) {
log.debug("deleting ProductEntity instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public List findByname(String proName) {
DetachedCriteria criteria =DetachedCriteria.forClass(ProductEntity.class);
criteria.add(Restrictions.like("prodName", proName,MatchMode.ANYWHERE));
return getHibernateTemplate().findByCriteria(criteria);
}
/**
* 删除方法(根据id删除)
*/
public void delete(int id)
{
long lid=id; //将int型转换成long型
this.delete(this.findById(lid));
}
public ProductEntity findById(java.lang.Long id) {
log.debug("getting ProductEntity instance with id: " + id);
try {
ProductEntity instance = (ProductEntity) getHibernateTemplate()
.get("crm.entity.other.ProductEntity", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(ProductEntity instance) {
log.debug("finding ProductEntity 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 ProductEntity instance with property: "
+ propertyName + ", value: " + value);
try {
String queryString = "from ProductEntity 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 findByProdName(Object prodName) {
return findByProperty(PROD_NAME, prodName);
}
public List findByProdType(Object prodType) {
return findByProperty(PROD_TYPE, prodType);
}
public List findByProdBatch(Object prodBatch) {
return findByProperty(PROD_BATCH, prodBatch);
}
public List findByProdUnit(Object prodUnit) {
return findByProperty(PROD_UNIT, prodUnit);
}
public List findByProdPrice(Object prodPrice) {
return findByProperty(PROD_PRICE, prodPrice);
}
public List findByProdMemo(Object prodMemo) {
return findByProperty(PROD_MEMO, prodMemo);
}
public List findAll() {
log.debug("finding all ProductEntity instances");
try {
String queryString = "from ProductEntity";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public ProductEntity merge(ProductEntity detachedInstance) {
log.debug("merging ProductEntity instance");
try {
ProductEntity result = (ProductEntity) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(ProductEntity instance) {
log.debug("attaching dirty ProductEntity instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(ProductEntity instance) {
log.debug("attaching clean ProductEntity instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static ProductDAO getFromApplicationContext(
ApplicationContext ctx) {
return (ProductDAO) ctx.getBean("ProductEntityDAO");
}
public List search(ProductEntity instance)
{
Session session=this.getSession();
Criteria c=session.createCriteria(ProductEntity.class);
if(null!=instance)
{
if(instance.getProdName()!=null&&!instance.getProdName().equals(""))
{
c.add(Restrictions.like("prodName", instance.getProdName(),MatchMode.ANYWHERE));
}
if(instance.getProdType()!=null&&!instance.getProdType().equals(""))
{
c.add(Restrictions.like("prodType", instance.getProdType(),MatchMode.ANYWHERE));
}
if(!instance.getProdBatch().equals("")&&instance.getProdBatch()!=null)
{
c.add(Restrictions.like("prodBatch", instance.getProdBatch(),MatchMode.ANYWHERE));
}
}
c.addOrder(Order.asc("prodId"));
return c.list();
}
/**
* 分页
*/
public List fenye(Integer pagNow)
{
String HQL="from ProductEntity";
Query query=this.getSession().createQuery(HQL);
int pagesie=5;
query.setFirstResult((pagNow-1)*pagesie);
query.setMaxResults(pagesie);
return query.list();
}
public int count()
{
String hql="select count(de) from ProductEntity de ";
Query query=this.getSession().createQuery(hql);
int count=(Integer)query.uniqueResult();
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -