import java.io.Serializable;
import java.util.List;
/**
* 基础数据库操作类
*
* @author Frank
*
*/
public interface BaseDAO<T>
{
/**
* 保存一个对象
*
* @param o
* @return the generated identifier or null
*/
public Serializable save(T o);
/**
* 删除一个对象
*
* @param 是否成功
*/
public boolean delete(T o);
/**
* 更新一个对象
*
* @param 是否成功
*/
public boolean update(T o);
/**
* 保存或更新对象
*
* @param 是否成功
*/
public boolean saveOrUpdate(T o);
/**
* 查询
*
* @param hql
* @return the result list or null
*/
public List<T> find(String hql);
/**
* 查询集合
*
* @param hql
* @param param
* @return the result list or null
*/
public List<T> find(String hql, Object[] param);
/**
* 查询集合
*
* @param hql
* @param param
* @return the result list or null
*/
public List<T> find(String hql, List<Object> param);
/**
* 查询集合(带分页)
*
* @param hql
* @param param
* @param page
* 查询第几页
* @param rows
* 每页显示几条记录
* @return the result list or null
*/
public List<T> find(String hql, Object[] param, Integer page, Integer rows);
/**
* 查询集合(带分页)
*
* @param hql
* @param param
* @param page
* @param rows
* @return the result list or null
*/
public List<T> find(String hql, List<Object> param, Integer page, Integer rows);
/**
* 获得一个对象
*
* @param c
* 对象类型
* @param id
* @return Object or null
*/
public T get(Class<T> c, Serializable id);
/**
* 获得一个对象
*
* @param hql
* @param param
* @return Object or null
*/
public T get(String hql, Object[] param);
/**
* 获得一个对象
*
* @param hql
* @param param
* @return Object or null
*/
public T get(String hql, List<Object> param);
/**
* select count(*) from 类
*
* @param hql
* @return return a single instance that matches
* the query, or null if the query returns no results.
*/
public Long count(String hql);
/**
* "select count(*) from 类 where 字段 = ?", new Object[]
{ 字段的值 }
*
* @param hql
* @param param
* @return return a single instance that matches
* the query, or null if the query returns no results.
*/
public Long count(String hql, Object[] param);
/**
* List<Object> objects = new ArrayList<Object>();
objects.add("男");
* "select count(*) from Customer where sex = ?", objects
*
* @param hql
* @param param
* @return return a single instance that matches
* the query, or null if the query returns no results.
*/
public Long count(String hql, List<Object> param);
/**
* 执行HQL语句
*
* @param hql
* @return The number of entities updated or deleted
*/
public Integer executeHql(String hql);
/**
* 执行HQL语句
*
* @param hql
* @param param
* @return The number of entities updated or deleted
*/
public Integer executeHql(String hql, Object[] param);
/**
* 执行HQL语句
*
* @param hql
* @param param
* @return The number of entities updated or deleted
*/
public Integer executeHql(String hql, List<Object> param);
}
import java.io.Serializable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.doshr.xmen.server.dao.BaseDAO;
@SuppressWarnings("all")
public class BaseDAOImpl<T> implements BaseDAO<T>
{
private static final Log log = LogFactory.getLog(BaseDAOImpl.class);// 获取log实例
private SessionFactory sessionFactory;
private HibernateTemplate hibernateTemplate;
public SessionFactory getSessionFactory()
{
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public HibernateTemplate getHibernateTemplate()
{
return hibernateTemplate;
}
@Autowired
public void setHibernateTemplate(HibernateTemplate hibernateTemplate)
{
this.hibernateTemplate = hibernateTemplate;
}
private Session getCurrentSession()
{
return sessionFactory.getCurrentSession();
}
public Serializable save(T o)
{
// 1.参数检查
if (o != null && hibernateTemplate != null)
{
// 2. 保存
try
{
return hibernateTemplate.save(o);
}
catch (Exception e)
{
log.error("save" + e);
}
}
return null;
}
public boolean delete(T o)
{
boolean flag = false;
// 1.参数检查
if (o != null && hibernateTemplate != null)
{
// 2. 删除
try
{
hibernateTemplate.delete(o);
flag = true;
}
catch (Exception e)
{
log.error("delete" + e);
flag = false;
}
}
return flag;
}
public boolean update(T o)
{
boolean flag = false;
// 1.参数检查
if (o != null && hibernateTemplate != null)
{
// 2. 更新
try
{
hibernateTemplate.update(o);
flag = true;
}
catch (Exception e)
{
log.error("update" + e);
flag = false;
}
}
return flag;
}
public boolean saveOrUpdate(T o)
{
boolean flag = false;
// 1.参数检查
if (o != null && hibernateTemplate != null)
{
// 2. 保存or更新
try
{
hibernateTemplate.saveOrUpdate(o);
flag = true;
}
catch (Exception e)
{
log.error("savaOrUpdate" + e);
flag = false;
}
}
return flag;
}
public List<T> find(String hql)
{
// 1.非空验证
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
// 2.查找并返回集合
return query.list();
}
}
// 3.返回null
return null;
}
public List<T> find(String hql, Object[] param)
{
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
if (param != null && param.length > 0)
{
for (int i = 0; i < param.length; i++)
{
query.setParameter(i, param[i]);
}
}
return query.list();
}
}
return null;
}
public List<T> find(String hql, List<Object> param)
{
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
if (param != null && param.size() > 0)
{
for (int i = 0; i < param.size(); i++)
{
query.setParameter(i, param.get(i));
}
}
return query.list();
}
}
return null;
}
public List<T> find(String hql, Object[] param, Integer page, Integer rows)
{
if (page == null || page < 1)
{
page = 1;
}
if (rows == null || rows < 1)
{
rows = 10;
}
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
if (param != null && param.length > 0)
{
for (int i = 0; i < param.length; i++)
{
query.setParameter(i, param[i]);
}
}
return query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
}
return null;
}
public List<T> find(String hql, List<Object> param, Integer page, Integer rows)
{
if (page == null || page < 1)
{
page = 1;
}
if (rows == null || rows < 1)
{
rows = 10;
}
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
if (param != null && param.size() > 0)
{
for (int i = 0; i < param.size(); i++)
{
query.setParameter(i, param.get(i));
}
}
return query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
}
return null;
}
public T get(Class<T> c, Serializable id)
{
if (this.getCurrentSession() != null && c != null && id != null)
{
return (T) this.getCurrentSession().get(c, id);
}
return null;
}
public T get(String hql, Object[] param)
{
List<T> l = this.find(hql, param);
if (l != null && l.size() > 0)
{
return l.get(0);
}
else
{
return null;
}
}
public T get(String hql, List<Object> param)
{
List<T> l = this.find(hql, param);
if (l != null && l.size() > 0)
{
return l.get(0);
}
else
{
return null;
}
}
public Long count(String hql)
{
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
return (Long) query.uniqueResult();
}
}
return null;
}
public Long count(String hql, Object[] param)
{
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
if (param != null && param.length > 0)
{
for (int i = 0; i < param.length; i++)
{
query.setParameter(i, param[i]);
}
return (Long) query.uniqueResult();
}
}
}
return null;
}
public Long count(String hql, List<Object> param)
{
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
if (param != null && param.size() > 0)
{
for (int i = 0; i < param.size(); i++)
{
query.setParameter(i, param.get(i));
}
}
return (Long) query.uniqueResult();
}
}
return null;
}
public Integer executeHql(String hql)
{
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
return query.executeUpdate();
}
}
return 0;
}
public Integer executeHql(String hql, Object[] param)
{
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
if (param != null && param.length > 0)
{
for (int i = 0; i < param.length; i++)
{
query.setParameter(i, param[i]);
}
}
return query.executeUpdate();
}
}
return 0;
}
public Integer executeHql(String hql, List<Object> param)
{
if (hql != null && this.getCurrentSession() != null)
{
Query query = this.getCurrentSession().createQuery(hql);
if (query != null)
{
if (param != null && param.size() > 0)
{
for (int i = 0; i < param.size(); i++)
{
query.setParameter(i, param.get(i));
}
}
return query.executeUpdate();
}
}
return 0;
}
}
原文:http://blog.csdn.net/coslay/article/details/43191803