Mybatis-plus使用pagehelper进行分页

Mybatis-plus使用pagehelper进行分页_第1张图片

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
    id   bigint(20) NOT NULL COMMENT '主键ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    PRIMARY KEY (id)
);

DELETE
FROM user;

INSERT INTO user (id, name)
VALUES (1, 'Jone'),
       (2, 'Jack'),
       (3, 'Tom'),
       (4, 'Sandy'),
       (5, 'Billie');
package com.zs.testmybatisplus;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.github.pagehelper.PageInterceptor;

/**
 * 两个分页插件都配置,不会冲突
 *
 * @author miemie
 * @date 2020/5/29
 */
@Configuration
public class MybatisPlusPageConfig {

    /**
     * pagehelper的分页插件
     */
    @Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }

//    @Bean
//    public ConfigurationCustomizer configurationCustomizer() {
//        return configuration -> configuration.setUseDeprecatedExecutor(false);
//    }
}

1实体类

package com.zs.testmybatisplus;

import lombok.Data;

/**
 * @author miemie
 * @since 2020-05-29
 */
@Data
public class User {

    private Long id;

    private String name;
}

2测试类

package com.zs.testmybatisplus;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @author miemie
 * @since 2020-05-29
 */
@SpringBootTest
class DefaultTest {

    @Autowired
    private UserMapper mapper;

    @Test
    void test() {
        /**
         * ==>  Preparing: SELECT COUNT(*) FROM user WHERE (id = ?)
         * ==> Parameters: 1(Integer)
         * <==    Columns: COUNT(*)
         * <==        Row: 1
         * <==      Total: 1
         * ==>  Preparing: SELECT id,name FROM user WHERE (id = ?) LIMIT ?
         * ==> Parameters: 1(Integer), 2(Long)
         * <==    Columns: id, name
         * <==        Row: 1, Jone
         * <==      Total: 1
         */
        Page<User> mpPage = mapper.selectPage(new Page<>(1, 2), Wrappers.<User>query()
                .eq("id", 1));
        assertThat(mpPage.getTotal()).isEqualTo(1L);
        List<User> records = mpPage.getRecords();
        assertThat(records).isNotEmpty();
        assertThat(records.size()).isEqualTo(1);

        // pagehelper
        /**
         * ==>  Preparing: SELECT count(0) FROM user WHERE id = ?
         * ==> Parameters: 1(Integer)
         * <==    Columns: count(0)
         * <==        Row: 1
         * <==      Total: 1
         * ==>  Preparing: SELECT id,name FROM user WHERE id=? LIMIT ?
         * ==> Parameters: 1(Integer), 2(Integer)
         * <==    Columns: id, name
         * <==        Row: 1, Jone
         * <==      Total: 1
         */
        PageInfo<User> info = PageHelper.startPage(1, 2)
                .doSelectPageInfo(
                        () -> mapper.selectById(1));

        assertThat(info.getTotal()).isEqualTo(1L);
        List<User> list = info.getList();
        assertThat(list).isNotEmpty();
        assertThat(list.size()).isEqualTo(1);
    }

    @Test
    void testIn() {
        /**
         * ==>  Preparing: SELECT COUNT(*) FROM user WHERE (id IN (?, ?))
         * ==> Parameters: 1(Long), 2(Long)
         * <==    Columns: COUNT(*)
         * <==        Row: 2
         * <==      Total: 1
         * ==>  Preparing: SELECT id,name FROM user WHERE (id IN (?,?)) LIMIT ?
         * ==> Parameters: 1(Long), 2(Long), 5(Long)
         * <==    Columns: id, name
         * <==        Row: 1, Jone
         * <==        Row: 2, Jack
         * <==      Total: 2
         */
        List<Long> ids = Arrays.asList(1L, 2L);
        Page<User> mpPage = mapper.selectPage(new Page<>(1, 5), Wrappers.<User>query()
                .in("id", ids));
        assertThat(mpPage.getTotal()).isEqualTo(2L);
        List<User> records = mpPage.getRecords();
        assertThat(records).isNotEmpty();
        assertThat(records.size()).isEqualTo(2);

        // pagehelper
        /**
         * ==>  Preparing: SELECT count(0) FROM user WHERE (id IN (?, ?))
         * ==> Parameters: 1(Long), 2(Long)
         * <==    Columns: count(0)
         * <==        Row: 2
         * <==      Total: 1
         * ==>  Preparing: SELECT id,name FROM user WHERE (id IN (?,?)) LIMIT ?
         * ==> Parameters: 1(Long), 2(Long), 5(Integer)
         * <==    Columns: id, name
         * <==        Row: 1, Jone
         * <==        Row: 2, Jack
         * <==      Total: 2
         */
        PageInfo<User> info = PageHelper.startPage(1, 5)
                .doSelectPageInfo(
                        () -> mapper.selectList(Wrappers.<User>query()
                                .in("id", ids)));
        assertThat(info.getTotal()).isEqualTo(2L);
        List<User> list = info.getList();
        assertThat(list).isNotEmpty();
        assertThat(list.size()).isEqualTo(2);
    }
}

3mapper

package com.zs.testmybatisplus;

import org.apache.ibatis.annotations.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;


/**
 * @author miemie
 * @since 2020-05-29
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

}

你可能感兴趣的:(Mybatis-plus使用pagehelper进行分页)