查询列表可筛选可模糊查询的写法
框架:mybatis-plus
1 @Override 2 public OrderListPageDto listOrder(OrderListVo orderListVo) { 3 //返回值 4 OrderListPageDto result = new OrderListPageDto(); 5 //页面对象 6 Page<Order> orderPage = new Page<>(); 7 orderPage.setSize(orderListVo.getPageSize()); 8 orderPage.setCurrent(orderListVo.getPageIndex()); 9 QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>(); 10 //从vo拿筛选条件 11 String status = orderListVo.getStatus(); 12 String serviceType = orderListVo.getServiceType(); 13 String createTimeBegin = orderListVo.getCreateTimeBegin(); 14 String createTimeEnd = orderListVo.getCreateTimeEnd(); 15 16 if (StringUtils.isNotEmpty(status)){ 17 orderQueryWrapper.eq("status",status); 18 } 19 if (StringUtils.isNotEmpty(serviceType)){ 20 orderQueryWrapper.eq("service_type",serviceType); 21 } 22 if (StringUtils.isNotEmpty(createTimeBegin) && StringUtils.isNotEmpty(createTimeEnd)){ 23 orderQueryWrapper.between("create_time",createTimeBegin,createTimeEnd); 24 } 25 //搜索条件: 患者姓名,手机号,下单人员 patient patient_phone user_name 26 String search = orderListVo.getSearch(); 27 if (StringUtils.isNotEmpty(search)){ 28 orderQueryWrapper.and(wrapper->wrapper.like("patient",search)) 29 .or() 30 .like("patient_phone",search) 31 .or() 32 .like("user_name",search); 33 } 34 orderQueryWrapper.orderByDesc("update_time"); 35 //查询 36 Page<Order> page = this.page(orderPage, orderQueryWrapper); 37 List<Order> orderList = page.getRecords(); 38 //给返回值赋值 39 ArrayList<OrderInfoDto> orderInfoDtos = new ArrayList<>(); 40 orderList.forEach(order -> { 41 OrderInfoDto orderInfoDto = new OrderInfoDto(); 42 BeanUtils.copyProperties(order,orderInfoDto); 43 orderInfoDtos.add(orderInfoDto); 44 }); 45 46 result.setOrderInfoDtoList(orderInfoDtos); 47 result.setPageIndex(orderListVo.getPageIndex()); 48 result.setPageSize(orderListVo.getPageSize()); 49 result.setPageNum(page.getPages()); 50 result.setTotal(page.getTotal()); 51 /** 52 * 总计的字段 53 */ 54 TotalNumber totalNumber = new TotalNumber(); 55 List<Order> allOrders = this.list(); 56 //下单数(去除已取消订单) 57 Integer size = allOrders.stream().filter(order -> !order.getStatus().equals(OrderStatusConstant.YQX)).collect(Collectors.toList()).size(); 58 totalNumber.setOrderNum(size); 59 //完成订单数(所有已完成) 60 int finished = allOrders.stream().filter(order -> order.getStatus().equals(OrderStatusConstant.YWC)).collect(Collectors.toList()).size(); 61 totalNumber.setFinishNum(finished); 62 //应收金额(去除已取消,已退款订单) 63 allOrders.removeIf(order -> order.getStatus().equals(OrderStatusConstant.YQX) || order.getStatus().equals(OrderStatusConstant.YTK)); 64 BigDecimal amountSum = allOrders.stream().map(Order::getAmount).reduce(BigDecimal::add).get(); 65 totalNumber.setTotalAmount(amountSum); 66 result.setTotalNumber(totalNumber); 67 68 return result; 69 }
like查询匹配多个字段的写法:
//搜索条件: 患者姓名,手机号,下单人员 patient patient_phone user_name String search = orderListVo.getSearch(); if (StringUtils.isNotEmpty(search)){ orderQueryWrapper.and(wrapper->wrapper.like("patient",search)) .or() .like("patient_phone",search) .or() .like("user_name",search); }
sql语句为:
SELECT
id,desire_hospital,desire_dept,desire_time,patient,identity_num,
patient_phone,identity_img,sick_desc,medical_record,insurance_img,amount,
account_id,user_name,account_num,status,pay_time,create_time,update_time,deleted,
final_hospital,final_dept,final_time,doctor,service_type
FROM `order`
WHERE deleted=0 AND ((patient LIKE ?) OR patient_phone LIKE ? OR user_name LIKE ?) ORDER BY update_time DESC LIMIT ?
==> Parameters: %王武%(String), %王武%(String), %王武%(String), 10(Long)
参考文章
https://blog.csdn.net/weixin_39406978/article/details/112221218?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0.pc_relevant_paycolumn_v2&spm=1001.2101.3001.4242.1&utm_relevant_index=3
posted on 2022-01-02 14:38 rachelgarden 阅读(199) 评论(0) 编辑 收藏 举报