SpringBoot | Mybatis通用mapper的分页查询

方式一

 @Test
    public void findByPageHleper() {
        DogInfo dogInfo = new DogInfo();
        dogInfo.setDogId("7");
        Example example = new Example(DogInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotNullOrNotEmpty(dogInfo.getDogId())) {
            // property是实体类的字段属性
            criteria.andLike("dogId", dogInfo.getDogId() + "%");
        }
        // orderBy的key是表的字段
        //sql ==>SELECT * FROM dog_info WHERE (dog_id LIKE ?) order by record_id desc LIMIT ?
        PageHelper.startPage(1, 10, "record_id desc");
        PageInfo<DogInfo> pageInfo = new PageInfo<>(dogInfoMapper.selectByExample(example));
        System.out.println("pageInfo.getSize() = " + pageInfo.getSize());//10
        System.out.println("pageInfo.getTotal() = " + pageInfo.getTotal());//459
    }

方式二

@Test
    public void findByPageHleper2() {
        DogInfo dogInfo = new DogInfo();
        dogInfo.setDogId("7");
        Example example = new Example(DogInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotNullOrNotEmpty(dogInfo.getDogId())) {
            // property是实体类的字段属性
            criteria.andLike("dogId", dogInfo.getDogId() + "%");
        }
        // orderBy的key是表的字段
        //sql ==>SELECT * FROM dog_info WHERE (dog_id LIKE ?) order by record_id desc LIMIT ?
        PageHelper.startPage(1, 10, "record_id desc");
        List<DogInfo> dogInfoList = dogInfoMapper.selectByExample(example);
        PageInfo<DogInfo> pageInfo = new PageInfo<>(dogInfoList);
        System.out.println("pageInfo.getSize() = " + pageInfo.getSize());//10
        System.out.println("pageInfo.getTotal() = " + pageInfo.getTotal());//459
    }

方式三

 @Test
    public void findByPageHleper3() {
        DogInfo dogInfo = new DogInfo();
        dogInfo.setDogId("7");
        Example example = new Example(DogInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotNullOrNotEmpty(dogInfo.getDogId())) {
            // property是实体类的字段属性
            criteria.andLike("dogId", dogInfo.getDogId() + "%");
        }
        // orderBy的key是表的字段
        // sql==> SELECT * FROM dog_info WHERE ( dog_id like ? )
        List<DogInfo> dogInfoList = dogInfoMapper.selectByExample(example);
        PageHelper.startPage(1, 10, "record_id desc");
        PageInfo<DogInfo> pageInfo = new PageInfo<>(dogInfoList);
        System.out.println("pageInfo.getSize() = " + pageInfo.getSize());//459
        System.out.println("pageInfo.getTotal() = " + pageInfo.getTotal());//459
    }
上一篇:PageHelper 记录总条数不正确问题处理


下一篇:SpringBoot实现简单的增删改查以及条件分页