分頁實現原理及model層代碼

 

     分頁怎麼實現:
           看到效果

       1    當前頁-1       當前頁+1       最後一頁   總頁數
      首頁   上一頁  下一頁   末頁
算法分析:------------------------------------------------
     當前頁:
             int nowpage;
     首頁:
            nowpage=1;
     末頁:
            int countpage; 總頁
            nowpage=countpage;
     上一頁:
            nowpage = nowpage-1;
            if(nowpage<1){
                  nowpage=1;
            }
     下一頁:
           nowpage = nowpage+1;
           if(nowpage>countpage){
              nowpage=countpage;
           }
-----------------------------------------------------
理解概念:
     當前頁 nowpage
     總頁數 countpage =======7頁     
     每頁顯示的記錄數  10條
     當前頁開始的記錄數:(nowpage-1)*10+1;
     1  1-10
     2  11-20
     3  21 30
     4  31 40
     5  41 50
     總記錄數:
           countrecord  =64記錄
總頁數==總記錄數%每頁顯示的記錄數==0?總記錄數/每頁顯示的記錄數:總記錄數/每頁顯示的記錄數+1;
------------------------------------------
表的操作:
    總記錄數:select count(*) from 表名;
    每頁顯示3條記錄: 聲明 int pagesize=3;
    總頁數:總頁數==總記錄數%每頁顯示的記錄數==0?總記錄數/每頁顯示的記錄數:總記錄數/每頁顯示的記錄數+1;
    當前頁的記錄信息:
          select * from 表名  limit (nowpage-1)*pagesize,pagesize;
    

package qi.dao;

import java.util.List;

import qi.domain.Admin;

public interface AdminDao {
 List<Admin>findNowPageInfo(Integer nowpage);
 Integer findCountRecord();
 Integer findCountPage();

}

 

 

 


 

package qi.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import qi.domain.Admin;

public class AdminDaoImpl implements AdminDao {
 
 private static Connection conn;
 private PreparedStatement pstmt;
 private ResultSet rs;
 private static final String URL = "jdbc:mysql://localhost:3306/3g?user=root&password=bwosi1nea&useUnicode=true&characterEncoding=UTF8";

 static {
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   conn = DriverManager.getConnection(URL);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 private static final Integer PAGESIZE=3;
 private Integer countRecord;
 private Integer countPage;
 public List<Admin> findNowPageInfo(Integer nowpage) {
  List<Admin> allentites = new ArrayList<Admin>();
  String sql = "select id,name,pass,sex,age from admin limit ?,?";
  try {
   pstmt = conn.prepareStatement(sql);
   int index=1;
   pstmt.setInt(index++,(nowpage-1)*PAGESIZE);
   pstmt.setInt(index++,PAGESIZE);
   rs = pstmt.executeQuery();
   while (rs.next()) {
    Admin entity = new Admin();
    entity.setId(rs.getInt("id"));
    entity.setName(rs.getString("name"));
    entity.setPass(rs.getString("pass"));
    entity.setSex(rs.getString("sex"));
    entity.setAge(rs.getInt("age"));

    allentites.add(entity);
   }
   release(rs, pstmt);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return allentites;
 }
 public Integer findCountRecord() {
  String sql="select count(*) from admin";
  try {
   pstmt = conn.prepareStatement(sql);
   rs=pstmt.executeQuery();
   if(rs.next()){
    
    this.countRecord = rs.getInt(1);
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return this.countRecord;
 }
 private void release(ResultSet rs, PreparedStatement pstmt) {
  if (rs != null) {
   try {
    rs.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  if (pstmt != null) {
   try {
    pstmt.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

 }
 public Integer findCountPage() {
  findCountRecord();
  this.countPage=this.countRecord%this.PAGESIZE==0?this.countRecord/this.PAGESIZE:this.countRecord/this.PAGESIZE+1;
  return this.countPage;
 }
}

 

 

 


 

package qi.domain;
public class Admin {
 
 private Integer id;
 private String name;
 private String pass;
 private String sex;
 private Integer age;
 public Admin() {
  super();
  // TODO Auto-generated constructor stub
 }
 public Admin(Integer id, String name, String pass, String sex, Integer age) {
  super();
  this.id = id;
  this.name = name;
  this.pass = pass;
  this.sex = sex;
  this.age = age;
 }
 @Override
 public String toString() {
  return "Admin [id=" + id + ", name=" + name + ", pass=" + pass
    + ", sex=" + sex + ", age=" + age + "]";
 }
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPass() {
  return pass;
 }
 public void setPass(String pass) {
  this.pass = pass;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public Integer getAge() {
  return age;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
 
}

 

 

 

 

package qi.junit;

import java.util.List;

import org.junit.Test;

import qi.domain.Admin;
import qi.service.AdminServiceImpl;


public class AdminTest {
 private AdminServiceImpl aService = new AdminServiceImpl();

 @Test
 public void getCountRecrd(){
  System.out.println(aService.findCountRecord());
 }
 @Test
 public void getCountPage(){
  System.out.println(aService.findCountPage());
 }
 @Test
 public void NowPageInfo(){
  List<Admin> admins = aService.findNowPageInfo(4);
  for(Admin admin:admins){
   System.out.println(admin.toString());
  }
 }
}

 

 

 


 

package qi.service;

import qi.dao.AdminDao;

public interface AdminService extends AdminDao{

}

 

 

 


 

package qi.service;

import java.util.List;

import qi.dao.AdminDao;
import qi.dao.AdminDaoImpl;
import qi.domain.Admin;

public class AdminServiceImpl implements AdminService{
 private AdminDao aDao = new AdminDaoImpl();
 public List<Admin> findNowPageInfo(Integer nowpage) {
  return aDao.findNowPageInfo(nowpage);
 }

 public Integer findCountRecord() {
  return aDao.findCountRecord();
 }

 public Integer findCountPage() {
  return aDao.findCountPage();
 }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章