博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HibernateDaoSupport实现分页技术总结
阅读量:4682 次
发布时间:2019-06-09

本文共 6579 字,大约阅读时间需要 21 分钟。

1.定义MyHibernateDaoSupport 扩展HibernateSupport

mport java.sql.SQLException;

import java.util.List;

import org.hibernate.HibernateException;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class MyHibernateDaoSupport extends HibernateDaoSupport {

 public List findByPage(final String hql,final int offset,final int pageSize)
 {
  List list=this.getHibernateTemplate().executeFind(new HibernateCallback()
  {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    List result=session.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize).list();
    return result;
   }
  }
  );
  return list;
 }
 public List findByPage(final String hql,final String value,final int offset,final int pageSize)
 {
  List list=this.getHibernateTemplate().executeFind(new HibernateCallback()
  {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    List result=session.createQuery(hql).setParameter(0,value).setFirstResult(offset).setMaxResults(pageSize).list();
    return result;
   }
  }
  );
  return list;
 }
 public List findByPage(final String hql,final Object[] values,final int offset,final int pageSize){
  List list=this.getHibernateTemplate().executeFind(new HibernateCallback()
  {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Query query=session.createQuery(hql);
    for(int i=0;i<values.length;i++)
    {
     query.setParameter(i,values[i]);
    }
    List result=query.setFirstResult(offset).setMaxResults(pageSize).list();
    return result;
   }
  }
  );
  return list;
 }
}

2.定义要分页的实体的Dao接口
如:
public interface StudentDao {
 Student get(int id);
 void save(Student student);
 void update(Student student);
 void delete(int id);
 void delete(Student student);
 List<Student> findAll();
 List<Student> findAllByPage(int pageNo,int pageSize);
 int getStudentCount();
 List<Student> findStudentByNameAndNumber(String stuName,String stuNumber);
}
3.定义实现类
主要写出两个分页中要用到的方法
public List<Student> findAllByPage(int pageNo, int pageSize) {
  if(pageNo<1){
   return null;
  }
  int offset=(pageNo-1)*pageSize;
  return findByPage("from Student", offset, pageSize);
 }
 public int getStudentCount() {
  List<Student> listStudent=this.getHibernateTemplate().find("from Student");
  return listStudent.size();
 }
4.定义Service接口
public interface ExamService {
 int STUDENT_PAGE_SIZE=3;
 int QUESTION_PAGE_SIZE=3;
 int addStudent(String stuNumber,String name,String className,String humanId,String email,String address,String phone) throws Exception;
 void deleteStudent(int id) throws Exception;
 List<Student> listStudent(int pageNo) throws Exception;
 int addQuestion(String quTitle,String quHard,String quScore,String quAnswer,String quType,String selectOption,int typeid) throws Exception;
 void deleteQuestion(int id) throws Exception;
 List<Question> listQuestion(int pageNo) throws Exception;
 void deleteExamtype(int typeid) throws Exception;
 int addExamtype(String textName,String testTime)throws Exception;
 List<Examtype> getAllExamtype()throws Exception;
 boolean adminLogin(String admName,String admPwd)throws Exception;
 int getStudentCount()throws Exception;
 int getQuestionCount()throws Exception;
 int getPageCount(int count,int pageSize);
 String studentLogin(String stuName,String stuNumber)throws Exception;
 Question getNextQuestion(List<Integer> alreadys,int typeid)throws Exception;
 Question getQuestionById(int id)throws Exception;
 String getExamtypeName(int typeid)throws Exception;;
}
5.定义实现类
public int getPageCount(int count, int pageSize) {
  return (count+pageSize-1)/pageSize;
 }
public int getStudentCount() throws Exception {
  return studentDao.getStudentCount();
 }

public List<Student> listStudent(int pageNo) throws Exception {

  return studentDao.findAllByPage(pageNo, STUDENT_PAGE_SIZE);

 }

6.ListStudentAction.java

int studentCount=examService.getStudentCount();
  ActionMessages errors=new ActionMessages();
  if(studentCount<1)
  {
   errors.add("studentCount",new ActionMessage("studentCount.null"));
   mapping.findForward("success");
  }
  int pageCount=examService.getPageCount(studentCount,examService.STUDENT_PAGE_SIZE);
  int pageNo;
  if(request.getParameter("pageNo")==null || request.getParameter("pageNo").trim().equals(""))
  {
   pageNo=1;
  }
  try {
   pageNo=Integer.parseInt(request.getParameter("pageNo").trim());
  } catch (Exception e) {
   pageNo=1;
  }
  if(pageNo>pageCount){
   pageNo=pageCount;
  }
  request.setAttribute("pageCount",pageCount);
  request.setAttribute("currentPage",pageNo);
  request.setAttribute("studentList", examService.listStudent(pageNo));
  return mapping.findForward("success");
7.listStudent.jsp

<table cellspacing="0" cellpadding="0" border="1" width="700">

  <tr>
   <th>
    <bean:message key="student.shenfenzheng" />
   </th>
   <th>
    <bean:message key="student.mingzi" />
   </th>
   <th>
    <bean:message key="student.banji" />
   </th>
   <th>
    <bean:message key="student.xuehao" />
   </th>
   <th>
    <bean:message key="student.youjian" />
   </th>
   <th>
    <bean:message key="student.dianhua" />
   </th>
   <th>
    <bean:message key="student.address" />
   </th>
   <th>
    <bean:message key="student.isdelete" />
   </th>
  </tr>
  <c:forEach items="${requestScope.studentList}" var="students">
   <tr>
    <td align="center">
     ${students.humanId}
    </td>
    <td align="center">
     ${students.stuName}
    </td>
    <td align="center">
     ${students.stuClassName}
    </td>
    <td align="center">
     ${students.stuNumber}
    </td>
    <td align="center">
     ${students.email}
    </td>
    <td align="center">
     ${students.phone}
    </td>
    <td align="center">
     ${students.address}
    </td>
    <td align="center">
     <a href="deleteStudent.do?delStuid=${students.id}"
      οnclick='return confirm("<bean:message key="confirm.del.student"/>");' target="center"><bean:message
       key="student.delete" />
     </a>
    </td>
   </tr>
  </c:forEach>
  <br />
  <tr>
   <td colspan="7" align="center">
    第${requestScope.currentPage}页 &nbsp;共${requestScope.pageCount}页
    <a href="listStudent.do?pageNo=1">首页</a>&nbsp;
    <logic:greaterThan value="1" name="currentPage" scope="request">
     <a href="listStudent.do?pageNo=${requestScope.currentPage-1}">
    </logic:greaterThan>
    上一页
    <logic:greaterThan value="1" name="currentPage" scope="request">
     </a>
    </logic:greaterThan>

    <logic:lessThan value="${requestScope.pageCount}" name="currentPage"

     scope="request">
     <a href="listStudent.do?pageNo=${requestScope.currentPage+1}">
    </logic:lessThan>
    下一页
    <logic:lessThan value="${requestScope.pageCount}" name="currentPage"
     scope="request">
     </a>
    </logic:lessThan>
    <a href="listStudent.do?pageNo=${requestScope.pageCount}">尾页</a>&nbsp;
   </td>
  </tr>
 </table>

转载于:https://www.cnblogs.com/dengjiali/articles/3599016.html

你可能感兴趣的文章
Python-面向对象(组合、封装与多态)
查看>>
Mininet
查看>>
COSC2531 Programming Fundamentals
查看>>
设计模式系列 - 访问者模式
查看>>
20180507小测
查看>>
eclipse左侧不见
查看>>
python会缓存小的整数和短小的字符
查看>>
格网与四叉树索引
查看>>
多张照片拍摄、图片浏览
查看>>
html(5) css
查看>>
Azure Web连接到Azure MySql Db
查看>>
Linux shell 命令判断执行语法 ; , && , ||
查看>>
vim代码格式化插件clang-format
查看>>
RTP Payload Format for Transport of MPEG-4 Elementary Streams over http
查看>>
递归 换零钱问题——由打靶子问题引申
查看>>
Python-函数基础
查看>>
Extensible Messaging and Presence Protocol (XMPP) 简介
查看>>
Farm Irrigation
查看>>
windows平板的开发和选型
查看>>
无平方因子的数(数论初步) By ACReaper
查看>>