20200115 PageHelper

Posted huangwenjie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20200115 PageHelper相关的知识,希望对你有一定的参考价值。

简介

PageHelper是Mybatis通用分页插件。

SpringBoot、Mybatis、PageHelper

  1. pom.xml
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.3</version>
</dependency>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
  1. 编写Mapper

与Mybatis一样

@Mapper
public interface UserMapper {
    @Select("select * from user")
    Page<User> getAllUsers();

}
  1. 使用PageHelper
@SpringBootApplication
public class PagehelperApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(PagehelperApplication.class, args);

        // ==================== 用法一 =============================
        // PageHelper.startPage(1, 2);
        // test(ctx);

        // ==================== 用法二 =============================
        // PageHelper.offsetPage(2,2);
        // test(ctx);

        // ==================== 用法三 =============================
        SqlSessionFactory sessionFactory = ctx.getBean(SqlSessionFactory.class);
        System.out.println(sessionFactory);
        SqlSession sqlSession = sessionFactory.openSession();
        List<User> userList = sqlSession.selectList("study.hwj.pagehelper.user.UserMapper.getAllUsers",null, new RowBounds(0, 2));
        System.out.println(userList);
        userList.forEach(System.out::println);
    }

    private static void test(ConfigurableApplicationContext ctx){
        System.out.println("======================================");
        UserMapper userMapper = ctx.getBean(UserMapper.class);
        Page<User> page = userMapper.getAllUsers();
        System.out.println(page);
        page.forEach(System.out::println);
    }

}

参考资料

以上是关于20200115 PageHelper的主要内容,如果未能解决你的问题,请参考以下文章

django框架基础-分页-长期维护-20200115

20200115--python学习第九天

Spring boot入门:SpringBoot集成结合AdminLTE(Freemarker),利用generate自动生成代码,利用DataTable和PageHelper进行分页显示(示例(代码

Mybatis PageHelper 使用的注意事项

关于在SSM框架下使用PageHelper

MyBatis分页插件:比PageHelper都要简单,使用很灵活,目前支持Oracle和MySQL