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数据库文件)
- import com.db4o.Db4o;
- import com.db4o.ObjectContainer;
- import com.db4o.ObjectSet;
- public class FirstStepsExample{
- public static void main(String[] args) {
- ObjectContainer db=Db4o.openFile("db.data");
- try {
- storeFirstPilot(db);
- storeSecondPilot(db);
- retrieveAllPilots(db);
- retrievePilotByName(db);
- retrievePilotByExactPoints(db);
- updatePilot(db);
- deleteFirstPilotByName(db);
- deleteSecondPilotByName(db);
- }
- finally {
- db.close();
- }
- }
- public static void accessDb4o() {
- ObjectContainer db=Db4o.openFile("db.data");
- try {
- // do something with db4o
- }
- finally {
- db.close();
- }
- }
- public static void storeFirstPilot(ObjectContainer db) {
- Pilot pilot1=new Pilot("Michael Schumacher",100);
- db.set(pilot1);
- System.out.println("Stored "+pilot1);
- }
- public static void storeSecondPilot(ObjectContainer db) {
- Pilot pilot2=new Pilot("Rubens Barrichello",99);
- db.set(pilot2);
- System.out.println("Stored "+pilot2);
- }
- public static void retrieveAllPilotQBE(ObjectContainer db) {
- Pilot proto=new Pilot(null,0);
- ObjectSet result=db.get(proto);
- listResult(result);
- }
- public static void retrieveAllPilots(ObjectContainer db) {
- ObjectSet result=db.get(Pilot.class);
- listResult(result);
- }
- public static void retrievePilotByName(ObjectContainer db) {
- Pilot proto=new Pilot("Michael Schumacher",0);
- ObjectSet result=db.get(proto);
- listResult(result);
- }
- public static void retrievePilotByExactPoints(ObjectContainer db) {
- Pilot proto=new Pilot(null,100);
- ObjectSet result=db.get(proto);
- listResult(result);
- }
- public static void updatePilot(ObjectContainer db) {
- ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
- Pilot found=(Pilot)result.next();
- found.addPoints(11);
- db.set(found);
- System.out.println("Added 11 points for "+found);
- retrieveAllPilots(db);
- }
- public static void deleteFirstPilotByName(ObjectContainer db) {
- ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
- Pilot found=(Pilot)result.next();
- db.delete(found);
- System.out.println("Deleted "+found);
- retrieveAllPilots(db);
- }
- public static void deleteSecondPilotByName(ObjectContainer db) {
- ObjectSet result=db.get(new Pilot("Rubens Barrichello",0));
- Pilot found=(Pilot)result.next();
- db.delete(found);
- System.out.println("Deleted "+found);
- retrieveAllPilots(db);
- }
- public static void listResult(ObjectSet result) {
- System.out.println(result.size());
- while(result.hasNext()) {
- System.out.println(result.next());
- }
- }
- }
- public class Pilot {
- private String name;
- private int points;
- public Pilot(String name,int points) {
- this.name=name;
- this.points=points;
- }
- public int getPoints() {
- return points;
- }
- public void addPoints(int points) {
- this.points+=points;
- }
- public String getName() {
- return name;
- }
- public String toString() {
- return name+"/"+points;
- }
- }
运行结果: 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
发表评论
- 浏览: 19802 次
- 性别:

- 来自: 杭州

- 详细资料
搜索本博客
最近加入圈子
链接
最新评论
-
[Jakarta Commons笔记] Co ...
看了Javadoc , 被 MultiValueMap 代替了。
-- by icank -
IE和firefox通用的复制到 ...
转载请注明出处copy text to clipboard with javas ...
-- by 花花公子 -
IE和firefox通用的复制到 ...
感谢!好像firefox处于安全考虑才对这个事件特殊处理?
-- by terryang -
[Jakarta Commons笔记] Co ...
[url][url][url][url][url][img][img] [lis ...
-- by xj_eon -
[Jakarta Commons笔记] Co ...
在使用时,MultiHashMap被建议不使用,为什么?有替代类?
-- by qfs_v






评论排行榜