分页Page插件
首先,要在配置类中注册MyBatisPlus的核心插件,同时添加分页插件。设置分页查询的配置类,interceptor只有拦截作用,功能需要自己添加。这里我们添加上分页查询功能。
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //标记为配置类
public class MyBatisConfig {
@Bean //设置该注解后可以在SpringBoot中直接通过@Autowired注入到其他类中
public MybatisPlusInterceptor mybatisPlusInterceptor() {
//设置MybatisPlus分页插件的拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//1.创建分页插件
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
paginationInnerInterceptor.setMaxLimit(1000L);
//2.添加分页插件
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
}
Test案例,OrderItem中true升序排序 ,false降序
@Test
void testPageQuery() {
int pageNo = 1; // 当前页码
int pageSize = 5; // 每页显示条数
//1.准备分页条件
//1.1分页条件
Page<User> page = Page.of(pageNo, pageSize);
page.addOrder(new OrderItem("balance", true));
page.addOrder(new OrderItem("id", true));
//2.分页查询
Page<User> p = iUserService.page(page);
//3.解析
long total = p.getTotal();
System.out.println("total = " + total);
long size = p.getSize();
System.out.println("size = " + size);
List<User> list = p.getRecords();
list.forEach(System.out::println);
}
案例
要求:
PageQuery
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(description = "分页查询实体类")
public class PageQuery {
@ApiModelProperty("页码")
private Integer pageNo;
@ApiModelProperty("每页大小")
private Integer pageSize;
@ApiModelProperty("排序字段")
private String sortBy;
@ApiModelProperty("是否升序")
private Boolean isAsc;
}
UserQuery继承PageQuery使其能接收Page相关参数
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)//继承父类属性的equals和hashCode方法
@Data
@ApiModel(description = "用户查询条件实体")
public class UserQuery extends PageQuery {
@ApiModelProperty("用户名关键字")
private String name;
@ApiModelProperty("用户状态:1-正常,2-冻结")
private Integer status;
@ApiModelProperty("余额最小值")
private Integer minBalance;
@ApiModelProperty("余额最大值")
private Integer maxBalance;
}
PageDTO为分页查询结果
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "分页结果")
public class PageDTO<T> {
@ApiModelProperty("总条数")
private Long total;
@ApiModelProperty("总页数")
private Long pages;
@ApiModelProperty("当前页数据")
private List<T> list;
}
控制层
@ApiOperation("根据条件分页查询用户接口")
@GetMapping("/page")
public PageDTO<UserVO> queryUserPage(UserQuery userQuery) {
return iUserService.queryUsersPage(userQuery);
}
Service层
PageDTO<UserVO> queryUsersPage(UserQuery userQuery);
Impl层
@Override
public PageDTO<UserVO> queryUsersPage(UserQuery query) {
String name = query.getName();
Integer status = query.getStatus();
//1.构建分页条件
//1.1分页条件
Page<User> page = Page.of(query.getPageNo(), query.getPageSize());
//1.2排序条件
if (StrUtil.isNotBlank(query.getSortBy())) {
//不为空
page.addOrder(new OrderItem(query.getSortBy(), query.getIsAsc()));
} else {
//为空,默认按照更新时间排序
page.addOrder(new OrderItem("update_time", false));
}
//2.分页查询
Page<User> p = lambdaQuery()
.like(name != null, User::getUsername, name)
.eq(status != null, User::getStatus, status)
.page(page);
//3.封装VO对象
PageDTO<UserVO> dto = new PageDTO<>();
//3.1总条数
dto.setTotal(p.getTotal());
//3.2总页数
dto.setPages(p.getPages());
//3.3当前页数据
List<User> records = p.getRecords();
if (CollUtil.isEmpty(records)) {
dto.setList(Collections.emptyList());
return dto;
}
//3.4拷贝user的vo
dto.setList(BeanUtil.copyToList(records, UserVO.class));
return dto;
}