Java分页查询page类

Java分页查询page

1.创建page实现类和想要分页数据的实现类

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class PageUtil <T>{
private Integer pageIndex; //页码
private Integer pageSize; //页容量
private Integer total; //总条数
private Integer totalPage; //总页数
private Integer beginIndex; //起始位置
private T rows; //数据集

public PageUtil(Integer pageIndex, Integer pageSize, Integer total) {
this.pageIndex = pageIndex;
this.pageSize = pageSize;
this.total = total;
this.totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize +1;
this.beginIndex = (pageIndex -1)*pageSize;
}

public Integer getPageIndex() {
return pageIndex;
}

public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
}

public Integer getPageSize() {
return pageSize;
}

public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}

public Integer getTotal() {
return total;
}

public void setTotal(Integer total) {
this.total = total;
}

public Integer getTotalPage() {
return totalPage;
}

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

public Integer getBeginIndex() {
return beginIndex;
}

public void setBeginIndex(Integer beginIndex) {
this.beginIndex = beginIndex;
}

public T getRows() {
return rows;
}

public void setRows(T rows) {
this.rows = rows;
}
}

想要分页的数据实现类

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
public class NewsCategory {
private Long id;
private String name;
private Date createDate;

@Override
public String toString() {
return "NewsCategory{" +
"id=" + id +
", name='" + name + '\'' +
", createDate=" + createDate +
'}';
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Date getCreateDate() {
return createDate;
}

public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}

2.Dao层

在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
41
42
43
44
45
46
/***
* 获取新闻分类数量
* @return
*/
public Integer getTotal(){
getConnection();
String sql = "select count(id) from news_category";
Integer total = null;
rs = executeQuery(sql , null);
try {
while(rs.next()){
total = rs.getInt("count(id)");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
close();
return total;
}

/***
* 分页查询数据
* @param beginIndex 起始位置
* @param pageSize 页容量
* @return 符合条件的数据集
*/
public List<NewsCategory> queryNewsPage(Integer beginIndex , Integer pageSize){
getConnection();
String sql = "select id,name,createDate from news_category limit ?,?";
Object[] params = {beginIndex,pageSize};
rs = executeQuery(sql,params);
List<NewsCategory> newsCategories = new ArrayList<>();
try {
while(rs.next()){
NewsCategory newsCategory = new NewsCategory();
newsCategory.setId(rs.getLong("id"));
newsCategory.setName(rs.getString("name"));
newsCategory.setCreateDate(rs.getDate("createDate"));
newsCategories.add(newsCategory);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
close();
return newsCategories;
}

3.service层

service层添加一个方法,就是分页功能方法,参数是页码和页容量,接口添加。

1
2
3
4
5
6
7
8
9
10
11
public PageUtil<List<NewsCategory>> page(Integer pageIndex, Integer pageSize){
pageIndex = pageIndex == null ? 1:pageIndex;
pageSize = pageSize == null ? 5 :pageSize;
//查找条数
Integer total = newsCategoryDao.getTotal();
PageUtil<List<NewsCategory>> pageUtil = new PageUtil<>(pageIndex,pageSize,total);
//开始分页查找
List<NewsCategory> newsCategories = newsCategoryDao.queryNewsPage(pageUtil.getBeginIndex(),pageUtil.getPageSize());
pageUtil.setRows(newsCategories);
return pageUtil;
}

4测试

1
2
3
4
5
6
7
8
9
10
11
public class test1 {
public static void main(String[] args) {
NewsCategoryService newsCategoryService = new NewsCategoryServiceImpl();
PageUtil<List<NewsCategory>> page = newsCategoryService.page(1,3);
System.out.println("总页数"+page.getTotalPage());
System.out.println("当前页数据");
for (NewsCategory row : page.getRows()){
System.out.println(row);
}
}
}