private HibernateTemplate hibernateTemplate = null;
private SessionFactory sessionFactory = null;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void save(Object obj) {
getHibernateTemplate().save(obj);
}
public void delete(Object obj) {
getHibernateTemplate().delete(obj);
}
public void update(Object obj) {
getHibernateTemplate().update(obj);
}
@SuppressWarnings("unchecked")
public Object get(Class clazz, int id) {
return getHibernateTemplate().get(clazz, id);
}
/**
* 通过完整的hql语句,将查询结果按pageSize分页,并返回第index页
*
* @param hql
* 完整的hql语句
* @param pageSize
* 页面大小
* @param index
* 页码
* @return page 页面
*/
public <T> Page<T> queryPageByHQL(final String hql, final int pageSize,
final int index) {
List<Page<T>> pageList = hibernateTemplate
.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List res = new ArrayList(1);
Page<T> page = new Page<T>();
Query query = session.createQuery(hql);
int totalRecord = query.list().size();
List<T> results = new ArrayList<T>();
int tmpIndex = 0;
if (totalRecord != 0) {
page.setPageSize(pageSize);
page.setTotalRecord(totalRecord);
page.countTotalPage();
if (index < 1) {
tmpIndex = 1;
}
if (index > page.getTotalPage()) {
tmpIndex = page.getTotalPage();
}
int firstResult = (tmpIndex - 1) * pageSize;
query.setFirstResult(firstResult);
query.setMaxResults(pageSize);
results = (List<T>) query.list();
page.setResults(results);
page.setIndex(tmpIndex);
page.setHasPreviousAndNext();
}
res.add(page);
return res;
}
});
return (Page<T>) pageList.get(0);
}
/**
* 通过带参数params的hql语句,将查询结果按pageSize分页,并返回第index页
*
* @param hql
* 带参数的hql语句
* @param pageSize
* 页面大小
* @param index
* 页码
* @param params
* 参数
* @return page 页面
*/
public <T> Page<T> queryPageByHQL(final String hql, final int pageSize,
final int index, final Map<String, Object> params) {
List<Page<T>> pageList = hibernateTemplate
.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List res = new ArrayList(1);
Page<T> page = new Page<T>();
Query query = session.createQuery(hql);
Iterator<String> iterator = params.keySet().iterator();
List<T> results = new ArrayList<T>();
int tmpIndex = 0;
while (iterator.hasNext()) {
String key = iterator.next();
query.setParameter(key, params.get(key));
}
int totalRecord = query.list().size();
if (totalRecord != 0) {
page.setPageSize(pageSize);
page.setTotalRecord(totalRecord);
page.countTotalPage();
if (index < 1) {
tmpIndex = 1;
}
if (index > page.getTotalPage()) {
tmpIndex = page.getTotalPage();
}
int firstResult = (tmpIndex - 1) * pageSize;
query.setFirstResult(firstResult);
query.setMaxResults(pageSize);
results = (List<T>) query.list();
page.setResults(results);
page.setIndex(tmpIndex);
page.setHasPreviousAndNext();
}
res.add(page);
return res;
}
});
return (Page<T>) pageList.get(0);
}
/**
* 通过完整的sql语句,将查询结果封装为entityClass类,并按pageSize分页,返回第index页
*
* @param entityClass
* 所要封装的类
* @param sql
* 完整的sql语句
* @param pageSize
* 页面大小
* @param index
* 页码
* @return page 页面
*/
public <T> Page<T> findPageBySQL(final Class<T> entityClass,
final String sql, final int pageSize, final int index) {
List<Page<T>> pageList = hibernateTemplate
.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List res = new ArrayList(1);
Page<T> page = new Page<T>();
Query query = session.createSQLQuery(sql).addEntity(
entityClass);
List<T> results = new ArrayList<T>();
int tmpIndex = 0;
int totalRecord = query.list().size();
if (totalRecord != 0) {
page.setPageSize(pageSize);
page.setTotalRecord(totalRecord);
page.countTotalPage();
// 设置第几页
page.setIndex(index);
if (index < 1) {
tmpIndex = 1;
}
if (index > page.getTotalPage()) {
tmpIndex = page.getTotalPage();
}
int firstResult = (tmpIndex - 1) * pageSize;
query.setFirstResult(firstResult);
query.setMaxResults(tmpIndex);
results = (List<T>) query.list();
page.setResults(results);
page.setHasPreviousAndNext();
}
res.add(page);
return res;
}
});
return (Page<T>) pageList.get(0);
}
/**
* 通过带参数params的sql语句,将查询结果封装为entityClass类 并按pageSize分页,返回第index页
*
* @param entityClass
* 所要封装的类
* @param sql
* 带参数的sql语句
* @param pageSize
* 页面大小
* @param index
* 页码
* @param params
* 参数
* @return page 页面
*/
public <T> Page<T> findPageBySQL(final Class<T> entityClass,
final String sql, final int pageSize, final int index,
final Map<String, Object> params) {
List<Page<T>> pageList = hibernateTemplate
.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List res = new ArrayList(1);
Page<T> page = new Page<T>();
Query query = session.createSQLQuery(sql).addEntity(
entityClass);
Iterator<String> iterator = params.keySet().iterator();
List<T> results = new ArrayList<T>();
int tmpIndex = 0;
while (iterator.hasNext()) {
String key = iterator.next();
query.setParameter(key, params.get(key));
}
int totalRecord = query.list().size();
if (totalRecord != 0) {
page.setPageSize(pageSize);
page.setTotalRecord(totalRecord);
page.countTotalPage();
if (index < 1) {
tmpIndex = 1;
}
if (index > page.getTotalPage()) {
tmpIndex = page.getTotalPage();
}
int firstResult = (tmpIndex - 1) * pageSize;
query.setFirstResult(firstResult);
query.setMaxResults(pageSize);
results = (List<T>) query.list();
page.setResults(results);
page.setIndex(tmpIndex);
page.setHasPreviousAndNext();
}
res.add(page);
return res;
}
});
return (Page<T>) pageList.get(0);
}
/**
* 通过完整的hql语句查询一条记录并返回
*
* @param hql
* 完整的hql语句
* @return result 结果
*/
@Override
public <T> T queryOneByHQL(final String hql) {
T result = (T) hibernateTemplate.execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
return (T) query.uniqueResult();
}
});
return result;
}
/**
* 通过带参数params的hql语句查询一条记录并返回
*
* @param hql
* 带参数的hql语句
* @param params
* 参数
* @return result 结果
*/
@Override
public <T> T queryOneByHQL(final String hql,
final Map<String, Object> params) {
T result = (T) hibernateTemplate.execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
Iterator<String> iterator = params.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
query.setParameter(key, params.get(key));
}
return (T) query.uniqueResult();
}
});
return result;
}
/**
* 通过完整的sql语句查询一条记录,并封装成entityClass类返回
*
* @param entityClass
* 所要封装的类
* @param sql
* 完整的sql语句
*/
@Override
public <T> T queryOneBySQL(final Class<T> entityClass, final String sql) {
T result = (T) hibernateTemplate.execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
return (T) session.createSQLQuery(sql).addEntity(entityClass)
.uniqueResult();
}
});
return result;
}
/**
* 通过带参数params的sql语句查询一条记录,并封装成entityClass类返回
*
* @param entityClass
* 所要封装的类
* @param sql
* 带参数的sql语句
* @param params
* 参数
*/
@Override
public <T> T queryOneBySQL(final Class<T> entityClass, final String sql,
final Map<String, Object> params) {
T result = (T) hibernateTemplate.execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql)
.addEntity(entityClass);
Iterator<String> iterator = params.keySet().iterator();
int tempNum = 0;
while (iterator.hasNext()) {
String key = iterator.next();
query.setParameter(tempNum, params.get(key));
tempNum++;
}
return (T) query.uniqueResult();
}
});
return result;
}
/**
* 通过完整的hql语句查询一组记录并返回
*
* @param hql
* 完整的hql语句
* @return results 结果集
*/
@Override
public <T> List<T> queryListByHQL(final String hql) {
List<T> results = hibernateTemplate
.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
return query.list();
}
});
return results;
}
/**
* 通过带参数params的hql语句查询一组记录并返回
*
* @param hql
* 带参数的hql语句
* @param params
* 参数
* @return results 结果集
*/
@Override
public <T> List<T> queryListByHQL(final String hql,
final Map<String, Object> params) {
List<T> results = hibernateTemplate
.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
Iterator<String> iterator = params.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
query.setParameter(key, params.get(key));
}
return query.list();
}
});
return results;
}
/**
* 通过完整的sql语句查询一组记录,并封装成entityClass类返回
*
* @param entityClass
* 所要封装的类
* @param sql
* 完整的sql语句
*/
@Override
public <T> List<T> queryListBySQL(final Class<T> entityClass,
final String sql) {
List<T> results = hibernateTemplate
.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
return session.createSQLQuery(sql)
.addEntity(entityClass).list();
}
});
return results;
}
/**
* 通过带参数params的sql语句查询一组记录,并封装成entityClass类返回
*
* @param entityClass
* 所要封装的类
* @param sql
* 完整的sql语句
* @param params
* 参数
*/
@Override
public <T> List<T> queryListBySQL(final Class<T> entityClass,
final String sql, final Map<String, Object> params) {
List<T> results = hibernateTemplate
.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql).addEntity(
entityClass);
Iterator<String> iterator = params.keySet().iterator();
int tempNum = 0;
while (iterator.hasNext()) {
String key = iterator.next();
query.setParameter(tempNum, params.get(key));
tempNum++;
}
return query.list();
}
});
return results;
}
@Override
public void saveOrUpdate(Object obj) {
getHibernateTemplate().saveOrUpdate(obj);
}
@Override
public void saveOrUpdateAll(List<Object> objs) {
getHibernateTemplate().saveOrUpdateAll(objs);
}
@SuppressWarnings("unchecked")
@Override
public Object get(Class clazz, long id) {
return getHibernateTemplate().get(clazz, id);
}
Page对象:
import java.util.List;
public class Page<T> {
private int index;//当前页码
private int pageSize;//每页大小
private int totalRecord;//总记录数
private int totalPage;//总页数?
private boolean hasNextPage;//是否有后一页
private boolean hasPreviousPage;//是否有前一页
List<T> results;//查询结果
public Page(int index, int maxSize, int totalRecord, int totalPage,
List<T> results) {
super();
this.index = index;
this.pageSize = maxSize;
this.totalRecord = totalRecord;
this.totalPage = totalPage;
this.results = results;
if(this.index<2){
hasPreviousPage=false;
}else{
hasPreviousPage=true;
}
if(this.index<totalPage){
hasNextPage = true;
}else{
hasNextPage = false;
}
}
public void setHasPreviousAndNext(){
if(this.index<2){
hasPreviousPage=false;
}else{
hasPreviousPage=true;
}
if(this.index<totalPage){
hasNextPage = true;
}else{
hasNextPage = false;
}
}
public Page() {
super();
}
public int getIndex() {
if (index == 0) {
index = 1;
}
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int maxSize) {
this.pageSize = maxSize;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getResults() {
return results;
}
public void setResults(List<T> results) {
this.results = results;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public int countTotalPage(){
int total = totalRecord % pageSize == 0 ? totalRecord/pageSize : totalRecord/pageSize+1;
this.setTotalPage(total);
return total;
}
public int getStartIndex() {
return (getIndex()-1) * pageSize + 1;
}
public int getEndIndex() {
if (getTotalRecord() > (getIndex() * pageSize)) {
return getIndex() * pageSize;
}else {
return getTotalRecord();
}
}
}