zoukankan      html  css  js  c++  java
  • morphia(3)-查询

    1、查询所有

    @Test
    public void query() throws Exception {
        final Query<Employee> query = datastore.createQuery(Employee.class);
        final List<Employee> list = query.asList();
        list.forEach(e -> Console.log("{}", e));
    }

    输出:

    Employee(id=5bcef23890c1d9280c07128e, name=小弟1, manager=null, directReports=[], salary=2000.0)
    Employee(id=5bcef23890c1d9280c07128f, name=小弟2, manager=null, directReports=[], salary=3000.0)
    Employee(id=5bcef23890c1d9280c071290, name=zuoys, manager=null, directReports=[Employee(id=5bcef23890c1d9280c07128e, name=小弟1, manager=null, directReports=[], salary=2000.0), Employee(id=5bcef23890c1d9280c07128f, name=小弟2, manager=null, directReports=[], salary=3000.0)], salary=10000.0)
    Employee(id=5bcef45d90c1d91d509941cd, name=小弟3有父, manager=Employee(id=5bcef23890c1d9280c071290, name=zuoys, manager=null, directReports=[Employee(id=5bcef23890c1d9280c07128e, name=小弟1, manager=null, directReports=[], salary=2000.0), Employee(id=5bcef23890c1d9280c07128f, name=小弟2, manager=null, directReports=[], salary=3000.0)], salary=10000.0), directReports=[], salary=22.0)

    使用asList()是可以的,但实际上,fetch()通常是更好的选择。

     2、条件过滤

    //条件过滤-field
    @Test
    public void query2() throws Exception {
        List<Employee> list = datastore.createQuery(Employee.class)
                .field("salary").lessThanOrEq(22)
                .asList();
        list.forEach(e -> Console.log("{}", e));
    }
    //条件查询-filter
    @Test
    public void query3() throws Exception {
        List<Employee> list = datastore.createQuery(Employee.class)
                .filter("salary <=", 22)
                .asList();
        list.forEach(e -> Console.log("{}", e));
    }

    使用filter比fiele更简洁,但要注意语法。

  • 相关阅读:
    STL笔记之【map之总概】
    STL笔记之set
    Effective C++笔记之Item49【了解new-handler的行为】
    明成软件条形码打印设置
    将Excel数据导入到SqlServer及导入时数据类型转换失败解决方案
    远程桌面无法复制粘贴传输文件解决办法
    DELPHI如何读取cxcheckcombobox中的值
    Delphi 插入Excel图片和值
    SQL 查询语句先执行 SELECT?
    Linux之xargs命令传递参数的一个过滤器
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/9841776.html
Copyright © 2011-2022 走看看