SSH 分页查询

直接进入主题:

建一个工具类:

Page

public class Page {
	//总页数
	private int page_zong;
	//有几条
	private int tiao_size;
	//当前页数
	private int dangqian;
	//总条数
	private int tiao_zong;
	public Page() {
		// TODO Auto-generated constructor stub
	}
	public int getPage_zong() {
		return page_zong;
	}
	public void setPage_zong(int page_zong) {
		this.page_zong = page_zong;
	}
	public int getTiao_size() {
		return tiao_size;
	}
	public void setTiao_size(int tiao_size) {
		this.tiao_size = tiao_size;
	}
	public int getDangqian() {
		return dangqian;
	}
	public void setDangqian(int dangqian) {
		this.dangqian = dangqian;
	}
	public int getTiao_zong() {
		return tiao_zong;
	}
	public void setTiao_zong(int tiao_zong) {
		this.tiao_zong = tiao_zong;
	}
	public Page(int page_zong, int tiao_size, int dangqian, int tiao_zong) {
		super();
		this.page_zong = page_zong;
		this.tiao_size = tiao_size;
		this.dangqian = dangqian;
		this.tiao_zong = tiao_zong;
	}
}
创建一个IPageDao的接口:

public interface IPageDao {
        //定义一个接口
	public List findByPage(Page p);
}
创建出来实体类PageDaoImpl:

public class PageDaoImpl implements IPageDao {
	public List findBypage(Page p) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		Criteria cri=session.createCriteria(Page.class);
		cri.setFirstResult((p.getDangqian()-1)*p.getTiao_size()).setMaxResults(p.getTiao_size());
		return cri.list();
	}	
}
创建出来业务逻辑层IPageService:

public interface IPageService {
        //定义一个接口
	public List findByPage(Page p);
}
创建出来业务逻辑层的实体类PageServiceImpl:

public class PageServiceImpl implements IPageService {

	private IPageDao ipd;
	
	public List findBypage(Page p) {
		// TODO Auto-generated method stub
		return ipd.findBypage(p);
	}
}
创建一个Action层PageAction:

注意:这里需要封装!!所定义的类   但是我省略掉了

public class PersonnelAction extends ActionSupport {
	private IPersonnelService ips;
	//分页
	private Page p=new Page();
	private int ye;
	private int ye_zong;
	private int tiaoshu;

        public String findbypage(){
        //得到总条数
		tiaoshu=2;
		int tiao=ips.findAll().size(); 
		//计算页数
            if(tiao%tiaoshu!=0){  p.setPage_zong((tiao/tiaoshu)+1);}else{
    	    p.setPage_zong(tiao/tiaoshu);	}
		p.setTiao_size(tiaoshu);
		if(ye>p.getPage_zong()){
			ye=p.getPage_zong();
		}if(ye==0){
			ye=1;
		}
		p.setDangqian(ye);
        ye_zong=p.getPage_zong();
		list_per=ips.findBypage(p);
		return "show";
	}
}


下面是JSP页面:

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
      //直接调用分页查询就可以,获取到他的第一页
    进入权限
  


list_p 界面  :

注意:在table外边写上一页---下一页。。。。。这里我就省略掉了 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'list_qx.jsp' starting page
    
	
	
	
	
	
	

  
  
  
    //。。。。省略
  	  已最前
  	 上一页
    
      ${ye }   /     ${ye_zong}
  	已最后
  	下一页
  
  


 对于本篇文章有不理解或者不怎么懂的地方可以在下方留言,我会给你解答,谢谢阅读!









你可能感兴趣的:(SSH 分页查询)