当前位置: 首页 > news >正文

【mybatis】实现分页查询

一 .使用原生分页器的实体类

        1.1 java代码部分

                方法多  不易书写

package cn.bdqn.entity;public class Page {private Integer pageIndex;//页码private Integer pageSize;//页大小  显示多少行数据private Integer totalCounts;//数据的总行数private Integer totalPages;//总页数private Integer startRows;//起始行private String sname="";private String grade="";private String sex="";public Page() {}public Page(Integer pageIndex) {this(pageIndex, 7);}public Page(Integer pageIndex, Integer pageSize, Integer totalCounts, Integer totalPages, Integer startRows, String snane, String sex, String grade) {this.pageIndex = pageIndex;this.pageSize = pageSize;this.totalCounts = totalCounts;this.totalPages = totalPages;this.startRows = startRows;this.sname = sname;this.sex = sex;this.grade = grade;}public Page(Integer pageIndex, Integer pageSize) {this.pageIndex = pageIndex;this.pageSize = pageSize;this.setStartRows((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 getTotalCounts(int count) {return totalCounts;}public void setTotalCounts(Integer totalCounts) {this.totalCounts = totalCounts;this.setTotalPages(totalCounts % pageSize == 0 ? totalCounts / pageSize : totalCounts / pageSize + 1);}public Integer getTotalPages() {return totalPages;}public void setTotalPages(Integer totalPages) {this.totalPages = totalPages;}public Integer getStartRows() {return startRows;}public void setStartRows(Integer startRows) {this.startRows = startRows;}//    public int getPageNum() {
//        return pageNum;
//    }
//public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}public String getSex() {return sex;}public void setSex(String grade) {this.sex = sex;}
//    @Override
//    public String toString() {
//        return "Page{" +
//                "pageIndex=" + pageIndex +
//                ", pageSize=" + pageSize +
//                ", totalCounts=" + totalCounts +
//                ", totalPages=" + totalPages +
//                ", startRows=" + startRows +
//                '}';
//    }@Overridepublic String toString() {return "Page{" +"pageIndex=" + pageIndex +", pageSize=" + pageSize +", totalCounts=" + totalCounts +", totalPages=" + totalPages +", startRows=" + startRows +", sname='" + sname + '\'' +", sex='" + sex + '\'' +", grade='" + grade + '\'' +'}';}
}

        1.2 在动态jsp页面中    用forEach  书写

<div class="a-run"><a href="<c:url context='${pageContext.request.contextPath}' value='/ServletScoreShow?pageIndex=1'/>">首页</a><c:if test="${page.pageIndex > 1}"><a href="<c:url context='${pageContext.request.contextPath}' value='/ServletScoreShow?pageIndex=${page.pageIndex - 1}' />">上一页</a></c:if><c:if test="${page.pageIndex == 1}"><a>上一页</a></c:if><c:if test="${page.pageIndex < page.totalPages}"><a href="<c:url context='${pageContext.request.contextPath}' value='/ServletScoreShow?pageIndex=${page.pageIndex + 1}'/>">下一页</a></c:if><c:if test="${page.pageIndex == page.totalPages}"><a>下一页</a></c:if><a href="<c:url context='${pageContext.request.contextPath}' value='/ServletScoreShow?pageIndex=${page.totalPages}'/>">尾页</a></div>

 二 . 使用mybatis  分页

             2.1.1  在pom.xml文件中引入依赖

       

 <!--引入分页插件--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.3.2</version></dependency>
</dependencies>

                2.1.2  在mybatis.xml中   设定标签(注意标签书写顺序)

<!--配置mybatis分页插件-->
<plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

三 . 测试类代码

package cn.sy;import cn.sy.entity.User;
import cn.sy.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class TestPageHelper {SqlSessionFactory factory = null;@Beforepublic void init() {try {InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis-config.xml");factory = new SqlSessionFactoryBuilder().build(resourceAsStream);} catch (IOException e) {e.printStackTrace();}}@Testpublic void test(){SqlSession sqlSession = factory.openSession(true);UserMapper mapper = sqlSession.getMapper(UserMapper.class);//pageNum : 分的页数  pageSize: 每页显示的记录数PageHelper.startPage(3,3);List<User> users = mapper.selectList();for (User user : users) {System.out.println("user = " + user);}// 创建分页信息对象  获取更多的分页信息PageInfo<User> userPageInfo = new PageInfo<>(users);// 获取分页的详细信息List<User> list = userPageInfo.getList();System.out.println("list = " + list);long total = userPageInfo.getTotal();System.out.println("total = " + total);System.out.println("*********************");System.out.println("userPageInfo = " + userPageInfo);}
}

         

 

                 

http://www.lryc.cn/news/2676.html

相关文章:

  • CF1560D Make a Power of Two 题解
  • C#开发的OpenRA的读取文件的函数
  • SpringBoot结合XXL-JOB实现定时任务
  • 【Node.js】 创建web服务器
  • 基于go语言实现RestFul交互
  • 情感溢出:读《浣溪沙》
  • 深入解读.NET MAUI音乐播放器项目(一):概述与架构
  • 【Python小游戏】某程序员将套圈游戏玩儿到了巅峰,好嗨哟~Pygame代码版《牛牛套圈》已上线,大人的套圈游戏太嗨了,小孩勿进。
  • php的declare命令如何使用?
  • 嵌软工程师要掌握的硬件知识2:一文看懂什么开漏和推挽电路(open-drain / push-pull)
  • 1.2.6存储结构-磁盘管理:单缓冲区与双缓冲区读取、流水线周期、计算流水线执行时间
  • 【pytest接口自动化测试】结合单元测试框架pytest+数据驱动模型+allure
  • 展锐平台WIFI吞吐问题解决方案
  • 全局向量的词嵌入(GloVe)
  • 华为OD机试 - 特异性双端队列(Python),真题含思路
  • 【Linux】操作系统进程概念
  • C语言const的用法详解
  • Day886.MySQL的“饮鸩止渴”提高性能的方法 -MySQL实战
  • 08- 数据升维 (PolynomialFeatures) (机器学习)
  • 2023备战金三银四,Python自动化软件测试面试宝典合集(二)
  • 笔试题-2023-紫光展锐-数字芯片设计【纯净题目版】
  • WordPress网站日主题Ri主题RiProV2主题开启了验证码登录但是验证码配置不对结果退出登录后进不去管理端了
  • 自动驾驶感知——毫米波雷达
  • 取电芯片全协议都可兼容
  • 自己总结优化代码写法
  • Java体系最强干货分享—挑战40天准备Java面试,最快拿到offer!
  • 云计算|OpenStack|错误记录和解决方案(不定时更新)
  • 项目实战-NewFixedThreadPool线程池
  • 导数与微分总复习——“高等数学”
  • Linux软件安装