展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

Mybatis plus入门(十):pagehelper分页插件

案例一

    @RequestMapping("/test2")
    @ResponseBody
    public String test2(){
        PageInfo<User> info = PageHelper.startPage(
                1, 2).doSelectPageInfo(() -> mapper.selectById(1)
        );
        List<User> list = info.getList();
        System.out.println(list);
        return "success";
    }

    @RequestMapping("/test3")
    @ResponseBody
    public String test3(){
        List<Long> ids = Arrays.asList(1L, 2L);
        PageInfo<User> info = PageHelper.startPage(1, 5)
                .doSelectPageInfo(() -> mapper.selectList(Wrappers.<User>query().in("id", ids)));
        List<User> list = info.getList();
        System.out.println(list);
        return "success";
    }

案例二

# 导入依赖
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.11</version>
</dependency>

# 配置类
    @Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }

List<UserBookRespDTO> findPage(UserBookReqDTO reqDTO);

    <select id="findPage" resultType="com.ychen.mybatis.model.dto.UserBookRespDTO">
        SELECT u.id, u.username, u.password, u.sex, u.birthday, b.bookname, b.author, b.price
        FROM user AS u LEFT JOIN book AS b ON u.username = b.author
        <where>
            <if test="username !=null and username!=''">
                or u.username like concat('%',#{username},'%')
            </if>
            <if test="sex !=null and sex!=''">
                or u.sex like concat('%',#{sex},'%')
            </if>
            <if test="bookname != null and bookname!=''">
                or b.bookname like concat('%',#{bookname}'%')
            </if>
        </where>
    </select>

    @GetMapping("/test2")
    @ResponseBody
    public String test2(@RequestBody UserBookReqDTO req){
        int pageNo = 1;
        int pageSize = 3;
        try {
            PageHelper.startPage(pageNo,pageSize);
            List<UserBookRespDTO> list = userMapper.findPage(req);
            PageInfo pageinfo = new PageInfo(list);
            System.out.println(pageinfo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "aaa";
    }

# 控制台
PageInfo{pageNum=1, pageSize=3, size=1, startRow=1, endRow=1, total=1, pages=1, list=Page{count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=1, pages=1, reasonable=false, pageSizeZero=false}[UserBookRespDTO(id=1, username=chen1, password=12345, sex=0, birthday=Tue Dec 07 10:40:52 CST 2021, bookname=book1, author=chen1, price=3.99)], prePage=0, nextPage=0, isFirstPage=true, isLastPage=true, hasPreviousPage=false, hasNextPage=false, navigatePages=8, navigateFirstPage=1, navigateLastPage=1, navigatepageNums=[1]}
posted @ 2022-04-27 15:40  DogLeftover  阅读(442)  评论(0编辑  收藏  举报