SpringMVC+Mybatis实现分页

本文档详细介绍了如何使用SpringMVC和Mybatis框架实现分页查询功能,涉及实体类、UserMapper.xml配置、接口定义、Service及Impl实现、Controller层控制以及前端HTML页面的创建和展示效果。
摘要由CSDN通过智能技术生成

实现分页查询

实体类

package com.approval.bean;

import java.io.Serializable;
import java.util.List;

import org.apache.poi.ss.formula.functions.T;

public class Page<T> implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	//每页显示多少条:默认10条
	private int pageSize = 10;
	//总页数
	private int pageTotal;
	//当前第多少页:默认第1页
	private int pageNum = 1;
	//总共多少条
	private int totalCount;
	//要显示的数据
	private List<T> dataList;
	
	//分页方法
	public Page(int pageNumumber, int pageSize, List<T> sourceList){
        if(sourceList == null || sourceList.isEmpty()){
            return;
        }

        // 总记录条数
        this.totalCount = sourceList.size();

        // 每页显示多少条记录
        this.pageSize = pageSize;

        //获取总页数
        this.pageTotal = this.totalCount / this.pageSize;
        if(this.totalCount % this.pageSize !=0){
            this.pageTotal = this.pageTotal + 1;
        }
        
        // 当前第几页数据
        this.pageNum = this.pageTotal < pageNum ?  this.pageTotal : pageNum;

        // 起始索引
        int fromIndex   = this.pageSize * (this.pageNum -1);

        // 结束索引
        int toIndex  = this.pageSize * this.pageNum > this.totalCount ? this.totalCount : this.pageSize * this.pageNum;

        this.dataList = sourceList.subList(fromIndex, toIndex);
    }

    public Page(){

    }

    public Page(int pageSize,int pageNum,int totalCount,int pageTotal,List<T> dataList) {
        super();
        this.pageSize = pageSize;
        this.pageNum = pageNum;
        this.totalCount = totalCount;
        this.pageTotal = pageTotal;
        this.dataList = dataList;
    }

	
	public int getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public int getPageTotal() {
		return pageTotal;
	}
	
	public void setPageTotal(int pageTotal) {
		this.pageTotal = pageTotal;
	}
	
	public int getPageNum() {
		return pageNum;
	}
	
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	
	public int getTotalCount() {
		return totalCount;
	}
	
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	public List<T> getDataList() {
		return dataList;
	}
	
	public void setDataList(List<T> dataList) {
		this.dataList = dataList;
	}
}

配置.xml文件:UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.approval.mapper.UserMapper">
     <resultMap id="BaseResultMap" type="com.approval.bean.User">
		<id column="user_id" property="userId" jdbcType="INTEGER" />
		<result column="user_code" property="userCode" jdbcType="VARCHAR" />
		<result column="user_n
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值