Springboot结合PageHelper实现55分页
PageHelper结合Spring boot快速实现分页查询
MyBatis 分页插件 PageHelper
如果你也在用 MyBatis,建议尝试该分页插件,这一定是最方便使用的分页插件。分页插件支持任何复杂的单表、多表分页。
但在其官方文档中,现阶段并没有看到此插件与spring boot的结合使用说明。如何使用分页插件 (pagehelper.github.io)
本篇博客实现Spring boot结合MyBatis 分页插件 PageHelper实现分页效果:
后端代码:
创建实体类:(HistoryLog,不多赘述)
service层:
import java.util.List;
public interface HistoryLogService {
/**
* 分页查询接口
* 这里统一封装了分页请求和结果,避免直接引入具体框架的分页对象, 如MyBatis或JPA的分页对象
* 从而避免因为替换ORM框架而导致服务层、控制层的分页接口也需要变动的情况,替换ORM框架也不会
* 影响服务层以上的分页接口,起到了解耦的作用
*/
Page<HistoryLog> QueryHistory2();
}
mapper持久层:
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface HistoryLogMapper {
// 分页查询用户浏览日志
@Select("select content_id,people_id,hl_ip,date_format(create_date, '%Y-%c-%d %h:%i:%s' ) create_date from cms_history_log where people_id is not null ORDER BY create_date desc ")
@Results({
@Result(property = "content_id", column = "content_id"),
@Result(property = "people_id", column = "people_id"),
@Result(property = "hl_ip", column = "hl_ip"),
@Result(property = "create_date", column = "create_date")
})
Page<HistoryLog> queryHistoryLog2();
}
实现类:
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HistoryLogServiceImpl implements HistoryLogService {
@Autowired
HistoryLogMapper historyLogMapper;
@Override
public Page<HistoryLog> QueryHistory2() {
return historyLogMapper.queryHistoryLog2();
}
}
控制层:
package net.mingsoft.front.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@RestController
public class HistoryLogController {
@Autowired
HistoryLogServiceImpl historyLogServiceimpl;
// 分页查询
@RequestMapping("ms/userlog/pagehelper")
public Object getUserList2(int pagenum) {
PageHelper.startPage(pagenum, 15);//设置跳转到第几页以及每页显示15条数据
List<HistoryLog> list = historyLogServiceimpl.QueryHistory2();
PageInfo<HistoryLog> pageInfo = new PageInfo<HistoryLog>(list);//将查询数据存到PageInfo中
ModelAndView mv = new ModelAndView();
mv.addObject("historyLog",pageInfo);
mv.setViewName("../WEB-INF/manager/cms/userLog/index1.html");
return mv;
}
}
前端代码:结合themeleaf模板,采用bootstrap组件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户浏览日志</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="table-responsive">
<table class="table">
<!-- <caption>用户浏览日志</caption>-->
<thead>
<tr>
<th>用户ID</th>
<th>IP访问地址</th>
<th>访问的文章ID</th>
<th>创建时间</th>
</tr>
</thead>
<tbody>
<tr th:each="history,historyinfo:${historyLog.list}">
<td th:text="${history.people_id}">people_id</td>
<td th:text="${history.hl_ip}">hl_ip</td>
<td th:text="${history.content_id}">content_id</td>
<td th:text="${history.create_date}">create_date</td>
</tr>
</tbody>
</table>
</div>
<nav aria-label="Page navigation" style="