Paging query problem records of mybaties

Show your own implementation first

service:

public PageInfo<TestBean> getTestBeanList(Integer pageSize, Integer pageNo){
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<TestVo> pageInfo = null;
        List<TestVo> list = testDao.getTestVoList();
        List<TestBean> beanlist = list.stream().map(this::toBean).collector(Collectors.toList()); //这里转换了会有问题
        pageInfo = new PageInfo<TestBean>(beanlist);
        return pageInfo;
    }

public TestBean toBean(TestVo testVo){
    //这里是转换方法
}

testDao:

@Mapper
public interface TestDao{

    List<TestVo> getTestVoList();
}

The data can be queried correctly. But the pagination parameters are indeed wrong.

After checking the breakpoint, it is found that mybaties is trying to process the pagination query, although we return a List. But it is actually Page (com.github.pagehelper.Page), which inherits ArryList and contains pagination information.

List<TestVo> list = testDao.getTestVoList(); //Here actually returns the Page object
 List<TestBean> beanlist = list.stream().map(this::toBean).collector(Collectors.toList()); //After the conversion here, the paging parameters will be lost, and the game object will change from Page to List.

 pageInfo = new PageInfo<TestBean>(beanlist); //The List object is passed in here, not the Page object, so there will be problems

In new PageInfo<TestBean>(List<T> list), Page<T> is actually passed in. And initialize the paging information in the constructor. as follows:

Therefore, if we use new PageInfo<TestBean>(List<T> list) and the list passed in is not the object (Page) returned by the original mapper method, the paging parameters of the data will not be parsed correctly. So page fault.

You can get the corresponding paging parameters by forcing the parameters to Page objects, or build Page objects yourself, and convert PageInfo into our custom paging objects.

Guess you like

Origin blog.csdn.net/qq_34484062/article/details/126741593