MyBatis Plus 的 Service 数据分页查询
1. 简单介绍
嗨,大家好,今天给想给大家分享一下关于Mybatis-plus 的 Service 层的一些方法的使用。今天没有总结,因为都是一些API没有什么可以总结的,直接看着调用就可以了。
下边的连接也可以看到同样的内容: 这里地址就是带中文的
https://wnagzainote.yuque.com/books/share/46f28001-903f-4fb6-abdc-ecf9c2bf02bb?# 《MyBatis Plus 学习》
下面介绍怎样使用 IServer 提供的 page 方法进行分页查询
2. 接口说明
接口提供了如下四个 page 方法:
// 无条件分页查询
IPage<T> page(IPage<T> page);
// 条件分页查询
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
// 无条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page);
// 条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
3. 参数说明
- page:翻页对象
- queryWrapper:实体对象封装操作类 QueryWrapper
4. 实例代码
4.1 无条件限制的数据分页
下面示例将对数据进行分页,每一页大小为5,取出第二页的数据
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxstrive.mybatis_plus.model.UserBean;
import com.hxstrive.mybatis_plus.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class Page1Test {
@Autowired
private UserService userService;
@Test
void contextLoads() {
Page<UserBean> page = new Page(2, 5);
userService.page(page);
for(UserBean userBean : page.getRecords()) {
System.out.println(userBean);
}
}
}
4.2 有条件的数据分页
使用 QueryWrapper 筛选所有薪水大于7000,性别为男的用户,然后对其进行分页
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxstrive.mybatis_plus.model.UserBean;
import com.hxstrive.mybatis_plus.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class Page2Test {
@Autowired
private UserService userService;
@Test
void contextLoads() {
QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
wrapper.gt("salary", 7000);
wrapper.eq("sex", "男");
Page<UserBean> page = new Page(2, 5);
userService.page(page, wrapper);
for(UserBean userBean : page.getRecords()) {
System.out.println(userBean);
}
}
}
4.3 关于IPage 接口的默认实现 Page
- 在 IService 接口中提供了很多以 page 开头的方法,这些方法均会接收一个 IPage 参数,该参数封装了分页信息
- MyBatis Plus 提供了 IPage 接口的默认实现 Page,代码如下:
public class Page<T> implements IPage<T> {
private static final long serialVersionUID = 1L;
/** 查询数据列表 */
protected List<T> records = Collections.emptyList();
/** 总数 */
protected long total = 0;
/** 每页显示条数,默认 10 */
protected long size = 10;
/** 当前页 */
protected long current = 1;
/** 排序字段信息 */
protected List<OrderItem> orders = new ArrayList<>();
/** 自动优化 COUNT SQL */
protected boolean optimizeCountSql = true;
/** 是否进行 count 查询 */
protected boolean isSearchCount = true;
/** 是否命中count缓存 */
protected boolean hitCount = false;
protected String countId;
protected Long maxLimit;
}
4.4 注意事项说明
- 请注意,这里我们所描述的一切方法都是基于 Service 层来说的
- 请注意,这里我们所描述的一切方法都是不是基于 Mapper 层来说的
- 请注意,这里的分页,如果需要实现,还需要做一些配置,具体配置请参考,下边连接