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

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

实现细节如下:

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

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

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

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

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

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

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

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

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

10. 分页

11. 排序

12. 总记录数


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

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

/**
 * 查询客户列表信息条件
 *
 * @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);

三、测试效果

  • 11
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值