服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|

服务器之家 - 数据库 - Mysql - MySql实现翻页查询功能

MySql实现翻页查询功能

2020-12-15 21:590vs1 Mysql

分页查询在网页中随处可见,那原理是什么呢?下面简单介绍一下基于MySql数据库的limit实现方法,感兴趣的朋友一起看看吧

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

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

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

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

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

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

1、建立JavaBean

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * 用于展示分页数据的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层实现类

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@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层实现类

  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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实现翻页查询功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/hpqbh/archive/2019/11/12/11839334.html

延伸 · 阅读

精彩推荐