Mybatis plus paging query return pit

 

 

Paging query of mybatis plus,
You need to pass in, page (paging condition), and Wrapper query condition to return an IPage, which should be known to all those who have used it

Note that looking down, I'll replace the incoming paging page object with the same one,

Page page = new Page(1, 3);
QueryWrapper<TodayNews> rdxwQueryWrapper = new QueryWrapper<>();
rdxwQueryWrapper.lambda().eq(TodayNews::getLabel, "22")
                    .orderByDesc(SuperEntity::getCreateTime);
IPage rdxwIPage = todayNewsMapper.selectPage(page, rdxwQueryWrapper);


QueryWrapper<TodayNews> ckjsQueryWrapper = new QueryWrapper<>();
ckjsQueryWrapper.lambda().eq(TodayNews::getLabel, "23")
                    .orderByDesc(SuperEntity::getCreateTime);
IPage ckjsIPage = todayNewsMapper.selectPage(page, ckjsQueryWrapper);

See here, without looking at the results, ask if the two IPage objects returned are the same?

 

The result is the same. The contents of the two ipages are the same, and the addresses of the objects are the same. I don't need to talk much about the following nonsense, just like the figure above,

After all, practice is the only test of truth:

When I first encountered this problem, I didn't pay attention to the address of the object. I only focused on why the content in the IPage object was the same. The question I thought about was:

1. Will the IPage object returned by the second query overwrite the first one?

2. It should not be overwritten, because the IPage object returned by each query is new, rather than the one used like the page object passed in

After thinking for a while, I thought that mybatis plus could not be used in this way, but I thought it shouldn't be. Then I used the traditional Sql+limt page to write and return multiple list sets (because the front-end was waiting, so I didn't go deep into this problem at that time),

After encountering the problem, I didn't check the source code, but asked Baidu. I don't know whether I didn't search right or what. I remember that there was a reason why I searched the first level cache and the second level cache. But after thinking about it, every query condition I wrote was different. Why did this happen?

Later, after consulting the eldest brother, I looked directly at the address of the object. If the two IPage addresses returned are the same, it means that they are an IPage, and the content must be the same.

But I return new ones for each query, which should not be used.

Then I went in and looked at the method of mybatis plus paging query select Page: the key point is here

/**
 * Query all records according to entity conditions (and turn pages)
 *
 * @param page         Paging query criteria (can be RowBounds.DEFAULT )
 * @param queryWrapper Entity object encapsulates operation class (null able)
 */
 IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

This is a query method. Note that the page object is not passed in here, but IPae. Why can page be passed in?

Let's go in and look at the class of the object page

public class Page<T> implements IPage<T> 

This is why the query method receives the IPage object, and the page it wears can also be used, because it inherits the IPage,

Seeing that these two methods suddenly come up with a problem, the above introduction said that the IPage object returned by the two queries is one, I can't help thinking, is the IPage object returned by the paging query the incoming page object, according to the paging parameters in the incoming page object, write the returned data of the query into the incoming page object, and then return it?

Or that sentence: practice is the only criterion to test the truth, go straight to the code

 Page rdxepage=new Page(1, 3);
 QueryWrapper<TodayNews> rdxwQueryWrapper = new QueryWrapper<>();
 rdxwQueryWrapper.lambda().eq(TodayNews::getLabel, "22")
                    .orderByDesc(SuperEntity::getCreateTime);
 IPage rdxwIPage = todayNewsMapper.selectPage(rdxepage, rdxwQueryWrapper);

 Page ckjspage=new Page(1, 3);
 QueryWrapper<TodayNews> ckjsQueryWrapper = new QueryWrapper<>();
 ckjsQueryWrapper.lambda().eq(TodayNews::getLabel, "23")
                    .orderByDesc(SuperEntity::getCreateTime);
 IPage ckjsIPage = todayNewsMapper.selectPage(ckjspage, ckjsQueryWrapper);

page is the incoming paging object, IPage is the query return object, and the incoming object address is the same as the returned object address.

See here I think, should be able to confirm my guess.

My conclusion here is that the IPage object returned by mybatis plus is the incoming page object, writes the data returned by the query to the incoming page object, and then returns an IPage object.

Dear Sirs, what are the shortcomings of this article can be pointed out directly. After all, for the first time, please understand.

Tags: Mybatis Lambda SQL

Posted on Thu, 18 Jun 2020 23:48:37 -0400 by Placebo