package com.adr.common.dao;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* @author youan
*
*/
/**
* @author youan
*
*/
/**
* @author youan
*
*/
public class BaseDAO extends HibernateDaoSupport implements DAO {
protected final Log log = LogFactory.getLog(getClass());
public Object getObject(Class clazz, Serializable id) {
// 实现返回与此类对应主键为id的所有值
Object o = getHibernateTemplate().get(clazz, id);
if (o == null) {
throw new ObjectRetrievalFailureException(clazz, id);
}
return o;
}
// 取得某个类的所有的记录的List集合
public List getAll(Class clazz) throws DataAccessException {
return getHibernateTemplate().loadAll(clazz);
}
/*
* (non-Javadoc)
*
* @see com.isoftstone.common.dao.DAO#load(java.lang.Class,
* java.io.Serializable)
*/
public Object load(Class clazz, Serializable id) throws DataAccessException {
Object o = getHibernateTemplate().load(clazz, id);
if (o == null) {
throw new ObjectRetrievalFailureException(clazz, id);
}
return o;
}
public void saveOrUpdate(Object o) throws DataAccessException {
getHibernateTemplate().saveOrUpdate(o);
}
public void remove(Object clazz) throws DataAccessException {
getHibernateTemplate().delete(clazz);
}
/**
* 实现根据数据库中与此类对应表的主键(id)删除此对象
*
* @param clazz
* @param id
* @throws DataAccessException
*/
public void removeObject(Class clazz, Serializable id)
throws DataAccessException {
getHibernateTemplate().delete(getObject(clazz, id));
}
/**
* 用于复杂的查询语句 我们在代入查询参数的时候, 建议使用 name里面放的是HQL语句 eg: "from Courses where
* name=:name" map里面放的是你想绑定的参数实现集 eg: Map map = new HashMap();
* map.put("name", "leon"); 最后得到的结果就是 from Courses where name="leon"
*/
public java.util.List getNamedQuery(final String name, final Object[] params)
throws DataAccessException {
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {
Query q = s.createQuery(name);
setParameterValue(q, params);
List list = q.list();
return list;
}
};
return (List) this.getHibernateTemplate().execute(hibernateCallback);
}
/**
* 新方法
* @param name
* @param ss
* @return
* @throws DataAccessException
*/
public java.util.List getHQL(final String name, final String ss)
throws DataAccessException {
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {
List list = s.createQuery(name).setString(0,ss).list();
return list;
}
};
return (List) this.getHibernateTemplate().execute(hibernateCallback);
}
// 自动匹配参数类型
protected void setParameterValue(Query query, Object[] params) {
if (null == params || params.length == 0) {
return;
}
for (int key = 0; key < params.length; key++) {
Object value = params[key];
if (value instanceof Boolean) {
query.setBoolean(key, ((Boolean) value).booleanValue());
} else if (value instanceof String) {
query.setString(key, (String) value);
} else if (value instanceof Integer) {
query.setInteger(key, ((Integer) value).intValue());
} else if (value instanceof Long) {
query.setLong(key, ((Long) value).longValue());
} else if (value instanceof Float) {
query.setFloat(key, ((Float) value).floatValue());
} else if (value instanceof Double) {
query.setDouble(key, ((Double) value).doubleValue());
} else if (value instanceof BigDecimal) {
query.setBigDecimal(key, (BigDecimal) value);
} else if (value instanceof Byte) {
query.setByte(key, ((Byte) value).byteValue());
} else if (value instanceof Calendar) {
query.setCalendar(key, (Calendar) value);
} else if (value instanceof Character) {
query.setCharacter(key, ((Character) value).charValue());
} else if (value instanceof Timestamp) {
query.setTimestamp(key, (Timestamp) value);
} else if (value instanceof Date) {
query.setDate(key, (Date) value);
} else if (value instanceof Short) {
query.setShort(key, ((Short) value).shortValue());
}
}
}
public List getQueryResult(final String hql, final Object[] values,
final BaseQEntity qEntity) throws DataAccessException {
this.log
.info("分页显示:["
+ qEntity.isShowPages()
+ "] 每页["
+ qEntity.getPerPageRows()
+ "]条记录,从第["
+ ((qEntity.getCurrentPage() - 1)
* qEntity.getPerPageRows() + 1) + "]条记录开始,显示第["
+ qEntity.getCurrentPage() + "]页...");
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {
String q = hql;
Query query = s.createQuery(q);
// 调用此方法向query对象中绑定查询参数
setParameterValue(query, values);
// 分页控制 (根据当前的页数来得到要现在的记录集)
if (qEntity.isShowPages()) {
query.setFirstResult((qEntity.getCurrentPage() - 1)
* qEntity.getPerPageRows());
query.setMaxResults(qEntity.getPerPageRows());
}
List list = query.list();
return list;
}
};
return this.getHibernateTemplate().executeFind(hibernateCallback);
}
// 根据你传入的hql语句得到记录集的记录条数
public Integer getQueryCount(final String hql, final Object[] values)
throws DataAccessException {
HibernateCallback hibernateCallback = new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {
String q = hql;
Query query = s.createQuery(q);
// 设置参数
setParameterValue(query, values);
Integer queryCount = (Integer) query.uniqueResult();
return queryCount;
}
};
return (Integer) this.getHibernateTemplate().execute(hibernateCallback);
}
}