DB4O之SODA查询

本文提供了一篇关于DB4O的详细指南,包括如何使用自定义图数据结构进行查询,以及如何执行各种查询操作如条件查询、使用关键字、复合条件查询、范围查询、模糊查询、查询空值和结果排序。通过示例代码,深入浅出地展示了DB4O查询的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在前面,详细的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(); 

 

 

 

 

 


(8)查询空值

 

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(); 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值