温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySql实现翻页查询功能

发布时间:2020-10-20 13:03:31 来源:脚本之家 阅读:238 作者:0vs1 栏目:MySQL数据库

首先明确为什么要使用分页查询,因为数据庞大,查询不可能全部显示在页面上,如果全部显示在页面上,也会造成查询速度慢的情况,所以分页查询解决了①数据查询;②性能优化,等(其他问题欢迎补充)的问题。

分页查询也分为真分页和假分页:

  真分页:基于数据库查出的数据直接分页显示,优点是改变数据库数据不会影响查询结果,缺点是速度稍慢。

  假分页:将所有数据查询出的数据,封装到list集合缓存中,表现层方法调用执行。由于将数据封装为集合放入了内存中,所以速度较快,但缺点是数据库改变后,会出现不匹配的情况。

  两种分页各有优缺点,小伙伴们视具体情况使用吧。

下面要介绍的就是真分页的方法:

1、建立JavaBean

import java.io.Serializable;
/**
 * 用户实体类
 * @author 
 *
 */
public class UserBean implements Serializable {
  /**用户ID*/
  private int id;
  /**用户名字*/
  private String name;
  public UserBean() {
  }
  public UserBean(int id, String name) {
    this.id = id;
    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

/**
 * 用于展示分页数据的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

总结

以上所述是小编给大家介绍的MySql实现翻页查询功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI