如何用java实现分页查询
作者:'买条秋裤穿 时间:2023-09-23 06:50:24
1.基本思路
我现阶段的分页查询的实现是基于sql语句的。
select * from user where id limit a, b
构造出相应的a和b就可以查询出想要的数据,在显示在页面上。重点是要构造出当前的页数,就要封装一个javaBean,存储有关分页的基本属性。
这样只需在service层计算想要的页数,并封装基本的信息,在查询出来显示在前端就可以了。
2.具体实现
1.定义JavaBean
public @Data
class PageBean<T> implements Serializable {
private Integer page;//当前页数
private Integer limit;//每页显示数
private Integer totalPage;//总页数
private Integer total;//总记录数
private List<T> pageRecode;//当前页面的数据集合
private List<Integer> pages;//返回页数的集合,用于显示index页面的上一页、下一页
}
2.controller:
PageBean<QuestionDTO> pageBean = questionService.questionList(page);
返回一个QuestionDTO类型的JavaBean,其中包含了分页的一些信息和当前页面所要显示的数据集合。有关QuestionDTO:
public @Data
class QuestionDTO {
private Integer id;
private String title;
private String description;
private Long gmtCreate;
private Long GmtModified;
private Integer creator;
private Integer attentionCount;
private Integer viewCount;
private Integer likeCount;
private String tag;
private User user;
}
3.调用的Service:
//查询所有的问题回显到index页面
public PageBean<QuestionDTO> questionList(Integer page) {
List<QuestionDTO> list = new ArrayList<>();
PageBean<QuestionDTO> pagesinfo = new PageBean<>();
//1.设置limit
Integer limit = 5;
pagesinfo.setLimit(limit);
//2.设置总记录数
Integer total = questionMapper.fingCount();
pagesinfo.setTotal(total);
//3.设置总的页数
Integer totalPage;
if(total % limit == 0){
totalPage = total / limit;
}else{
totalPage = total / limit + 1;
}
pagesinfo.setTotalPage(totalPage);
//4.设置页数的集合
List<Integer> pages = new ArrayList<>();
for(int i=1;i<totalPage+1;i++){
pages.add(i);
}
pagesinfo.setPages(pages);
//5.设置每页的数据集合
List<Question> questions = questionMapper.questionList(page,limit);
for(Question question : questions){
User user = userMapper.findById(question.getCreatar());
QuestionDTO questionDTO = new QuestionDTO();
BeanUtils.copyProperties(question,questionDTO);
questionDTO.setUser(user);
list.add(questionDTO);
}
pagesinfo.setPageRecode(list);
return pagesinfo;
}
在service层为PageBean的属性赋值,并且查询出相关的数据。上述代码中第5步如果有疑惑请参照多表联合查询的简单另类的实现方式。
4.mapper
//查询所有的问题并回显到index页面
@Select("select * from question where id limit #{page},#{limit}")
List<Question> questionList(Integer page, Integer limit);
//查询总的问题数
@Select("select count(id) from question")
Integer fingCount();
做完这些,controller中的PageBean中就会封装有查询的数据。在返回前端显示就完成了。
5.前端代码
<!-- 分页 -->
<nav aria-label="Page navigation" th:align="right">
<ul class="pagination">
<li th:if="${pageBean.totalPage>5 || pageBean.totalPage==1}">
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li th:each="page:${pageBean.pages}"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" th:text="${page}"></a></li>
<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Next" th:if="${pageBean.totalPage>5}">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
循环取出page。
另一种方式,使用mybatis-generator生成的分页查询方法
1.新建generatorConfig.xml配置文件‘
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 生成带有分页方法的插件-->
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
<!-- 连接数据库的基本信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&serverTimezone=UTC"
userId="root"
password="root">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成的实体类
targetPackage:实体类存放的包名
targetProject:项目地址(到java)
-->
<javaModelGenerator targetPackage="cn.fzkj.community.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成的xml映射文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成的mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.fzkj.community.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 表名,想生成的实体类的名称 -->
<table tableName="question" domainObjectName="Question" ></table>
<table tableName="user" domainObjectName="User" ></table>
<table tableName="comment" domainObjectName="Comment" ></table>
<table tableName="notification" domainObjectName="Notification" ></table>
</context>
</generatorConfiguration>
注:
1.配置文件的名称固定是generatorConfig.xml
2.在控制台运行命令mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate生成带有分页查询方法的代码
注:overwrite=true会覆盖掉前一次生成的代码,可根据实际需求进行调整。
3.举例:
QuestionExample example = new QuestionExample();
example.createCriteria().
andCreatorEqualTo(id);
List<Question> questions = questionMapper.selectByExampleWithRowbounds(example, new RowBounds(page,limit));
设置好页数和每一页显示的个数,就可以返回对应的结果集。也同样可以做到分页。
持续更新~~~
来源:https://www.cnblogs.com/mrjkl/p/13051535.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#验证给定字符串是否为数字的方法
java读取ftp中TXT文件的案例
![](https://img.aspxhome.com/file/2023/1/101771_0s.jpg)
Android TabHost组件使用方法详解
![](https://img.aspxhome.com/file/2023/3/114373_0s.jpg)
System 类 和 Runtime 类的常用用法介绍
Java虚拟机栈jvm栈的作用
![](https://img.aspxhome.com/file/2023/8/129268_0s.png)
Android中View的炸裂特效实现方法详解
C++内存池的简单实现
![](https://img.aspxhome.com/file/2023/9/109769_0s.png)
C#中csv文件与DataTable互相导入处理实例解析
![](https://img.aspxhome.com/file/2023/8/126128_0s.png)
java通过PDF模板填写PDF表单
![](https://img.aspxhome.com/file/2023/1/66651_0s.jpg)
ubuntu环境下反编译android apk的方法
![](https://img.aspxhome.com/file/2023/4/129054_0s.png)
C#实现文件上传与下载功能实例
Android实战教程第六篇之一键锁屏应用问题解决
![](https://img.aspxhome.com/file/2023/5/93325_0s.jpg)
详解Maven settings.xml配置(指定本地仓库、阿里云镜像设置)
![](https://img.aspxhome.com/file/2023/9/65799_0s.png)
java 二叉查找树实例代码
Android自定义View绘图实现渐隐动画
![](https://img.aspxhome.com/file/2023/2/139092_0s.gif)
Android的UI调优教程
![](https://img.aspxhome.com/file/2023/0/137620_0s.png)
android实现扫码枪功能
Java使用动态规划算法思想解决背包问题
![](https://img.aspxhome.com/file/2023/7/89497_0s.png)
Kotlin定义其他类的实现详解
![](https://img.aspxhome.com/file/2023/3/138163_0s.png)