面向 Java 开发人员的 db4o 指南: 超越简单对象
2010-04-01 00:00:00 来源:WEB开发网清单 3 中的代码创建了两个到达婚龄的 Person,代码和您预想的很接近:
清单 3. 去礼堂,要结婚了……
import java.util.*;
import com.db4o.*;
import com.db4o.query.*;
import com.tedneward.model.*;
public class App
{
public static void main(String[] args)
throws Exception
{
ObjectContainer db = null;
try
{
db = Db4o.openFile("persons.data");
Person ben = new Person("Ben", "Galbraith",
Gender.MALE, 29, Mood.HAPPY);
Person jess = new Person("Jessica", "Smith",
Gender.FEMALE, 29, Mood.HAPPY);
ben.setSpouse(jess);
System.out.println(ben);
System.out.println(jess);
db.set(ben);
db.commit();
List<Person> maleGalbraiths =
db.query(new Predicate<Person>() {
public boolean match(Person candidate) {
return candidate.getLastName().equals("Galbraith") &&
candidate.getGender().equals(Gender.MALE);
}
});
for (Person p : maleGalbraiths)
{
System.out.println("Found " + p);
}
}
finally
{
if (db != null)
db.close();
}
}
}
更多精彩
赞助商链接