写在前面,详细的db4o中英文指南参见满江红:http://wiki.redsaga.com/confluence/pages/viewpage.action?pageId=2137
1.SODA Query Graphs
自:http://www.cnblogs.com/cxccbv/archive/2009/02/19/1394380.html
----a graph data structure
查找所有name为“Lincoln”的Person
2.SODA关键字
(1)查询关键字
(2)约束关键字
3.查询示例
(1)查询全部
db = Db4o.openFile("customer.yap"); Query query = db.query(); query.constrain(Customer.class); List<Customer> rs = query.execute(); for(Customer c:rs){ System.out.println(c.getName()); }
(2)条件查询
Query query=db.query(); query.constrain(Customer.class);//类级别的约束 query.descend("name").constrain("customer2");//字段级别的约束 List<Customer> rs=query.execute();
(3)使用关键字
db = Db4o.openFile("customer.yap"); Query query=db.query(); query.constrain(Customer.class); query.descend("name").constrain("customer2").not(); List<Customer> rs=query.execute();
(4)复合条件
and的用法:
Query query=db.query(); query.constrain(Customer.class); Constraint firstConstr=query.descend("phoneNumber").constrain("phone2"); query.descend("name").constrain("customer2").and(firstConstr); List<Customer> rs=query.execute();
等价于:
Query query=db.query(); query.constrain(Customer.class); query.descend("name").constrain("customer2"); query.descend("phoneNumber").constrain("phone2"); List<Customer> rs=query.execute(); Query query=db.query();
or的用法:
query.constrain(Customer.class); Constraint firstConstr=query.descend("phoneNumber").constrain("phone3"); query.descend("name").constrain("customer2").or(firstConstr); List<Customer> rs=query.execute();
(5)greater/smaller
Query query=db.query(); query.constrain(Person.class); query.descend("_age").constrain(80).greater(); ObjectSet result=query.execute();
(6)范围查询
Query query=db.query(); query.constrain(Person.class); Constraint firstConstr = query.descend("_age").constrain(60).greater(); query.descend("_age").constrain(80).smaller().and(firstConstr);
(7)模糊查询
Query query=db.query(); query.constrain(Person.class); query.descend("_name").constrain("Ma").like(); // also works with "ma" ObjectSet result= query.execute();
Query query=db.query(); query.constrain(Person.class); query.descend("age").constrain(0); // field has been set ObjectSet result=query.execute();
(9)结果排序
JAVA Query query=db.query(); query.constrain(Person.class); query.descend("_name").orderAscending(); // the list should start with the lowest ObjectSet result=query.execute();