本文主要是介绍db4o面向对象数据库::NoSQL,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 完全原生于 Java 和 .NET
- 100% 面向对象, 抛开对象-关系映射
- 为嵌入式应用优化
- 开源,可以基于GPL协议免费使用.
db4o 的 dRS(db4o Replication System)可实现 db4o 与关系型数据库的双向同步(复制) 。 dRS 是基于 Hibernate 开发,基于 dRS 可实现 db4o 到 Hibernate/RDBMS 、 db4o 到 db4o 以及 Hibernate/RDBMS 到 Hibernate/RDBMS 的双向复制。
应用示例:
Pilot.java
package com.agcro.db4o;
import java.util.Date;
class Pilot {private String name;private int age;private Date brth;public Date getBrth() {return brth;}public void setBrth(Date brth) {this.brth = brth;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Pilot(String name,int age,Date brth) {this.name = name;this.age = age;this.brth = brth;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
Db4o.java
package com.agcro.db4o;import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.query.Predicate;
import com.db4o.query.Query;
import com.db4o.query.QueryComparator;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/*** User: anyx* Date: 13-3-27* Time: 下午1:29*/
public class Db4o {//db4o数据库文件路径public static final String dbFile="e:/Project/Test/var/dataFile.db4o";public static void storeObject() throws Exception{ObjectContainer oc= Db4oEmbedded.openFile(dbFile);//日期格式化SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");try{Pilot p=new Pilot("zero",31,sdf.parse("1999-5-1"));oc.store(p);}finally{oc.close();}}public static void query() throws Exception{ObjectContainer oc= Db4oEmbedded.openFile(dbFile);try{SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd");List<Pilot> pilots=oc.query(new Predicate<Pilot>(){SimpleDateFormat sdf2=new SimpleDateFormat("YYYY-MM-dd");Date d=new Date();Date b=sdf2.parse(sdf2.format(d));int a=0;@Overridepublic boolean match(Pilot p){//return p.getName().equals("zs");//return p.getAge() > 25;//return p.getAge() > 18 && p.getAge()<33;//return p.getAge() >= 18 && p.getAge()<33 && (p.getName().startsWith("a") || p.getName().endsWith("s"));//return p.getName().startsWith("a") || p.getName().endsWith("s");a=p.getAge();//return a !=0 && (p.getName().startsWith("a") || p.getName().endsWith("s"));return a !=0 && p.getBrth().after(b)==true && (p.getName().startsWith("a") || p.getName().endsWith("s"));}},new QueryComparator<Pilot>(){public int compare(Pilot p1,Pilot p2){return p1.getName().compareTo(p2.getName());}});for(Pilot p:pilots){System.out.println(p.getName()+" "+p.getAge() + " " + sdf.format(p.getBrth()));}}finally{oc.close();}}public static void querySort() throws Exception{ObjectContainer oc= Db4oEmbedded.openFile(dbFile);try{SimpleDateFormat sdf2=new SimpleDateFormat("YYYY-MM-dd");Date d=new Date();Date b=sdf2.parse(sdf2.format(d));Query query=oc.query();query.constrain(Pilot.class);query.descend("name").orderAscending();query.descend("age").orderDescending();query.descend("brth").orderDescending();query.descend("age").constrain(0).not().and(query.descend("brth").constrain(b).greater()).and(query.descend("name").constrain("a").startsWith(true).or(query.descend("name").constrain("s").endsWith(true)));List<Pilot> pilots=query.execute();for(Pilot p:pilots){System.out.println(p.getName()+" "+p.getAge() + " " + sdf2.format(p.getBrth()));}}finally{oc.close();}}
}
Test.java
package com.agcro.db4o;/*** User: anyx* Date: 13-3-27* Time: 下午1:28*/
public class Test {public static void main(String[] args) throws Exception{//Db4o.storeObject();//Db4o.query();Db4o.querySort();}
}
这篇关于db4o面向对象数据库::NoSQL的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!