package com.house.dao.impl;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.house.util.Constants;
public class BaseDao<T> extends HibernateDaoSupport {
// 添加单对象
public int save(T entity) {
int row = 0;
try {
super.getHibernateTemplate().save(entity);
row = 1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
// 修改单对象
public int update(T entity) {
int row = 0;
try {
super.getHibernateTemplate().clear();
super.getHibernateTemplate().update(entity);
row = 1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
// 删除单对象
public int delete(Class cls, Serializable id) {
int row = 0;
T o = (T) this.getHibernateTemplate().get(cls, id);
try {
super.getHibernateTemplate().delete(o);
row = 1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
// 根据ID(主键)获取单个对象
public T get(Class cls, Serializable id) {
return (T) getHibernateTemplate().get(cls, id);
}
// 查询获取对象集合,hql查询语句
public List getlistbyhql(String hql) {
List list = new ArrayList();
list = getHibernateTemplate().find(hql);
return list;
}
public List getlistbyhql(final String hql, final Object[] parms) {
List list = new ArrayList();
list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (parms != null) {
for (int i = 0; i < parms.length; i++) {
query.setParameter(i, parms[i]);
}
}
return query.list();
}
});
return list;
}
// 查询数据量
public Object getuniqbyhql(final String hql) {
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
return query.uniqueResult();
}
});
return o;
}
public Object getuniqbyhql(final String hql, final Object[] parms) {
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (parms != null) {
for (int i = 0; i < parms.length; i++) {
query.setParameter(i, parms[i]);
}
}
return query.uniqueResult();
}
});
return o;
}
public Long getRecordCount(final String hql) {
return getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Long recordCount = (Long) session.createQuery(hql).uniqueResult();
return recordCount;
}
});
}
public Long getRecordCount( final String hql,final Object[] prams) {
Long recordCount=0l;
recordCount = (Long) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (prams != null) {
for (int i = 0; i < prams.length; i++) {
query.setParameter(i, prams[i]);
}
}
Long recordCount = (Long) query.uniqueResult();
return recordCount;
}
});
return recordCount;
}
// 执行批量更新(修改删除)
public int exeupdatehql(final String hql) {
int row = 0;
row = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
int row = query.executeUpdate();
return row;
}
});
return row;
}
public int exeupdatehql(final String hql, final Object[] prams) {
int row = 0;
row = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (prams != null) {
for (int i = 0; i < prams.length; i++) {
query.setParameter(i, prams[i]);
}
}
int row = query.executeUpdate();
return row;
}
});
return row;
}
/*
* 分页方法,参数pagesize表示每页显示数据量,startpage表示数据库从指定开始数据读取到指定位置,
*
* 例:int startpage=(当前页数-1) * pagesize;
*
* 当前页数为1,pagesize为3,读取数据库0到2数据
*
* 当前页数为2,pagesize为3,读取数据库3到5数据
*/
public List pageList(final String hql, final int pagesize,
final int pageNo) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult((pageNo-1)*Constants.MaxResult);
query.setMaxResults(pagesize);
List list = query.list();
return list;
}
});
}
public List pageList(final String hql, final int pagesize,
final int pageNo, final Object[] parms) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (parms != null) {
for (int i = 0; i < parms.length; i++) {
query.setParameter(i, parms[i]);
}
}
query.setFirstResult((pageNo-1)*Constants.MaxResult);
query.setMaxResults(pagesize);
List list = query.list();
return list;
}
});
}
}
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.house.util.Constants;
public class BaseDao<T> extends HibernateDaoSupport {
// 添加单对象
public int save(T entity) {
int row = 0;
try {
super.getHibernateTemplate().save(entity);
row = 1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
// 修改单对象
public int update(T entity) {
int row = 0;
try {
super.getHibernateTemplate().clear();
super.getHibernateTemplate().update(entity);
row = 1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
// 删除单对象
public int delete(Class cls, Serializable id) {
int row = 0;
T o = (T) this.getHibernateTemplate().get(cls, id);
try {
super.getHibernateTemplate().delete(o);
row = 1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
// 根据ID(主键)获取单个对象
public T get(Class cls, Serializable id) {
return (T) getHibernateTemplate().get(cls, id);
}
// 查询获取对象集合,hql查询语句
public List getlistbyhql(String hql) {
List list = new ArrayList();
list = getHibernateTemplate().find(hql);
return list;
}
public List getlistbyhql(final String hql, final Object[] parms) {
List list = new ArrayList();
list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (parms != null) {
for (int i = 0; i < parms.length; i++) {
query.setParameter(i, parms[i]);
}
}
return query.list();
}
});
return list;
}
// 查询数据量
public Object getuniqbyhql(final String hql) {
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
return query.uniqueResult();
}
});
return o;
}
public Object getuniqbyhql(final String hql, final Object[] parms) {
Object o = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (parms != null) {
for (int i = 0; i < parms.length; i++) {
query.setParameter(i, parms[i]);
}
}
return query.uniqueResult();
}
});
return o;
}
public Long getRecordCount(final String hql) {
return getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Long recordCount = (Long) session.createQuery(hql).uniqueResult();
return recordCount;
}
});
}
public Long getRecordCount( final String hql,final Object[] prams) {
Long recordCount=0l;
recordCount = (Long) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (prams != null) {
for (int i = 0; i < prams.length; i++) {
query.setParameter(i, prams[i]);
}
}
Long recordCount = (Long) query.uniqueResult();
return recordCount;
}
});
return recordCount;
}
// 执行批量更新(修改删除)
public int exeupdatehql(final String hql) {
int row = 0;
row = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
int row = query.executeUpdate();
return row;
}
});
return row;
}
public int exeupdatehql(final String hql, final Object[] prams) {
int row = 0;
row = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (prams != null) {
for (int i = 0; i < prams.length; i++) {
query.setParameter(i, prams[i]);
}
}
int row = query.executeUpdate();
return row;
}
});
return row;
}
/*
* 分页方法,参数pagesize表示每页显示数据量,startpage表示数据库从指定开始数据读取到指定位置,
*
* 例:int startpage=(当前页数-1) * pagesize;
*
* 当前页数为1,pagesize为3,读取数据库0到2数据
*
* 当前页数为2,pagesize为3,读取数据库3到5数据
*/
public List pageList(final String hql, final int pagesize,
final int pageNo) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult((pageNo-1)*Constants.MaxResult);
query.setMaxResults(pagesize);
List list = query.list();
return list;
}
});
}
public List pageList(final String hql, final int pagesize,
final int pageNo, final Object[] parms) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (parms != null) {
for (int i = 0; i < parms.length; i++) {
query.setParameter(i, parms[i]);
}
}
query.setFirstResult((pageNo-1)*Constants.MaxResult);
query.setMaxResults(pagesize);
List list = query.list();
return list;
}
});
}
}