场景: 查询客户列表, 不同条件之间取交集(且的关系), 单个条件内取并集(或的关系)

实现细节如下:

1. 全等于 (手机号全字匹配)

2. 模糊查询 (客户名称模糊搜索)

3. 单个条件查询多个字段 (客户编号)

4. 日期范围 (近期消费时间)

5. 数值范围 (消费总金额)

6. 数组字段满足任一 (来源平台、下单店铺)

7. 数组字段满足全部 (客户标签)

8. 查询返回指定字段 (自定义列表)

9. 排除指定字段 (自定义列表)

10. 分页

11. 排序

12. 总记录数

文章目录

  • 前端查询条件入参实体类
  • 实现类中使用mongoTemplate构造查询条件
  • 测试效果

提示:以下是本篇文章正文内容,下面案例可供参考

一、前端查询条件入参实体类

/*** 查询客户列表信息条件** @author xiaoyj* @date 2022-07-01*/@Data
@ApiModel("查询客户列表信息条件")
public class ClientBasicInfoBO {/*** 客户名称*/@ApiModelProperty(value = "客户名称")private String name;/*** ADMP客户ID*/@ApiModelProperty(value = "ADMP客户ID")private String admpId;/*** 企业微信id*/private String wechatId;/*** 企微手机号*/@ApiModelProperty(value = "企微手机号")private String mobile;/*** 收货手机号*/@ApiModelProperty(value = "收货手机号")private String receiverMobile;/*** 企微绑定状态*/@ApiModelProperty(value = "企微绑定状态")private String bindStatus;/*** 企微留存状态*/@ApiModelProperty(value = "企微留存状态")private String retentionStatus;/*** 客户标签*/@ApiModelProperty(value = "客户标签")private Long[] admpLabels;/*** 标签范围*/@ApiModelProperty(value = "标签范围: 任一 any ,全部 all")private String tagScope;/*** 店铺id*/@ApiModelProperty(value = "店铺id")private String[] shopIds;/*** 最近消费开始时间*/@ApiModelProperty(value = "最近消费开始时间")private String recentlyBuyBeginTime;/*** 最近消费结束时间*/@ApiModelProperty(value = "最近消费结束时间")private String recentlyBuyEndTime;/*** 最低消费总金额*/@ApiModelProperty(value = "最低消费总金额")private Double lowestTotalBuyAmount;/*** 最高消费总金额*/@ApiModelProperty(value = "最高消费总金额")private Double highestTotalBuyAmount;/*** 最小消费次数*/@ApiModelProperty(value = "最小消费次数")private Integer minTotalBuyTimes;/*** 最多消费次数*/@ApiModelProperty(value = "最大消费次数")private Integer maxTotalBuyTimes;/*** 收货省份*/@ApiModelProperty(value = "收货省份")private String[] receiverProvince;/*** 收货城市*/@ApiModelProperty(value = "收货城市")private String[] receiverCity;/*** 平台类型*/@ApiModelProperty(value = "平台类型")private Integer[] platformTypes;/*** 收货区县*/@ApiModelProperty(value = "收货区县")private String[] receiverDistrict;/*** 自定义列*/@ApiModelProperty(value = "自定义列")private List <String> customColumn;/*** 分页大小*/@ApiModelProperty(value = "分页大小")private Integer pageSize;/*** 当前页数*/@ApiModelProperty(value = "当前页数")private Integer pageNum;
}

二、实现类中使用mongoTemplate构造查询条件

// 创建条件对象
Criteria criteria = new Criteria();
// 3. 单个条件查询多个字段 (客户编号)
if (StringUtils.isNotEmpty(bo.getAdmpId())) {criteria.orOperator(Criteria.where("final_uid").is(bo.getAdmpId()),Criteria.where("customer_ids").in(bo.getAdmpId()),Criteria.where("official_ids").in(bo.getAdmpId()),Criteria.where("tb_ids").in(bo.getAdmpId()),Criteria.where("jd_ids").in(bo.getAdmpId()),Criteria.where("yz_ids").in(bo.getAdmpId()),Criteria.where("wm_ids").in(bo.getAdmpId()),Criteria.where("dd_ids").in(bo.getAdmpId()),Criteria.where("ks_ids").in(bo.getAdmpId()));
}
// 2. 模糊查询 (客户名称模糊搜索)
if (StringUtils.isNotBlank(bo.getName())) {criteria.and("name").regex(Pattern.compile("^.*" + bo.getName() + ".*$", Pattern.CASE_INSENSITIVE));
}
// 1. 全等于 (手机号全字匹配)
if (StringUtils.isNotBlank(bo.getMobile())) {criteria.and("mobile").is(bo.getMobile());
}
if (StringUtils.isNotBlank(bo.getBindStatus())) {criteria.and("bind_status").is(bo.getBindStatus());
}
if (StringUtils.isNotBlank(bo.getRetentionStatus())) {criteria.and("retention_status").is(bo.getRetentionStatus());
}
// 4. 日期范围 (近期消费时间)
if (StringUtils.isNotEmpty(bo.getRecentlyBuyBeginTime()) && StringUtils.isNotEmpty(bo.getRecentlyBuyEndTime())) {criteria.andOperator(Criteria.where("recently_buy_time").gte(bo.getRecentlyBuyBeginTime()), Criteria.where("recently_buy_time").lte(bo.getRecentlyBuyEndTime()));
}
if (StringUtils.isNotNull(bo.getLowestTotalBuyAmount()) && StringUtils.isNotNull(bo.getHighestTotalBuyAmount())) {criteria.and("total_buy_amount").gte(bo.getLowestTotalBuyAmount()).lte(bo.getHighestTotalBuyAmount());
}
if (StringUtils.isNotNull(bo.getLowestTotalBuyAmount()) && StringUtils.isNull(bo.getHighestTotalBuyAmount())) {criteria.and("total_buy_amount").gte(bo.getLowestTotalBuyAmount());
}
if (StringUtils.isNull(bo.getLowestTotalBuyAmount()) && StringUtils.isNotNull(bo.getHighestTotalBuyAmount())) {criteria.and("total_buy_amount").lte(bo.getHighestTotalBuyAmount());
}
// 5. 数值范围 (消费总金额)
if (StringUtils.isNotNull(bo.getMinTotalBuyTimes()) && StringUtils.isNotNull(bo.getMaxTotalBuyTimes())) {criteria.and("total_buy_count").gte(bo.getMinTotalBuyTimes()).lte(bo.getMaxTotalBuyTimes());
}
if (StringUtils.isNotNull(bo.getMinTotalBuyTimes()) && StringUtils.isNull(bo.getMaxTotalBuyTimes())) {criteria.and("total_buy_count").gte(bo.getMinTotalBuyTimes());
}
if (StringUtils.isNull(bo.getMinTotalBuyTimes()) && StringUtils.isNotNull(bo.getMaxTotalBuyTimes())) {criteria.and("total_buy_count").lte(bo.getMaxTotalBuyTimes());
}
if (!CollectionUtils.isEmpty(Arrays.asList(bo.getAdmpLabels()))) {if ("all".equals(bo.getTagScope())) {//  7. 数组字段满足全部 (客户标签)criteria.and("admp_labels").all(bo.getAdmpLabels());} else if ("any".equals(bo.getTagScope())) {criteria.and("admp_labels").in(bo.getAdmpLabels());}
}
if (StringUtils.isNotEmpty(bo.getReceiverMobile())) {criteria.and("receiver_mobiles").in(bo.getReceiverMobile());
}
// 6. 数组字段满足任一 (来源平台、下单店铺)
if (StringUtils.isNotNull(bo.getPlatformTypes()) && bo.getPlatformTypes().length > 0) {criteria.and("source_codes").in(bo.getPlatformTypes());
}
if (StringUtils.isNotNull(bo.getShopIds()) && bo.getShopIds().length > 0) {criteria.and("shop_ids").in(bo.getShopIds());
}
if (StringUtils.isNotNull(bo.getReceiverProvince()) && bo.getReceiverProvince().length > 0) {criteria.and("receiver_provinces").in(bo.getReceiverProvince());
}
if (StringUtils.isNotNull(bo.getReceiverCity()) && bo.getReceiverCity().length > 0) {criteria.and("receiver_cities").in(bo.getReceiverCity());
}
if (StringUtils.isNotNull(bo.getReceiverDistrict()) && bo.getReceiverDistrict().length > 0) {criteria.and("receiver_districts").in(bo.getReceiverDistrict());
}
Query query = new Query();
query.addCriteria(criteria);
// 12. 总记录数
long total = mongoTemplate.count(query, ClientBasicInfoDO.class);
// 8. 查询返回指定字段 (自定义列表)
query.fields().include("final_uid", "name", "wechat_id", "mobile", "u_id", "retention_status", "tb_ids", "jd_ids", "yz_ids", "tb_ids", "wm_ids", "dd_ids", "ks_ids");
// 10. 分页
query.with(PageRequest.of(bo.getPageNum() - 1, bo.getPageSize(),
// 11. 排序
Sort.by(Sort.Order.desc("earliest_add_time"))));
// 执行查询
List<ClientBasicInfoDO> list = mongoTemplate.find(query, ClientBasicInfoDO.class);

三、测试效果

Java使用MongoTemplate实现多条件、模糊查询、排序、范围、分页查询相关推荐

  1. MySQL 排序、分页查询、聚合查询

    文章目录 1. 排序 2. 分页查询 3. 聚合查询 3.1 分组聚合 GROUP BY 练习 LeetCode 176. 第二高的薪水 练习 LeetCode 177. 第N高的薪水 练习 Leet ...

  2. mybatis 分页查询_MyBatis之分页查询:MyBatis PageHelper

    MyBatis,作为目前流行的ORM框架,大大方便了日常开发.而对于分页查询,虽然可以通过SQL的limit语句实现,但是比较繁琐.而MyBatis PageHelper的出现,则解决了这一痛点.这里 ...

  3. oracle不排序分页结果随机,关于ORACLE排序后分页查询出现反复数据的探讨

    关于ORACLE排序后分页查询出现重复数据的探讨 最近在帮一个同事调试BUG,有一个条BUG是这样描述的: 所有有排序功能的列表,当其中某一字段当前页面内容一致时,翻页就有误. 页码改变,列表内容不变 ...

  4. java通用分页条件查询_通用分页查询

    packagecom.dao;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;import ...

  5. sorl java 建索引_solr 的客户端调用solrj 建索引+分页查询

    在 solr 3.5 配置及应用(一) 讲过一了 solr 3.5的详细配置,本节我们讲利用solr 的客户端调用solr的应用了! 一.利用SolrJ操作solr API 使用SolrJ操作Solr ...

  6. SpringData JPA条件查询、排序、分页查询

    前言 在刚开始学习的时候,在dao的定义的接口需要继承JpaRepository<T, ID>接口和JpaSpecificationExecutor< T >接口,但是一直以来 ...

  7. java mybatis分页查询语句_mybatis分页查询的实现(一)

    一.总结了mybatis中五种不同实现分页查询的方法 UserMapper.java接口文件 public interface UserMapper { //分页查询 public List sele ...

  8. java的sqlserver连库信息 包括增删改查 分页查询

    //代码复制出来,修改一下bean,加载一下jar包,能直接用的,不懂加我Q 983331283 package cn.tootoo.entity; public class Page { priva ...

  9. java oracle数据库高效分页查询_oracle高效分页查询总结

    探索查询语句: --分页参数:size = 20 page = 2 --没有order by的查询 -- 嵌套子查询,两次筛选(推荐使用) --SELECT * -- FROM (SELECT ROW ...

  10. java oracle分页查询语句_oracle分页查询语句,java得到分页查询语句的方法

    oracle分页查询语句 select * from ( select a.*, rownum rn from (select * from table_name) a where rownum &l ...

最新文章

  1. ECMAScript+DOM+BOM
  2. XenApp 6安装过程中的两个常见错误
  3. inPixio Photo Studio 11(图片编辑软件)官方正式版V11.0.7709.20526 | 超好用的图片编辑器
  4. ListView优化的
  5. python实验报告实验目的_Python实验报告五
  6. MATLAB gcf图窗保存图像,黑色背景/透明背景
  7. Jetson Nano 2G 使用HDMI连接电视机没有声音的问题
  8. 服务器游戏性能测试工具,python 游戏服务器 性能测试工具
  9. lae界面开发工具入门之介绍十二--iOS系统如何编译打包?
  10. Excel多条件计数——COUNTIFS【获奖情况统计】
  11. PEEKABOO——alpha冲刺置顶集合随笔
  12. java.sql.SQLException: HOUR_OF_DAY: 2 -> 3
  13. zynq linux内核出错,Xilinx Zynq Linux内核源码编译过程
  14. webstrom设置启动时可选择打开项目,不打开最近的项目
  15. 如何播放无限长度的音乐
  16. 适配B2主题的WordPress外链跳转插件AnyLink
  17. 永星电子 HDMI LVDS VGA USB 转 to MIPI DSI 驱动DOME支持1080P旋转横屏安卓RK3288音频缩放自适应机顶盒子投影仪相机PS4 T13530080630
  18. 办公室白领先生的一日锻炼 (转载)
  19. 网络配置——undo 命令
  20. 简单实现短信验证注册功能

热门文章

  1. 双击vs解决方案打不开
  2. tokudb_TokuDB优缺点总结
  3. 将maven项目打包上传到私服
  4. Python 计算混淆矩阵,计算Kappa系数,总体精度
  5. 计算机应用基础网络统考操作,网络教育统考计算机应用基础模拟题(操作题)精选...
  6. 工作中遇到的问题与处理
  7. vue报错Error in render: TypeError: Cannot read property 'name' of undefined
  8. 【2023细胞生物学III】2023细胞生物学III复习资料更新
  9. mac无法访问服务器的共享文件,如何使用Mac上的“文件共享”进行连接
  10. # 关于如何判断一个数是不是整数的方法