Hibernate3 提供了DetachedCriteria,使得我们可以在Web层构造detachedCriteria,然后调用业务层Bean,进行动态条件查询,根 据这一功能,我设计了通用的抽象Bean基类和分页类支持,代码来自于Quake Wang的javaeye-core包的相应类,然后又做了很多修改。
分页支持类:
package org.exam.util;
import java.util.List;
public class PaginationSupport {
public final static int PAGESIZE = 30;
private int pageSize = PAGESIZE;
private List items;
private int totalCount;
private int[] indexes = new int[0];
private int startIndex = 0;
public PaginationSupport(List items, int totalCount) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(0);
}
public PaginationSupport(List items, int totalCount, int startIndex) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
setPageSize(pageSize);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if (totalCount > 0) {
this.totalCount = totalCount;
int count = totalCount / pageSize;
if (totalCount % pageSize > 0)
count++;
indexes = new int[count];
for (int i = 0; i < count; i++) {
indexes[i] = pageSize * i;
}
} else {
this.totalCount = 0;
}
}
public int[] getIndexes() {
return indexes;
}
public void setIndexes(int[] indexes) {
this.indexes = indexes;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
if (totalCount <= 0)
this.startIndex = 0;
else if (startIndex >= totalCount)
this.startIndex = indexes[indexes.length - 1];
else if (startIndex < 0)
this.startIndex = 0;
else {
this.startIndex = indexes[startIndex / pageSize];
}
}
public int getNextIndex() {
int nextIndex = getStartIndex() + pageSize;
if (nextIndex >= totalCount)
return getStartIndex();
else
return nextIndex;
}
public int getPreviousIndex() {
int previousIndex = getStartIndex() - pageSize;
if (previousIndex < 0)
return 0;
else
return previousIndex;
}
}
service层方法:
package org.exam.service.impl;
import java.util.List;
import org.exam.dao.QuestionDao;
import org.exam.dao.TestQuestionsDao;
import org.exam.model.Question;
import org.exam.model.TestQuestions;
import org.exam.service.QuestionService;
import org.exam.util.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
public class QuestionServiceImpl implements QuestionService{
private QuestionDao questionDao;
private TestQuestionsDao testQuestionsDao;
public TestQuestionsDao getTestQuestionsDao() {
return testQuestionsDao;
}
public void setTestQuestionsDao(TestQuestionsDao testQuestionsDao) {
this.testQuestionsDao = testQuestionsDao;
}
public QuestionDao getQuestionDao() {
return questionDao;
}
public void setQuestionDao(QuestionDao questionDao) {
this.questionDao = questionDao;
}
public Integer add(Integer tqid , String title , String hard , String type , Integer score)
{
Question q = new Question();
q.setTq((TestQuestions)testQuestionsDao.findById(tqid).get(0));
q.setTitle(title);
q.setHard(hard);
q.setType(type);
q.setScore(score);
questionDao.save(q);
return q.getId();
}
public boolean delete(Integer id)
{
questionDao.delete(id);
return true;
}
public List<Question> getByTqid(Integer id)
{
List<Question> tmp = questionDao.findByTqId(id);
return tmp;
}
public List<Question> getAll(int pageSize , int index)
{
DetachedCriteria detachedCriteria = getDetachedCriteria();
PaginationSupport ps = questionDao.findPageByCriteria(detachedCriteria , pageSize , index);
return(ps.getItems());
}
private DetachedCriteria getDetachedCriteria(String key)
{
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Question.class);
detachedCriteria.add(Restrictions.like("title", "%"+key+"%"));
return detachedCriteria;
}
private DetachedCriteria getDetachedCriteria()
{
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Question.class);
return detachedCriteria;
}
public List<Question> getByKey(String key , int pageSize , int index)
{
DetachedCriteria detachedCriteria = getDetachedCriteria(key);
PaginationSupport ps = questionDao.findPageByCriteria(detachedCriteria , pageSize , index);
return(ps.getItems());
}
public int getCount(String key)
{
DetachedCriteria detachedCriteria = getDetachedCriteria(key);
PaginationSupport ps = questionDao.findPageByCriteria(detachedCriteria);
return ps.getTotalCount();
}
public int getIndex(String key)
{
DetachedCriteria detachedCriteria = getDetachedCriteria(key);
PaginationSupport ps = questionDao.findPageByCriteria(detachedCriteria);
return ps.getStartIndex();
}
public int getCount()
{
DetachedCriteria detachedCriteria = getDetachedCriteria();
PaginationSupport ps = questionDao.findPageByCriteria(detachedCriteria);
return ps.getTotalCount();
}
public int getIndex()
{
DetachedCriteria detachedCriteria = getDetachedCriteria();
PaginationSupport ps = questionDao.findPageByCriteria(detachedCriteria);
return ps.getStartIndex();
}
public int getNextIndex(String key , int pageSize , int index)
{
DetachedCriteria detachedCriteria = null;
if(key == null)
{
detachedCriteria = getDetachedCriteria();
}
else
{
detachedCriteria = getDetachedCriteria(key);
}
PaginationSupport ps = questionDao.findPageByCriteria(detachedCriteria , pageSize , index);
return ps.getNextIndex();
}
public int getPreviousIndex(String key , int pageSize , int index)
{
DetachedCriteria detachedCriteria = null;
if(key == null)
{
detachedCriteria = getDetachedCriteria();
}
else
{
detachedCriteria = getDetachedCriteria(key);
}
PaginationSupport ps = questionDao.findPageByCriteria(detachedCriteria , pageSize , index);
return ps.getPreviousIndex();
}
public int getLast(String key , int pageSize)
{
DetachedCriteria detachedCriteria = null;
if(key == null)
{
detachedCriteria = getDetachedCriteria();
}
else
{
detachedCriteria = getDetachedCriteria(key);
}
PaginationSupport ps = questionDao.findPageByCriteria(detachedCriteria);
int index = ps.getTotalCount()/pageSize + ps.getTotalCount()%pageSize;
return (index - 1) * pageSize;
}
}
用户在web层构造查询条件detachedCriteria,和可选的startIndex,调用业务bean的相应findByCriteria方法,返回一个PaginationSupport的实例ps。
ps.getItems()得到已分页好的结果集
ps.getIndexes()得到分页索引的数组
ps.getTotalCount()得到总结果数
ps.getStartIndex()当前分页索引
ps.getNextIndex()下一页索引
ps.getPreviousIndex()上一页索引