概述
- mybatis分页插件PageHelper支持mysql,oracle,db2,sqlserver等一系列的数据库
- 使用教程:
(1):导入mybatis所使用的架包或者依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
(2):在mybatis的全局配置文件中配置一个PageHelper插件
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- config params as the following -->
<!--使用mysql数据库-->
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
(3):报销页面实现分页
分完页之后的界面是这样子的:
实现代码:
前端:page.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
Created by IntelliJ IDEA.
User: 杨
Date: 2020/7/15
Time: 17:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery-3.3.1.js"></script>
<script type="text/javascript">
function queryList() {
var pageNo=$("#pagenum").val();
var reg=/^[1-9]d*$/;
if(!reg.test(pageNo)){
alert("请输入正确的页码");
return;
}else{
window.location.href="${requestURI}?pageNum="+pageNo+"${queryStr}";
}
}
</script>
<tr align="right" bgcolor="#EEF4EA">
<td height="36" colspan="12" align="center">
<div>
<a href="${requestURI}?pageNum=1${queryStr}">首页</a>
<a href="${requestURI}?pageNum=${page.pageNum-1}${queryStr}">上一页</a>
<c:forEach items="${page.navigatepageNums}" var="pageNum">
<c:if test="${pageNum==page.pageNum}">
<span style="color: black;font-weight: bold">${pageNum}</span>
</c:if>
<c:if test="${pageNum!=page.pageNum}">
<a href="${requestURI}?pageNum=${pageNum}${queryStr}">${pageNum}</a>
</c:if>
</c:forEach>
<a href="${requestURI}/bx/myList?pageNum=${page.pageNum+1}${queryStr}">下一页</a>
<a href="${requestURI}/bx/myList?pageNum=${page.pages}${queryStr}">末页</a>
跳转到<input type="text" size="1px" id="pagenum" onblur="queryList()">页
</div>
</td>
</tr>
mybaoxiao-base.jsp
<jsp:include page="page.jsp"></jsp:include>
后端:
controller层
//我的报销
@RequestMapping(value = "/myList",method = RequestMethod.GET)
public ModelAndView getMyBaoxiaoList(HttpServletRequest request, HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
//传递方式:${pageContext.request.contextPath}/bx/myList?search_status=1&search_keyword=wd
//map的key是浏览器传过来的参数的键去掉指定前缀之后剩下的那部分为status或keyword
//map的value是浏览器传来的参数的值
Map<String, Object> parameterMap = WebUtils.getParametersStartingWith(request, "search_");
String queryStr=parseparameterMapToString(parameterMap);
System.out.println(parameterMap);
Employee loginUser = (Employee)session.getAttribute("loginUser");
String requestURI = request.getRequestURI();
Integer eid = loginUser.getEid();
PageInfo<BaoXiao> page=baoXiaoService.getMyBaoxiaoList(eid,pageNum,parameterMap);
ModelAndView modelAndView = new ModelAndView("mybaoxiao-base");
modelAndView.addObject("page",page);
modelAndView.addObject("queryStr",queryStr);
modelAndView.addObject("requestURI",requestURI);
return modelAndView;
}
private String parseparameterMapToString(Map<String, Object> parameterMap) {
Set<Map.Entry<String, Object>> entries = parameterMap.entrySet();
String str="";
for (Map.Entry<String, Object> entry : entries) {
String key=entry.getKey();
String value=(String)entry.getValue();
str=str+"&"+"search_"+key+"="+value;
}
return str;
}
Service层
//我的报销,分页
@Override
public PageInfo<BaoXiao> getMyBaoxiaoList(Integer eid, Integer pageNum, Map<String, Object> parameterMap) {
BaoXiaoExample example=new BaoXiaoExample();
BaoXiaoExample.Criteria criteria = example.createCriteria();
criteria.andEmpFkEqualTo(eid);
Map<String,String> mybatisMap=parseParameterMapToMyBatisMap(parameterMap);
//获取键值
String status=mybatisMap.get("status");
if (status != null && status != "") {
criteria.andBxstatusEqualTo(Integer.parseInt(status));
}
//获取键值
String keyword=mybatisMap.get("keyword");
if (keyword != null && keyword!="") {
criteria.andBxremarkLike(keyword);
}
//pageNum:从第几页开始,默认从第一页开始,pageSize:每页显示的默认的记录条数
PageHelper.startPage(pageNum,3);
List<BaoXiao> baoXiaos = baoXiaoMapper.selectByExample(example);
//navigatePages:导航栏的页数
PageInfo<BaoXiao> page=new PageInfo<BaoXiao>(baoXiaos,5);
return page;
}
/**
* 区别精确查询和模糊查询
* @param parameterMap
* @return
*/
private Map<String, String> parseParameterMapToMyBatisMap(Map<String, Object> parameterMap) {
Map<String,String> resultMap=new HashMap<String,String>();
Set<Map.Entry<String, Object>> entries = parameterMap.entrySet();
for (Map.Entry<String, Object> entry : entries) {
String key=entry.getKey();
String value=(String)entry.getValue();
if (key.contains("like")){
key=key.substring(key.indexOf("_")+1);
value="%"+value+"%";
}
resultMap.put(key,value);
}
return resultMap;
}
最后
以上就是彪壮可乐为你收集整理的mybatis分页插件PageHelper的全部内容,希望文章能够帮你解决mybatis分页插件PageHelper所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复