java编写一个分页类

主要是帮助大家理解分页功能,脱离了很多分页控件我们如果实现分页效果。

首先 分页我们知道要有上一页 下一页 总页数 当前页 这些信息,那么我们定义分页类如下:

package com.deom.commons;
import java.util.List;
/**
 * 分页功能
 * 
 * @author spdai
 * 
 */
public class Page {
// 总的记录数
private List records;
// 当前页面
private int currentPageNum;
// 总页数
private int totalPage;
// 页的大小
private int pageSize = 10;
// 总的记录条数
private int totalRecords;
// 开始分页的位置
private int startIndex;
// 上一页
private int prePageNum;
// 下一页
private int nextPageNum;
// 开始页码
private int startPage;
// 结束页码
private int endPage;
// 设置跳转页的url
private String uri;


/**
* 初始化页面 只需要 转入当前页 总记录条数

* @param currentPageNum
* @param totalRecords
*/
public Page(int currentPageNum, int totalRecords) {
this.currentPageNum = currentPageNum;
this.totalRecords = totalRecords;
this.totalPage = totalRecords % pageSize == 0 ? totalRecords / pageSize
: totalRecords / pageSize + 1;
this.startIndex = (this.currentPageNum - 1) * pageSize;
if (totalPage > 5) {
startPage = currentPageNum - 2;
endPage = currentPageNum + 2;


if (startPage < 1) {
startPage = 1;
endPage = 5;
}
if (endPage > totalPage) {
endPage = totalPage;
startPage = totalPage - 4;
}


} else {


startPage = 1;
endPage = totalPage;
}


}


/**
* @return the records
*/
public List getRecords() {
return records;
}


/**
* @param records
*            the records to set
*/
public void setRecords(List records) {
this.records = records;
}


/**
* @return the totalPage
*/
public int getTotalPage() {
return totalPage;
}


/**
* @param totalPage
*            the totalPage to set
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}


/**
* @return the pageSize
*/
public int getPageSize() {
return pageSize;
}


/**
* @param pageSize
*            the pageSize to set
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}


/**
* @return the totalRecords
*/
public int getTotalRecords() {
return totalRecords;
}


/**
* @param totalRecords
*            the totalRecords to set
*/
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}


/**
* @return the startIndex
*/
public int getStartIndex() {
return startIndex;
}


/**
* @param startIndex
*            the startIndex to set
*/
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}


/**
* @return the prePageNum
*/
public int getPrePageNum() {
this.prePageNum = this.currentPageNum - 1;
if (this.prePageNum < 1) {
this.prePageNum = 1;
}
return this.prePageNum;


}


/**
* @param prePageNum
*            the prePageNum to set
*/
public void setPrePageNum(int prePageNum) {
this.prePageNum = prePageNum;
}


/**
* @return the nextPageNum
*/
public int getNextPageNum() {
nextPageNum = currentPageNum + 1;
if (nextPageNum > totalPage)
nextPageNum = totalPage;
return nextPageNum;
}


/**
* @param nextPageNum
*            the nextPageNum to set
*/
public void setNextPageNum(int nextPageNum) {
this.nextPageNum = nextPageNum;
}


/**
* @return the startPage
*/
public int getStartPage() {
return startPage;
}


/**
* @param startPage
*            the startPage to set
*/
public void setStartPage(int startPage) {
this.startPage = startPage;
}


/**
* @return the endPage
*/
public int getEndPage() {
return endPage;
}


/**
* @param endPage
*            the endPage to set
*/
public void setEndPage(int endPage) {
this.endPage = endPage;
}


/**
* @return the uri
*/
public String getUri() {
return uri;
}
/**
* @param uri
*            the uri to set
*/
public void setUri(String uri) {
this.uri = uri;
}
/**
* @return the currentPageNum
*/
public int getCurrentPageNum() {
return currentPageNum;
}
/**
* @param currentPageNum
*            the currentPageNum to set
*/
public void setCurrentPageNum(int currentPageNum) {
this.currentPageNum = currentPageNum;
}
}


dao层调用方法:

@Override
public int getTotalRecords() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement("select count(*) from customers");
rs = stmt.executeQuery();
if(rs.next()){
return rs.getInt(1);
}
return 0;
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}


@Override
public List findPageRecords(int startIndex, int pageSize) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement("select * from customers limit ?,?");
stmt.setInt(1, startIndex);
stmt.setInt(2, pageSize);
rs = stmt.executeQuery();
List<Customer> cs = new ArrayList<Customer>();
while(rs.next()){
Customer c = new Customer();
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setBirthday(rs.getDate("birthday"));
c.setCellphone(rs.getString("cellphone"));
c.setEmail(rs.getString("email"));
c.setHobby(rs.getString("hobby"));
c.setType(rs.getString("type"));
c.setDescription(rs.getString("description"));
cs.add(c);
}

return cs;
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}

jsp页面使用:

   
    第${page.currentPageNum}页/共${page.totalPage}页&nbsp;&nbsp;
    <a href="${pageContext.request.contextPath}">首页</a>
    <a href="${pageContext.request.contextPath}${page.uri}&num=${page.prePageNum}">上一页</a>
    &nbsp;&nbsp;
    
    <c:forEach begin="${page.startPage}" end="${page.endPage}" var="num">
    <a href="${pageContext.request.contextPath}${page.uri}&num=${num}">${num}</a>
    </c:forEach>
    
    &nbsp;&nbsp;
    <a href="${pageContext.request.contextPath}${page.uri}&num=${page.nextPageNum}">下一页</a>
    <a href="${pageContext.request.contextPath}${page.uri}&num=${page.totalPage}">尾页</a>
    
    <select id="pagenum" οnchange="jump(this)">
    <c:forEach begin="1" end="${page.totalPage}" var="num">
    <option value="${num}" ${num==page.currentPageNum?'selected="selected"':'' }>${num}</option>
    </c:forEach>
    </select>
    &nbsp;&nbsp;
    
    <input type="text" id="newnum" size="2"/>
    <input type="button" value="跳转" οnclick="jump1()"/>
    
    <script type="text/javascript">
    function jump(selectObj){
    window.location.href="${pageContext.request.contextPath}${page.uri}&num="+selectObj.value;
    }
    function jump1(){
    var newnum = document.getElementById("newnum").value;
    //验证
   
    //验证必须是一个自然整数
    if(!/^[1-9][0-9]*$/.test(newnum)){
    alert("请输入正确的页码");
    return;
    }
   
    //范围不能超过1~最大页数
    if(newnum<1||newnum>${page.totalPage}){
    alert("页码已经超出了范围");
    return;
    }
   
   
    window.location.href="${pageContext.request.contextPath}${page.uri}&num="+newnum;
    }
    </script>

效果如下:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以参考以下示例代码编写一个基于Java和MyBatis Plus的分页查询方法: 首先,您需要在您的mapper接口中定义一个查询方法,如下所示: ```java public interface UserMapper extends BaseMapper<User> { /** * 分页查询用户列表 * @param page 分页参数 * @param username 查询条件-用户名 * @return 用户列表 */ IPage<User> getUserList(Page<User> page, @Param("username") String username); } ``` 在上述代码中,我们定义一个名为getUserList的查询方法,它接受两个参数:分页参数page和查询条件username。getUserList方法的返回值型是IPage<User>,其实现为MyBatis Plus提供的Page。 接下来,我们需要在mapper.xml文件中编写SQL语句,如下所示: ```xml <select id="getUserList" resultType="com.example.entity.User"> SELECT * FROM user WHERE username LIKE CONCAT('%',#{username},'%') </select> ``` 在上述代码中,我们使用SELECT语句查询了user表,在WHERE子句中使用了LIKE运算符实现模糊匹配。 最后,在Service层中调用getUserList方法即可实现分页查询,如下所示: ```java @Service public class UserServiceImpl implements UserService { private final UserMapper userMapper; @Autowired public UserServiceImpl(UserMapper userMapper) { this.userMapper = userMapper; } @Override public IPage<User> getUserList(int pageNum, int pageSize, String username) { Page<User> page = new Page<>(pageNum, pageSize); return userMapper.getUserList(page, username); } } ``` 在上述代码中,我们使用Page创建了一个分页参数page,并将其作为参数传递给了getUserList方法。getUserList方法将返回一个IPage<User>对象,该对象包含了查询结果和分页信息。 以上就是一个基于Java和MyBatis Plus的分页查询方法的实现示例,您可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值