日期:2014-05-16 浏览次数:20734 次
自己封装的DB4O Template, 可很方便的进行对象保存,更新,级联更新及删除! 废话不多说, 自己看源码:
?
/**
?* DB4OUtils.java
?* com.dzg.ticket.util
?*
?* ? ver ? ? date ? ? ? author
?* ──────────────────────────────────
?* ? 2012-2-10 Administrator
?*
?* Copyright (c) 2012, TNT All Rights Reserved.
*/
?
package utils;
?
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
?
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Predicate;
//import com.dzg.ticket.bo.Passenger;
//import com.dzg.ticket.bo.Ticket;
//import com.dzg.ticket.bo.User;
?
/**
?* ClassName:DB4OUtils
?* Copyright: copyright (c)2012
?* @author ? Deng Zhiguang
?* @Date 2012-2-10
?* Email: dengzhiguang@gmail.com
?* Modification History:
?* Date ? ? ?Author ? ? ?Version ? ?Reason
?* ------------------------------------------
?*/
public class DB4OTemplate {
private final static String DB4O_FILE_PATH = getClassPath() + "/db.yap";
?
private static String getClassPath() {
URL url = DB4OTemplate.class.getResource("/");
String path = null;
try {
path = URLDecoder.decode(url.getPath(), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
?
return path;
}
?
private static void cascadeClasses(EmbeddedConfiguration configuration, List<Class<?>> classList) {
for (Class<?> c : classList) {
configuration.common().objectClass(c).cascadeOnUpdate(true);
configuration.common().objectClass(c).cascadeOnDelete(true);
}
}
?
public static <T> List<T> queryByExample(T t) {
List<T> result = new ArrayList<T>();
ObjectContainer db = null;
try {
db = Db4oEmbedded.openFile(DB4O_FILE_PATH);
ObjectSet<T> oSet = db.queryByExample(t);
while (oSet.hasNext()) {
result.add(oSet.next());
}
return result;
} finally {
if (db != null)
db.close();
}
}
?
public static <T&