Chinaunix首页 | 论坛 | 博客
  • 博客访问: 564291
  • 博文数量: 136
  • 博客积分: 4010
  • 博客等级: 上校
  • 技术积分: 1343
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-19 23:18
文章分类

全部博文(136)

文章存档

2011年(28)

2009年(60)

2008年(48)

我的朋友

分类: Java

2009-04-02 14:01:17

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    {
     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 findAll();
 List findAllByPage(int pageNo,int pageSize);
 int getStudentCount();

 List findStudentByNameAndNumber(String stuName,String stuNumber);
}
3.定义实现类
主要写出两个分页中要用到的方法
public List 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 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 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 listQuestion(int pageNo) throws Exception;
 void deleteExamtype(int typeid) throws Exception;
 int addExamtype(String textName,String testTime)throws Exception;
 List 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 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 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


  
   
   
   
   
   
   
   
   
  
  
   

    
    
    
    
    
    
    
    
   
  
  

  
   
  
 

    
   

    
   

    
   

    
   

    
   

    
   

    
   

    
   

     ${students.humanId}
    

     ${students.stuName}
    

     ${students.stuClassName}
    

     ${students.stuNumber}
    

     ${students.email}
    

     ${students.phone}
    

     ${students.address}
    

           onclick='return confirm("");' target="center">       key="student.delete" />
     

    

    第${requestScope.currentPage}页  共${requestScope.pageCount}页
    首页 
    
     
    

    上一页
    
     

    

         scope="request">
     
    

    下一页
         scope="request">
     

    
    尾页 
   

阅读(3809) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~