package example;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import org.books.dao.BaseDao;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.base.dao.IBaseDao;
@SuppressWarnings("unchecked")
/**
* T extends Serializable T其实就是一个占位符, 你也可以换成其它字母,但作为惯例 当我们需要一种类型时,我们都T来占位,表示Type ,
* 后面的extends Serializable 表示在实际应用过程中,替换T的类型必须实现Serializable接口. PK => Primary
* Key
*/
public class BaseDaoImpl<T extends Serializable, PK extends Serializable> extends HibernateDaoSupport implements IBaseDao<T, PK>{
/***************************************************************************
* 新增
**************************************************************************/
/* (non-Javadoc)
* @see org.books.dao.BaseDao#save(T)
*/
public Serializable save(T entity) {
return super.getHibernateTemplate().save(entity);
}
/***************************************************************************
* 更新
**************************************************************************/
/* (non-Javadoc)
* @see org.books.dao.BaseDao#update(T)
*/
public void update(T t) {
super.getHibernateTemplate().update(t);
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#update(java.lang.String, java.lang.Object[])
*/
public void update(final String hql, final Object[] params) {
// 使用回调接口完成操作
super.getHibernateTemplate().executeWithNativeSession(
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
System.out.println(hql);
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
query.executeUpdate();
return null;
}
});
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#update(java.lang.Class, java.lang.String, java.lang.Object, java.lang.String, java.lang.Object)
*/
public void update(Class<T> entityClass,String pkName,Object pkValue,String propName,Object propValue){
this.update(entityClass, pkName, pkValue, new String[]{propName}, new Object[]{propValue});
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#update(java.lang.Class, java.lang.String, java.lang.Object, java.lang.String[], java.lang.Object[])
*/
public void update(Class<T> entityClass,String pkName,Object pkValue,String[] propNames,Object[] propValues){
if (!(propNames!=null&&propValues!=null&&propNames.length==propValues.length&&propNames.length>0)) {
throw new RuntimeException("请确保提供的参数是正确的!属性名称的个数与属性值的个数必须一致!必须提供至少一个属性名称!");
}
String entityName = entityClass.getName();
if (entityName.lastIndexOf(".")!=-1) {
entityName = entityName.substring(entityName.lastIndexOf(".")+1);
}
String hql = "update "+entityName+" obj set ";
for (int i = 0; i < propValues.length; i++) {
hql += "obj."+propNames[i]+" =? and ";
}
if (hql.lastIndexOf("and")!=-1) {
hql = hql .substring(0,hql.lastIndexOf("and"));
}
hql += " where obj."+pkName+" = ?";
Object[] ps = new Object[propValues.length+1];
for (int i = 0; i < propValues.length; i++) {
ps[i] = propValues[i];
}
ps[ps.length-1] = pkValue;
this.update(hql,ps);
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#saveorupdate(T)
*/
public void saveorupdate(T entity) {
super.getHibernateTemplate().saveOrUpdate(entity);
}
/***************************************************************************
* 删除
**************************************************************************/
/* (non-Javadoc)
* @see org.books.dao.BaseDao#delete(T)
*/
public void delete(T entity) {
super.getHibernateTemplate().delete(entity);
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#deleteById(java.lang.Class, PK)
*/
public void deleteById(Class<T> entityClass, PK id) {
super.getHibernateTemplate().delete(this.findEnityById(entityClass, id));
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#deleteByProperty(java.lang.Class, java.lang.String, java.lang.Object)
*/
public void deleteByProperty(final Class<T> entityClass,final String propName,final Object propValue){
this.deleteByProperty(entityClass, new String[]{propName}, new Object[]{propValue});
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#deleteByProperty(java.lang.Class, java.lang.String[], java.lang.Object[])
*/
public void deleteByProperty(final Class<T> entityClass,final String[] propNames,final Object[] propValues){
if (!(propNames!=null&&propValues!=null&&propNames.length==propValues.length&&propNames.length>0)) {
throw new RuntimeException("请确保提供的参数是正确的!属性名称的个数与属性值的个数必须一致!必须提供至少一个属性名称!");
}
String entityName = entityClass.getName();
if (entityName.lastIndexOf(".")!=-1) {
entityName = entityName.substring(entityName.lastIndexOf(".")+1);
}
String hql = "delete from "+entityName+" obj where ";
for (int i = 0; i < propNames.length; i++) {
hql += " obj."+propNames[i]+" = ? and ";
}
if (hql.lastIndexOf("and")!=-1) {
hql = hql .substring(0,hql.lastIndexOf("and"));
}
this.update(hql, propValues);
}
/***************************************************************************
* 查询
**************************************************************************/
/* (non-Javadoc)
* @see org.books.dao.BaseDao#findEnityById(java.lang.Class, PK)
*/
public T findEnityById(Class<T> entityClass, PK id) {
return (T) super.getHibernateTemplate().get(entityClass, id);
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#findAll(java.lang.Class)
*/
public List<T> findAll(Class<T> entityClass) {
return super.getHibernateTemplate().loadAll(entityClass);
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#findAllAsc(java.lang.Class, java.lang.String)
*/
public List<T> findAllAsc(Class<T> entityClass,String orderProperty) {
return this.findAll_order(entityClass, orderProperty, "asc");
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#findAllDesc(java.lang.Class, java.lang.String)
*/
public List<T> findAllDesc(Class<T> entityClass,String orderProperty) {
return this.findAll_order(entityClass, orderProperty, "desc");
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#findAll_order(java.lang.Class, java.lang.String, java.lang.String)
*/
public List<T> findAll_order(Class<T> entityClass,String orderProperty,String orderType) {
String hql = "from "+entityClass.getName()+" obj order by obj."+orderProperty+" "+orderType;
return this.findAllByHQL(hql);
}
/*
* 自定义排序 查询所有实体集合
*
* @param entityClass
* 实体类型信息
* @return 查询到的实体对象集合
*/
/* (non-Javadoc)
* @see org.books.dao.BaseDao#findAll_order(java.lang.Class, java.lang.String)
*/
public List<T> findAll_order(Class<T> entityClass,String orderDesc) {
String hql = "from "+entityClass.getName()+" obj order by "+orderDesc;
return this.findAllByHQL(hql);
}
/* (non-Javadoc)
* @see org.books.dao.BaseDao#findAllByProperty(java.lang.Class, java.lang.String, java.lang.Object)
*/
public List<T> fin