Mybatisのpagehelperは出会った穴を使って
以下はmavenにおけるpagehelpの構成である:5.1を用いる.バージョン2
次はspring-mybatisです.xmlのプロファイル:
次はmybatis-configです.xml
次はBlogMapperです.xml:
次はBlogMapperです.java:
次はBlogServicesです.java:
次はBlogControllerです.java:
次はフロントエンドのページ、すなわちpagehelperです.html:フロントエンドページthymeleafフレームワークを使用しています.読者は私の別のブログを参考にすることができます.thymeleafテンプレート言語の概要
次に、実行後のインタフェースの効果を示します.
[外部チェーン画像の転送に失敗しました.ソース局には盗難防止チェーンがある可能性があります.画像を保存して直接アップロードすることをお勧めします(img-asBC 1 woZ-1578014063129)(https://i.imgur.com/xXSwAn9.png)]
pagehelperを使用すると、次の問題が発生します.
解決策はmybatis-configをxmlの下の変更
理由:
もう1つ注意しなければならないのは、5.0以降のバージョンでは、
以下、PageHelper 5.0と以前のバージョンとは異なる箇所を貼り付けます. QueryInterceptor仕様を使用してページングロジックを処理し、新しいページングプラグインブロックは 新しいPageHelperは特殊なDialect実装クラスであり、より友好的な方法で以前の機能を実現した.新しいページングプラグインはdialectのパラメータしかなく、デフォルトのdialect実装クラスはPageHelperである. PageHelperは依然として以前提供したパラメータをサポートし、最新の使用ドキュメントでPageHelperのhelperDialectパラメータがすべて更新されたのは以前のdialect機能と同じで、具体的にはドキュメントのパラメータの説明を見ることができる. は、純RowBoundsとPageRowBoundsに基づくページング実装を追加し、 PageHelperではあまり使われていない方法の新しいドキュメントを削除し、これまでログに記載されていた重要な内容を更新し、英語バージョンのドキュメントを提供し、バグを解決してDb 2 RowDialectをDb 2 RowBoundsDialectに変更し、すべてのページングプラグインが投げ出した異常をPageExceptionに変更した.
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.1.2version>
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
<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>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="reasonable" value="true" />
plugin>
plugins>
configuration>
次はBlogMapperです.xml:
<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();
}
次はBlogServicesです.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 + Springtitle>
head>
<body>
<div>
<table class="example">
<caption>Blogcaption>
<thead>
<tr>
<th>numberth>
<th>indexth>
<th>idth>
<th>titleth>
<th>mainbodyth>
<th>creattimeth>
tr>
thead>
<tbody th:remove="all-but-first">
<tr th:each="blog,blogStat:${blogList}">
<td th:text="${blogStat.count}">1td>
<td th:text="${blogStat.index}">0td>
<td th:text="${blog.id}">titletd>
<td th:text="${blog.title}">titletd>
<td th:text="${blog.mainbody}">mainbodytd>
<td th:text="${blog.creattime}">creattimetd>
tr>
tbody>
<tbody th:remove="all-but-first">
<tr>
<td colspan="2"><p th:text="'Total:' + ${page.pages}">Total pagep>td>
<td><a th:href="@{/blog/pagehelper(page=1)}">firsta>td>
<td><a th:href="@{/blog/pagehelper(page=${page.nextPage})}">nexta>td>
<td><a th:href="@{/blog/pagehelper(page=${page.prePage})}">prexa>td>
<td><a th:href="@{/blog/pagehelper(page=${page.lastPage})}">lasta>td>
tr>
tbody>
table>
div>
body>
html>
次に、実行後のインタフェースの効果を示します.
[外部チェーン画像の転送に失敗しました.ソース局には盗難防止チェーンがある可能性があります.画像を保存して直接アップロードすることをお勧めします(img-asBC 1 woZ-1578014063129)(https://i.imgur.com/xXSwAn9.png)]
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>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql" />
<property name="reasonable" value="true" />
plugin>
plugins>
理由:
もう1つ注意しなければならないのは、5.0以降のバージョンでは、
helperDialect
が元のdialect
の属性に代わって使用されていることです.以下、PageHelper 5.0と以前のバージョンとは異なる箇所を貼り付けます.
com.github.pagehelper.PageInterceptor
である.com.github.pagehelper.dialect.rowbounds
パッケージではdialectパラメータの例としての実装に用いられ、後で詳細なドキュメントを補足し、ページングプラグインに適さないorderby
機能を削除し、後で単一のソートプラグインを提供する.