Hibernate实现DAO的常用CRUD操作
package com.utstar.nms.vmm.spring.dao;
import java.io.Serializable;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.utstar.nms.vmm.spring.util.GenericsUtils;
public class HibernateEntityDao extends HibernateDaoSupport
implements EntityDao {
protected Class entityClass;
public HibernateEntityDao() {
entityClass = GenericsUtils.getSuperClassGenricType(getClass());
}
/**
* find an object by its id
* @param id
* @return an object
*/
public T findByID(Serializable id) {
return (T) getHibernateTemplate().get(entityClass, id);
}
/**
* @return find all objects
*/
public List findAll()
{
return getHibernateTemplate().loadAll(entityClass);
}
/**
* find objects by sql(jdbc) or hql(hibernate)
* @param ql
* @param values parameter that
* @return an objects list of the query result
*/
public List findByQL(String hql, Object... values)
{
return getHibernateTemplate().find(hql, values);
}
/**
* save the object input
* @param o
*/||| public void save(Object object)
{
getHibernateTemplate().save(object);
}
/**
* update the object input
* @param o
*/
public void update(Object object)
{
getHibernateTemplate().update(object);
}
/**
* saveOrUpdate the object input
* @param o
*/
public void saveOrUpdate(Object object)
{
getHibernateTemplate().saveOrUpdate(object);
}
/**
* delete the object input
* @param o
*/
public void remove(Object object)
{
/*delete the object*/
getHibernateTemplate().delete(object);
}
/**
* delete an object by its id
* @param id
*/
public void removeById(Serializable id)
{
/*find the object by id*/
Object object=findByID(id);
/*delete the object*/
remove(object);
编辑推荐:
温馨提示:因考试政策、内容不断变化与调整,长理培训网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准! (责任编辑:长理培训)
点击加载更多评论>>