1.基本思路
我现阶段的分页查询的实现是基于sql语句的。
select * from user where id limit a, b 构造出相应的a和b就可以查询出想要的数据,在显示在页面上。重点是要构造出当前的页数,就要封装一个javaBean,存储有关分页的基本属性。
这样只需在service层计算想要的页数,并封装基本的信息,在查询出来显示在前端就可以了。
2.具体实现
1.定义JavaBean
public @Data class PageBean implements Serializable { private Integer page;//当前页数 private Integer limit;//每页显示数 private Integer totalPage;//总页数 private Integer total;//总记录数 private List pageRecode;//当前页面的数据集合 private List pages;//返回页数的集合,用于显示index页面的上一页、下一页 }
2.controller:
PageBean 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 questionList(Integer page) { List list = new ArrayList<>(); PageBean 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 pages = new ArrayList<>(); for(int i=1;i<totalPage+1;i++){ pages.add(i); } pagesinfo.setPages(pages); //5.设置每页的数据集合 List 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 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="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li th:each="page:${pageBean.pages}"><a href="#" th:text="${page}"></a></li>
<li>
<a href="#" aria-label="Next" th:if="${pageBean.totalPage>5}">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
循环取出page。
另一种方式,使用mybatis-generator生成的分页查询方法 1.新建generatorConfig.xml配置文件‘
<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>
注:配置文件的名称固定是generatorConfig.xml
2.在控制台运行命令mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate生成带有分页查询方法的代码 注:overwrite=true会覆盖掉前一次生成的代码,可根据实际需求进行调整。
3.举例:
QuestionExample example = new QuestionExample(); example.createCriteria(). andCreatorEqualTo(id); List questions = questionMapper.selectByExampleWithRowbounds(example, new RowBounds(page,limit)); 设置好页数和每一页显示的个数,就可以返回对应的结果集。也同样可以做到分页。