Mybatis Plus 查询条件中and和or的使用:
mybatis plus 查询条件默认是使用and拼接的,有的情况下我们需要自定义查询条件顺序,希望用到()或者or来进行sql语句优先级查询,所以需要 自己处理 Wrapper查询条件:
1、使用LambdaQueryWrapper进行条件封装:
-
当有多个查询条件时,or()最好结合and()使用
-
当有两个查询条件时,or()可以直接使用;
LambdaQueryWrapper<VipManagement> vipManagementLambdaQueryWrapper = Wrappers.<VipManagement>lambdaQuery()
.like(StringUtils.isNotBlank(vipManagementDto.getVipCarNumber()), VipManagement::getVipCarNumber, vipManagementDto.getVipCarNumber())
.like(StringUtils.isNotBlank(vipManagementDto.getVipName()), VipManagement::getVipName, vipManagementDto.getVipName())
.like(StringUtils.isNotBlank(vipManagementDto.getVipSchemeName()), VipManagement::getVipSchemeName, vipManagementDto.getVipSchemeName())
.like(StringUtils.isNotBlank(vipManagementDto.getCauseTypeName()), VipManagement::getCauseTypeName, vipManagementDto.getCauseTypeName())
.eq(vipManagementDto.getStatus() != null, VipManagement::getStatus, vipManagementDto.getStatus())
.orderByDesc(VipManagement::getCreateTime);
List<Long> marketParkingId = parkingService.getMarketParkingId(vipManagementDto.getMarketId());
if (marketParkingId.size() == 0) {
return PageBean.builder().build();
}
vipManagementLambdaQueryWrapper.and((vipManagementLambdaQueryWrapper1) -> {
marketParkingId.stream().forEach(parkingId -> {
vipManagementLambdaQueryWrapper1.like(VipManagement::getParkingId, parkingId).or();
});
});
2、分简单分页模型:Page
-
page.getCurrent() //当前页,默认 1
-
page.getSize() //每页显示条数,默认 10
-
page.getTotal() //总条数
-
page.getRecords() //分页记录列表
Page<Parking> page = new Page<>();
if (parkingDto.getPageNum() != null || parkingDto.getPageSize() != null) {
page.setCurrent(parkingDto.getPageNum()).setSize(parkingDto.getPageSize());
}
Page<Parking> parkingPage = this.page(page, Wrappers.<Parking>lambdaQuery()
.like(parkingDto.getTypeId() != null, Parking::getTypeId, parkingDto.getTypeId())
.eq(parkingDto.getSupplierId() != null, Parking::getSupplierId, parkingDto.getSupplierId())
.eq(parkingDto.getStatus() != null, Parking::getStatus, parkingDto.getStatus())
.eq(parkingDto.getPlateId() != null, Parking::getPlateId, parkingDto.getPlateId())
.eq(parkingDto.getMarketId() != null, Parking::getMarketId, parkingDto.getMarketId())
.orderByDesc(Parking::getCreateTime).and(StringUtils.isNotBlank(parkingDto.getParkingName()), lambdaQueryWrapper -> {
lambdaQueryWrapper.like(Parking::getParkingName, parkingDto.getParkingName()).or().like(Parking::getParkingNo, parkingDto.getParkingName());
}));