2007-11-29

db4o入门实例

一、db4o简介:
    简单的说,就是可以用对象的方式存储数据,比如java的集合对象,javabean,pojo对象等。
    开源,可以基于dboGPL协议免费使用,最新版本目前到了6.1版本(java)。官方网站有基于MySql数据库和其他存取方式性能比较的数据,性能十分不错。传闻应用十分广泛,从金融系统到战斗机控制系统都有使用。

db4o 为我们带来的是这样一种面向对象的查询方式:
1.完全原生,目前查询语言能用Java和.NET表达
2.100% 面向对象, 抛开对象-关系映射
3.100% 的类型安全 查询语言能完全获取现代IDE的特性,
    比如语法检测、类型检测、重构,等等。

二、安装
只要将jar文件加到classpath中即可。

下载了之后,有PDF格式和HTML格式的指南文档。
 
三、入门实例:(运行以后将在当前目录中产生db.data数据库文件)

java 代码
  1. import com.db4o.Db4o;   
  2. import com.db4o.ObjectContainer;   
  3. import com.db4o.ObjectSet;   
  4. public class FirstStepsExample{       
  5.     public static void main(String[] args) {   
  6.            
  7.                  
  8.         ObjectContainer db=Db4o.openFile("db.data");   
  9.         try {   
  10.             storeFirstPilot(db);   
  11.             storeSecondPilot(db);   
  12.             retrieveAllPilots(db);   
  13.             retrievePilotByName(db);   
  14.             retrievePilotByExactPoints(db);   
  15.             updatePilot(db);   
  16.             deleteFirstPilotByName(db);   
  17.             deleteSecondPilotByName(db);   
  18.         }   
  19.         finally {   
  20.             db.close();   
  21.         }   
  22.     }   
  23.        
  24.     public static void accessDb4o() {   
  25.         ObjectContainer db=Db4o.openFile("db.data");   
  26.         try {   
  27.             // do something with db4o   
  28.         }   
  29.         finally {   
  30.             db.close();   
  31.         }   
  32.     }   
  33.        
  34.     public static void storeFirstPilot(ObjectContainer db) {   
  35.         Pilot pilot1=new Pilot("Michael Schumacher",100);   
  36.         db.set(pilot1);   
  37.         System.out.println("Stored "+pilot1);   
  38.     }   
  39.     public static void storeSecondPilot(ObjectContainer db) {   
  40.         Pilot pilot2=new Pilot("Rubens Barrichello",99);   
  41.         db.set(pilot2);   
  42.         System.out.println("Stored "+pilot2);   
  43.     }   
  44.     public static void retrieveAllPilotQBE(ObjectContainer db) {   
  45.         Pilot proto=new Pilot(null,0);   
  46.         ObjectSet result=db.get(proto);   
  47.         listResult(result);   
  48.     }   
  49.        
  50.     public static void retrieveAllPilots(ObjectContainer db) {   
  51.         ObjectSet result=db.get(Pilot.class);   
  52.         listResult(result);   
  53.     }   
  54.     public static void retrievePilotByName(ObjectContainer db) {   
  55.         Pilot proto=new Pilot("Michael Schumacher",0);   
  56.         ObjectSet result=db.get(proto);   
  57.         listResult(result);   
  58.     }   
  59.        
  60.     public static void retrievePilotByExactPoints(ObjectContainer db) {   
  61.         Pilot proto=new Pilot(null,100);   
  62.         ObjectSet result=db.get(proto);   
  63.         listResult(result);   
  64.     }   
  65.     public static void updatePilot(ObjectContainer db) {   
  66.         ObjectSet result=db.get(new Pilot("Michael Schumacher",0));   
  67.         Pilot found=(Pilot)result.next();   
  68.         found.addPoints(11);   
  69.         db.set(found);   
  70.         System.out.println("Added 11 points for "+found);   
  71.         retrieveAllPilots(db);   
  72.     }   
  73.     public static void deleteFirstPilotByName(ObjectContainer db) {   
  74.         ObjectSet result=db.get(new Pilot("Michael Schumacher",0));   
  75.         Pilot found=(Pilot)result.next();   
  76.         db.delete(found);   
  77.         System.out.println("Deleted "+found);   
  78.         retrieveAllPilots(db);   
  79.     }   
  80.     public static void deleteSecondPilotByName(ObjectContainer db) {   
  81.         ObjectSet result=db.get(new Pilot("Rubens Barrichello",0));   
  82.         Pilot found=(Pilot)result.next();   
  83.         db.delete(found);   
  84.         System.out.println("Deleted "+found);   
  85.         retrieveAllPilots(db);   
  86.     }   
  87.    public static void listResult(ObjectSet result) {   
  88.     System.out.println(result.size());   
  89.     while(result.hasNext()) {   
  90.         System.out.println(result.next());   
  91.     }   
  92.   }   
  93. }   
  94. public class Pilot {       
  95.     private String name;   
  96.     private int points;     
  97.        
  98.     public Pilot(String name,int points) {   
  99.         this.name=name;   
  100.         this.points=points;   
  101.     }   
  102.            
  103.     public int getPoints() {   
  104.         return points;   
  105.     }   
  106.        
  107.     public void addPoints(int points) {   
  108.         this.points+=points;   
  109.     }   
  110.        
  111.     public String getName() {   
  112.         return name;   
  113.     }   
  114.        
  115.     public String toString() {   
  116.         return name+"/"+points;   
  117.     }   
  118. }   

 

运行结果: C:\Test>java FirstStepsExample
Stored Michael Schumacher/100
Stored Rubens Barrichello/99
2
Michael Schumacher/100
Rubens Barrichello/99
1
Michael Schumacher/100
1
Michael Schumacher/100
Added 11 points for Michael Schumacher/111
2
Michael Schumacher/111
Rubens Barrichello/99
Deleted Michael Schumacher/111
1
Rubens Barrichello/99
Deleted Rubens Barrichello/99
0

评论
发表评论

您还没有登录,请登录后发表评论

cnhzliye
搜索本博客
存档
最新评论