136. [Video] Spring Boot MyBatis Upgrade - Annotation - Paging Query

 

【Video & Communication Platform】

à SpringBoot Video 

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à  SpringCloud video

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot source code 

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot communication platform 

http://412887952-qq-com.iteye.com/blog/2321532

 

 

[This blog has a supporting video, the video address: " Spring Boot MyBatis Upgrade - Annotation - Paging Query " , click the following to read the original text in the official account, and the video will explain in more detail]

Origin of need:

       In the previous blog, we introduced the addition, deletion, modification and query of MyBatis . For query, we query all the data. In practice, we may need to query by paging, query 10 pieces of data at a time for display, and then the user clicks the next When the page is displayed, the data of the next page is displayed. Here we mainly use a plugin PageHelper . Take a look at the outline of this section:

Outline of this section:

(1) Integration principle description
(2) Introduction to PageHelper
(3) Integration preparation
(4) Configuration file writing
(5) Coding test

 

       Let's take a look at the specific content:

( 1 ) Description of the integration principle

MyBatis provides an interceptor interface, we can implement our own interceptor and load it into SqlSessionFactory as a plugin .

 

( 2 ) Introduction to PageHelper

PageHelper is a paging plugin written by a developer on Github , which can be easily added to the interceptor interface of MyBatis .

       Github project address: https://github.com/pagehelper/Mybatis-PageHelper

 

( 3 ) Integration preparation

       集成PageHelper很简单,只需要在pom.xml文件中添加如下依赖:

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>

 

 

4)配置文件编写

       我们需要新增一个配置文件:

新增MyBatisConfiguration.java

package com.kfit.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.pagehelper.PageHelper;
 
/**
 * mybatis配置类.
 * @author Angel --守护天使
 * @version v.0.1
 * @date 2017年7月22日
 */
@Configuration
public class MyBatisConfiguration {
   
    /**
     * 注册MyBatis分页插件PageHelper
     * @return
     */
    @Bean
    public PageHelper pageHelper() {
       System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}

 

 

5)编码测试

这个使用起来特别的简单,只是在原来查询全部的代码之前加入一句:

PageHelper.startPage(1,2);

第一个参数是第几页;第二个参数是每页显示条数。

       具体的代码示例如下:

@RequestMapping("/selectAll")
public List<Demo> selectAll(@RequestParam(defaultValue="1")intpageNum){
       PageHelper.startPage(pageNum, 2);
       return demoService.selectAll();
}

 

 

访问http://127.0.0.1:8080/selectAll?pageNum=2进行测试。

 

视频&交流平台

à SpringBoot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037955&siteId=291194637