MySql query page

Paging query can be seen everywhere in the web page, what principle is it? Following a brief limit implementation based on MySql database.

First clear why use paging query, because the data is large, the query can not show all on the page, if all displayed on the page, can also cause slow query case, the paging query data query solved ①; ② performance optimization, etc. (please add other issues) problems.

Pagination queries can also be divided into true and false pagination pagination:

  True page: Isolated based database data directly pagination, the advantage is to change the database data will not affect the results, the drawback is slower.

  False Page: check out all the data of the data package to the collection list cache, the presentation layer method called. Since the data is packaged as a set into the memory, so that faster, but the drawback is the database changes, there will be a mismatch.

  Two kinds of pages have advantages and disadvantages, depending on the specific circumstances little friends use it.

Paging is the true way to introduce the following:

1, the establishment of JavaBean

Import the java.io.Serializable;
 / ** 
 * User entity class 
 * @author  
 * 
 * / 
public  class the UserBean the implements the Serializable {
     / ** User ID * / 
    Private  int ID;
     / ** user name * / 
    Private String name;
     public the UserBean () { 
        
    } 
    public the UserBean ( int ID, String name) {
         the this .id = ID;
         the this .name = name; 
    } 
    public  int getId () {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "UserBean [id=" + id + ", name=" + name + "]";
    }

}

2, JavaBean paged data for display

/**
 * 用于展示分页数据的JavaBean对象
 * @author
 *
 */

import java.util.List;

public class PagenationBean {
    /** 当前页数 */
    private Integer currPage;
    /** 总页数 */
    private Integer totalPage;
    /** 用于展示的table数据 */
    private List<UserBean> dataList;

    public Integer getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List<StuBean> getDataList() {
        return dataList;
    }

    public void setDataList(List<StuBean> dataList) {
        this.dataList = dataList;
    }

}

3、dao层实现类

  @Override
    public int getTotalCount() { //计算总的数据条数
        this.setConnection();
        int totalCount = 0;
        try {
            ps = con.prepareStatement("select count(*) from t_user");
            rs = ps.executeQuery();
            if (rs.next()) {
                totalCount = rs.getInt(1);
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
        return totalCount;
    }
   @Override
   public List<UserBean> getUserListByStartIndex(int StartIndex) { //根据传入的limit第一位参数得到该参数后面的10条数据
        List<UserBean> userList = new ArrayList<>();
        UserBean userBean= null;
        this.setConnection();
        int totalCount = 0;
        try {
            ps = con.prepareStatement("select * from t_user limit ? , 10");
            ps.setInt(1, StartIndex);
            rs = ps.executeQuery();
            while (rs.next()) {
                userBean= new StuBean();
                userBean.setId(rs.getInt("id"));
                userBean.setName(rs.getString("name"));
                stuList.add(userBean);
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            this.closeConnection();
        }        
        return userList;
    }    

4、service层实现类

  private IUserDao isd = new UserDaoImpl();

  @Override
public int getTotalPage() { //得到数据据条数 int totalCount = isd.getTotalCount(); //计算总页数公式 int totalPage = (totalCount + 10 -1)/10; return totalPage; }
  @Override
    public List<UserBean> getUserListByCurrPage(int currPage) {
        //通过当前页计算起始索引
        int StartIndex = (currPage - 1) * 10;
        List<UserBean> userList = isd.getStuListByStartIndex(StartIndex);
        return userList;
    }

5、将查询出的数据放入页面展示就OK了。

以上方法中,分页显示的是10条数据,计算分析如下:

   数据总条数:  totalCount

  每页显示条数: pageSize

  总页数:    totalPage

  起始索引    StartIndex

  当前页数    currPage

  总页计算公式:

     totalCount % pageSize

      如果余数为0 ——> totalPage=totalCount / pageSize

         如果余数不为0 ——> totalPage=totalCount / pageSize +1

    得出结论:totalPage = (totalCount + pageSize -1)/pageSize

 

欢迎指正不足之处!


The END

 

Guess you like

Origin www.cnblogs.com/hpqbh/p/11839334.html