zoukankan      html  css  js  c++  java
  • MongoDBTemplate多条件查询的问题

    问题:

    在使用Spring Data MongoDB 进行条件查询数据时,发现条件判断不起作用,结果会返回所有的数据。

    Criteria criteria = new Criteria();
                criteria.where("shopId")
                        .is(request.getShopId())
                        .and("tradeDate").gte(request.getBeginTradeDate())
                        .lte(request.getEndTradeDate());
                List<ReportInfo> reportInfoList = reportMongoTemplate.
                        find(new Query(criteria), ReportInfo.class,"rept_wallet_refundAndPay");
    

    原因:

    经过查看文档和源代码才发现,Criteria的where方法是一个静态工厂方法,它会返回一个实例化的criteria对象,所以就不需要自己new 一个criteria对象了。否则 find(new Query(criteria)里的criteria没有任何判断条件,因此会返回所有的数据。

    MongoTemplate 源码

        public static Criteria where(String key) {
            return new Criteria(key);
        }
    

    修改

    Criteria criteria = Criteria.where("shopId")
                        .is(request.getShopId())
                        .and("tradeDate").gte(request.getBeginTradeDate())
                        .lte(request.getEndTradeDate());
                List<ReportInfo> reportInfoList = reportMongoTemplate.
                        find(new Query(criteria), ReportInfo.class,"rept_wallet_refundAndPay");
    

    总结:

    还是要看官方的文档和源代码

  • 相关阅读:
    poj 2135 最小费用最大流初步
    HDU4864 贪心好题
    HDU 5643 约瑟夫环的应用
    HDU 5642 多重集排列数 递推
    HDU 5640
    HDU 2819 最大匹配
    poj 1988 多校联赛 带权并查集
    HDU 2817 多校联赛1
    HDU 2822 多校联赛1
    第二节(标识符,关键字,数据类型,运算符)
  • 原文地址:https://www.cnblogs.com/lanqi/p/7978048.html
Copyright © 2011-2022 走看看