1. 程式人生 > >hibernate中的通用分頁basedao

hibernate中的通用分頁basedao

hibernate原生態分頁寫法

public List<Book> list1(Book book,PageBean pageBean) {
		Session session = SessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		String hql="from Book where 1=1";
		if(StringUtils.isNotBlank(book.getBookname())) {
			hql+=" and bookname like :bookname";
		}
		Query query = session.createQuery(hql);
		if(StringUtils.isNotBlank(book.getBookname())) {
			query.setParameter("bookname","%"+book.getBookname()+"%");
			
		}
		if(pageBean!=null && pageBean.isPagination()) {
			query.setFirstResult(pageBean.getStartIndex());
			query.setMaxResults(pageBean.getRows());
		}
		List list = query.list();
		
		transaction.commit();
		session.close();
		return list;
	}

如果表有10個列段,10個都要有query.setParameter 這樣對於維護以及後續的改進來說太過繁瑣

下面是basedao(不怎麼完整的,可繼續簡化)

package com.util;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.query.Query;

/**
 * 分頁:
 * 1、hql-->counthql-->total-->pagebean
 * 2、hql-->pagehql-->result(hibernate呼叫內建介面自動生成分頁語句)
 * 
 * @author nie
 *
 */
public class BaseDao {

	private void setParament(Query query,Map<String, Object> map) {
		if(map==null||map.size()==0) {
			return;//直接結束,不返回
		}
		Object value=null;
		//給query賦值
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			value=entry.getValue();
			//對value值是集合的處理方式
			if(value instanceof Collection) {
				query.setParameterList(entry.getKey(),(Collection) value);
			}//value值是陣列
			else if(value instanceof Object[]) {
				query.setParameterList(entry.getKey(),(Object[]) value);
			}else {
				query.setParameter(entry.getKey(), value);
			}
		}
	}
	
	private String getCountHql(String hql) {
		int index=hql.toUpperCase().indexOf("FROM");
		return "select count(*)"+hql.substring(index);
	}
	
	public List executeQuery(String hql,PageBean pageBean,Map<String, Object> map,Session session) {
		if(pageBean!=null&&pageBean.isPagination()) {
			String countHql=getCountHql(hql);
			Query countquery = session.createQuery(countHql);
			this.setParament(countquery, map);
			String total = countquery.getSingleResult().toString();
			pageBean.setTotal(total);
			Query pagequery = session.createQuery(hql);
			this.setParament(pagequery, map);
			pagequery.setFirstResult(pageBean.getStartIndex());
			pagequery.setMaxResults(pageBean.getRows());
			return pagequery.list();
		}else {
			Query query = session.createQuery(hql);
			this.setParament(query, map);
			return query.list();
		}
	}
	
}

修改剛才的dao方法

	public List<Book> list2(Book book,PageBean pageBean) {
		Session session = SessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		String hql="from Book where 1=1";

		Map<String, Object> map=new HashMap<String, Object>();
		if(StringUtils.isNotBlank(book.getBookname())) {
			hql+=" and bookname like :bookname";
			map.put("bookname","%"+book.getBookname()+"%");
		}
		List list = super.executeQuery(hql, pageBean, map, session);
		
		transaction.commit();
		session.close();
		return list;
	}