分页功能
属性封装分页工具类统计总记录数分页查询功能的实现
属性
private int totalCount
;
private int pageSize
;
private int pageCount
;
private int currentPageIndex
;
private boolean hasPrevious
;
private boolean hasNext
;
private int beginRow
;
封装分页工具类
package com
.neusoft
.utils
;
public class PageInfo {
private int totalCount
;
private int pageSize
;
private int pageCount
;
private int currentPageIndex
=1;
private int beginRow
;
private boolean hasPrevious
;
private boolean hasNext
;
public int getTotalCount() {
return totalCount
;
}
public void setTotalCount(int totalCount
) {
this.totalCount
= totalCount
;
}
public int getPageSize() {
return pageSize
;
}
public void setPageSize(int pageSize
) {
this.pageSize
= pageSize
;
}
public int getPageCount() {
if (totalCount
%pageSize
==0){
pageCount
=totalCount
/pageSize
;
}else {
pageCount
=(totalCount
/pageSize
)+1;
}
return pageCount
;
}
public void setPageCount(int pageCount
) {
this.pageCount
= pageCount
;
}
public int getCurrentPageIndex() {
return currentPageIndex
;
}
public void setCurrentPageIndex(int currentPageIndex
) {
this.currentPageIndex
= currentPageIndex
;
}
public int getBeginRow() {
beginRow
=(currentPageIndex
-1)*pageSize
;
return beginRow
;
}
public void setBeginRow(int beginRow
) {
this.beginRow
= beginRow
;
}
public boolean isHasPrevious() {
hasPrevious
= currentPageIndex
==1?false:true;
return hasPrevious
;
}
public void setHasPrevious(boolean hasPrevious
) {
this.hasPrevious
= hasPrevious
;
}
public boolean isHasNext() {
hasNext
=currentPageIndex
==pageCount
?false:true;
return hasNext
;
}
public void setHasNext(boolean hasNext
) {
this.hasNext
= hasNext
;
}
}
统计总记录数
UserDaoImpl:
@Override
public int getTotalCount() {
int result
=0;
try {
conn
= DBUtils
.getConn();
ps
= conn
.prepareStatement("select count(*) from t_user");
rs
= ps
.executeQuery();
if (rs
.next()){
result
= rs
.getInt(1);
}
} catch (SQLException e
) {
e
.printStackTrace();
}finally {
DBUtils
.closeAll(rs
,ps
,conn
);
}
return result
;
}
分页查询功能的实现
public List
<User> pageQueryAll(PageInfo pageInfo
) {
List
<User> users
= new ArrayList<>();
User user
= null
;
try {
conn
= DBUtils
.getConn();
ps
= conn
.prepareStatement("select * from t_user limit ?,?");
ps
.setInt(1,pageInfo
.getBeginRow());
ps
.setInt(2,pageInfo
.getPageSize());
rs
= ps
.executeQuery();
while(rs
.next()){
user
= new User();
user
.setUserName(rs
.getString("user_name"));
users
.add(user
);
}
} catch (SQLException e
) {
e
.printStackTrace();
}
return users
;
}
转载请注明原文地址: https://lol.8miu.com/read-1205.html