关于Mybatis的pagehelper使用遇到的坑
参考博客: https://blog.csdn.net/wzyxdwll/article/details/66473466
下面给出pagehelp使用的配置, 在springmvc中的配置:
下面是maven中配置pagehelp:
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version> <!--我这里用的是5.1.2版本-->
</dependency>
下面是spring-mybatis.xml的配置文件:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/demo/mapping/*.xml"></property>
<property name="configLocation" value="classpath:conf/mybatis-config.xml"></property>
</bean>
下面是mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
<setting name="cacheEnabled" value="true" />
</settings>
<typeAliases>
<typeAlias alias="Blog" type="com.demo.pojo.Blog" />
</typeAliases>
<plugins>
<!--注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor-->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--<property name="helperDialect" value="mysql" />-->
<property name="reasonable" value="true" />
</plugin>
</plugins>
</configuration>
下面是BlogMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.demo.dao.BlogMapper" >
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />
<resultMap id="BaseResultMap" type="com.demo.pojo.Blog" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="authorid" property="authorid" jdbcType="INTEGER" />
<result column="title" property="title" jdbcType="VARCHAR" />
<result column="creattime" property="creattime" jdbcType="VARCHAR" />
</resultMap>
<resultMap id="ResultMapWithBLOBs" type="com.demo.pojo.Blog" extends="BaseResultMap" >
<result column="mainbody" property="mainbody" jdbcType="LONGVARCHAR" />
</resultMap>
<select id="selectAllByList" resultMap="ResultMapWithBLOBs">
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from blog
</select>
</mapper>
下面是BlogMapper.java:
package com.demo.dao;
import com.demo.pojo.Blog;
public interface BlogMapper {
//注意这里返回的类型是List, 而我们在BlogMapper.xml中写的返回类型是ResultMapWithBLOBs类型
List<Blog> selectAllByList();
}
下面是BlogService.java:
package com.demo.service;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.demo.dao.BlogMapper;
import com.demo.pojo.Blog;
@Service(value="blogService")
public class BlogService {
@Resource
private BlogMapper blogMapper;
public List<Blog> selectBlogByList() {
List<Blog> blog = this.blogMapper.selectAllByList();
return blog;
}
}
下面是BlogController.java:
package com.demo.controller; import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.demo.pojo.Blog;
import com.demo.service.BlogService; @Controller
@RequestMapping(value = "/blog")
public class BlogController {
private static final Logger LOG = LogManager.getLogger(BlogController.class);
@Autowired
private BlogService blogService;
@RequestMapping(value="/pagehelper", method = RequestMethod.GET)
public String blogPageHelper(HttpServletRequest request, @RequestParam(required=true,defaultValue="1") Integer page,
@RequestParam(required=false,defaultValue="4") Integer pageSize, ModelMap model) {
// 我这里起始的默认值是第1页, 每页的大小是4个, 读者可以根据需要改变
LOG.info("page="+page+",pageSize="+pageSize); // 开始的打印结果: page=1,pageSize=4, 之后的page根据传入的值变化 PageHelper.startPage(page, pageSize); //开始起始页
List<Blog> blogList = blogService.selectBlogByList(); // 获取数据
PageInfo<Blog> p = new PageInfo<Blog>(blogList); // 实例化PageInfo
model.addAttribute("blogList", blogList);
model.addAttribute("page", p); // 设置属性到前端页面
return "blog/pagehelper";
}
}
下面是前端的页面,即pagehelper.html:
前端页面我使用的是thymeleaf框架, 读者可以参考我的另一篇博客: thymeleaf 模板语言简介
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<script type="text/javascript" th:src="@{/static/js/jquery-1.11.3.min.js}"></script>
<script type="text/javascript" th:src="@{/static/js/springDemo.js}"></script>
<link rel="stylesheet" th:href="@{/static/css/springDemo.css}" />
<title>SpringMVC + Mybatis + Spring</title>
</head>
<body>
<div>
<table class="example">
<caption>Blog</caption>
<thead>
<tr>
<th>number</th>
<th>index</th>
<th>id</th>
<th>title</th>
<th>mainbody</th>
<th>creattime</th>
</tr>
</thead>
<tbody th:remove="all-but-first">
<tr th:each="blog,blogStat:${blogList}">
<td th:text="${blogStat.count}">1</td>
<td th:text="${blogStat.index}">0</td>
<td th:text="${blog.id}">title</td>
<td th:text="${blog.title}">title</td>
<td th:text="${blog.mainbody}">mainbody</td>
<td th:text="${blog.creattime}">creattime</td>
</tr>
</tbody>
<tbody th:remove="all-but-first"> <!--我这里ui显示比较简单,就是为了方便显示-->
<tr>
<td colspan="2"><p th:text="'Total:' + ${page.pages}">Total page</p></td>
<td><a th:href="@{/blog/pagehelper(page=1)}">first</a></td>
<td><a th:href="@{/blog/pagehelper(page=${page.nextPage})}">next</a></td>
<td><a th:href="@{/blog/pagehelper(page=${page.prePage})}">prex</a></td>
<td><a th:href="@{/blog/pagehelper(page=${page.lastPage})}">last</a></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
下面是运行后的界面显示效果:

在使用pagehelper时候, 发现出现下面问题:
14:18:54.095 [localhost-startStop-1] DEBUG org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
2018-04-22 14:18:54,103 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [conf/spring-mybatis.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Object[]' to required type 'org.apache.ibatis.plugin.Interceptor[]' for property 'plugins'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'com.github.pagehelper.PageHelper' to required type 'org.apache.ibatis.plugin.Interceptor' for property 'plugins[0]': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:591)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
解决办法就是: 把mybatis-config.xml中下面的更改过来
<plugins>
<!--注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor-->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql" />
<property name="reasonable" value="true" />
</plugin>
</plugins>
还有一个小坑需要注意的是,在5.0以后的版本,使用了helperDialect代替了原来的dialect属性
下面把PageHelper 5.0与以前版本不一样的地方给贴出来。
- 使用 QueryInterceptor 规范 处理分页逻辑 新的分页插件拦截器为
com.github.pagehelper.PageInterceptor 新的 PageHelper 是一个特殊的 Dialect - 实现类,以更友好的方式实现了以前的功能 新的分页插件仅有 dialect 一个参数,默认的 dialect 实现类为 PageHelper
- PageHelper 仍然支持以前提供的参数,在最新的使用文档中已经全部更新 PageHelper 的 helperDialect 参数和以前的 dialect 功能一样,具体可以看文档的参数说明
- 增加了基于纯 RowBounds 和 PageRowBounds 的分页实现,在com.github.pagehelper.dialect.rowbounds 包中,这是用于作为 dialect 参数示例的实现,后面会补充更详细的文档 去掉了不适合出现在分页插件中的 orderby功能,以后会提供单独的排序插件
- 去掉了PageHelper 中不常用的方法新的文档,更新历来更新日志中提到的重要内容,提供英文版本文档 解决 bug 将 Db2RowDialect 改为Db2RowBoundsDialect 所有分页插件抛出的异常改为 PageException
关于Mybatis的pagehelper使用遇到的坑的更多相关文章
- Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件
上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...
- SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页
SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...
- spingBoot整合mybatis+generator+pageHelper
spingBoot整合mybatis+generator+pageHelper 环境/版本一览: 开发工具:Intellij IDEA 2018.1.4 springboot: 2.0.4.RELEA ...
- Mybatis的PageHelper分页插件的PageInfo的属性参数,成员变量的解释,以及页面模板
作者:个人微信公众号:程序猿的月光宝盒 //当前页 private int pageNum; //每页的数量 private int pageSize; //当前页的数量 private int si ...
- MySQL高效分页-mybatis插件PageHelper改进
MySQL分页在表比较大的时候,分页就会出现性能问题,MySQL的分页逻辑如下:比如select * from user limit 100000,10 它是先执行select * from user ...
- springboot mybatis的pagehelper分页
maven repositary里,分页组件常用的有两个 com.github.pagehelper » pagehelper-spring-boot-starter com.github.page ...
- 62. mybatis 使用PageHelper不生效【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 在Spirng Boot中集成了PageHelper,然后也在需要使用分页的地方加入了如下代码: PageHelper.startPage(1,1 ...
- spring整合mybatis使用<context:property-placeholder>时的坑
背景 最近项目要上线,需要开发一个数据迁移程序.程序的主要功能就是将一个数据库里的数据,查询出来经过一系列处理后导入另一个数据库.考虑到开发的方便快捷.自然想到用spring和mybatis整合一下. ...
- Spring + Mybatis 使用 PageHelper 插件分页
原文:http://www.cnblogs.com/yucongblog/p/5330886.html 先增加maven依赖: <dependency> <groupId>co ...
随机推荐
- 使用python内置模块os和openpyxl搜索指定文件夹下Excel中的内容
在指定路径下,搜索Excel文件中包含的指定内容,首先需要遍历指定路径,得到该路径下所有Excel文件的绝对/相对路径:然后读取Excel中内容,将文件中的每个单元格的值与要搜索的内容进行判断(正则比 ...
- 8、scala面向对象编程之Trait
一.Trait基础 1.将trait作为接口使用 // Scala中的Triat是一种特殊的概念 // 首先我们可以将Trait作为接口来使用,此时的Triat就与Java中的接口非常类似 // 在t ...
- Java静态检测工具/Java代码规范和质量检查简单介绍(转)
静态检查: 静态测试包括代码检查.静态结构分析.代码质量度量等.它可以由人工进行,充分发挥人的逻辑思维优势,也可以借助软件工具自动进行.代码检查代码检查包括代码走查.桌面检查.代码审查等,主要检查代码 ...
- git branch简单使用
1,branch的建立及使用git clone user@192.168.0.136:/media/projiect/omap4/nexttab/kernel kernel/android3.0/ ...
- Vue 全家桶单元测试简要指南
此文已由作者张汉锐授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. vue 的单元测试环境 按照目前全家桶的情况,是需要 webpack 的支持的.可以直接使用 vue-cli ...
- VIM命令图---可定制版
近期闲来无事,便自己用PS制作了一份VIM命令助记图,之前看到网上有类似的图片,可是有些解释感觉不太好,并有一些错误的地方,所以就自己做了一份,并且保留了PS源文件,为了方便会PS的网友可以对本图进行 ...
- MATLAB进行无约束优化
首先先给出三个例子引入fminbnd和fminuc函数求解无约束优化,对这些函数有个初步的了解 求f=2exp(-x)sin(x)在(0,8)上的最大.最小值. 例2 边长3m的正方形铁板,四角减去相 ...
- Yahoo Progamming Contest 2019D(DP,思维)
#include<bits/stdc++.h>using namespace std;long long n,v,a,b,c,d,e;int main(){ scanf(" ...
- Unity3D -- shader语法内置函数
该篇是Unity Shader中HLSL的内置函数,主要是一些数学方面的计算函数.在写Shader的时候可以直接使用. abs //计算输入值的绝对值. acos //返回输入值反余弦值. all / ...
- tomcat mamcached session共享方法
下载后输入命令安装命令: c:\memcached\memcached.exe -d install 然后再输入如下命令把其作为win service常驻启动: c:\memcached\memcac ...