一、mybatis-plus
是mybatis的一种优化版本,省略了单表的xml文件的编写
二、关于mybatis-plus的分页
1.依赖配置如下
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.shidai.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2.分页响应数据模型
你传入的@RequestBody参数对象必须继承BasePageReqVO
@Data
@ApiModel("基础分页request")
public class BasePageReqVO implements Serializable {
@ApiModelProperty("页码,索引从1开始")
private Integer pageNo;
@ApiModelProperty("每页大小")
private Integer pageSize;
}
筛选数据的条件+分页条件,MenuReqVO 与MenuPO其实属性也一样,只不过多了分页的
@Data
@ApiModel("菜单请求数据")
public class MenuReqVO extends BasePageReqVO{
@ApiModelProperty("菜单id")
private Integer menuId;
@ApiModelProperty("菜单名字")
private String menuName;
@ApiModelProperty("菜单url")
private String menuUrl;
@ApiModelProperty("父级id")
private Integer parentId;
@ApiModelProperty("水平")
private Integer level;
}
@Data
public class MenuResVO {
private Integer menuId;
private String menuName;
private String menuUrl;
private Integer parentId;
private Integer level;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("基础分页response")
public class BasePageResVO<T> implements Serializable {
@ApiModelProperty("页码,索引从1开始")
private Integer pageNo;
@ApiModelProperty("每页大小")
private Integer pageSize;
@ApiModelProperty("总页数")
private Integer totalPage;
@ApiModelProperty("总条数")
private Long totalCount;
@ApiModelProperty("列表数据")
private List<T> rows;
public BasePageResVO(int pageNo, int pageSize, long totalCount, List<T> rows) {
this.pageNo = pageNo;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.totalPage = totalPage();
this.rows = rows;
}
private Integer totalPage() {
if (totalCount == 0 || pageSize == 0) {
return 1;
}
return (int) Math.ceil((float) totalCount / pageSize);
}
}
一个实体,无论是查询列表数据,还是分页数据,一般都有一对reqvo和resvo对象 响应时我们不能使泛型为PO对象,虽然从数据库里查询出来的是PO对象,但我们可以通过Stream流得map转换类型!
3.分页数据
public BasePageResVO<MenuResVO> menuPage(MenuReqVO reqVO) {
LambdaQueryWrapper<MenuPO> menuPOLambdaQueryWrapper = new LambdaQueryWrapper<>();
LambdaQueryWrapper<MenuPO> lambdaQueryWrapper = menuPOLambdaQueryWrapper.like(StringUtils.isNotBlank(reqVO.getMenuName()),MenuPO::getMenuName,reqVO.getMenuName())
.orderByDesc(MenuPO::getLevel);
Page<MenuPO> menuPOPage = menuMapper.selectPage(new Page<>(reqVO.getPageNo(), reqVO.getPageSize()), lambdaQueryWrapper);
List<MenuPO> menuResVOS = menuPOPage.getRecords();
List<MenuResVO> menuResVOs = menuResVOS.stream().map(o -> {
MenuResVO menuPageVO = new MenuResVO();
BeanUtils.copyProperties(o, menuPageVO);
return menuPageVO;
}).collect(Collectors.toList());
return new BasePageResVO<MenuResVO>((int)menuPOPage.getCurrent(),(int)menuPOPage.getSize(),menuPOPage.getTotal(), menuResVOs);
}
|