DB4O 封装(面向对象数据库),采用泛型,支持任意对象存取,可移植性贼好! 哈哈

自己封装的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: [email protected]

 * 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> List<T> query(Class<T> clazz) {

List<T> result = new ArrayList<T>();

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(DB4O_FILE_PATH);

ObjectSet<T> oSet = db.query(clazz);

while (oSet.hasNext()) {

result.add(oSet.next());

}

return result;

} finally {

if (db != null)

db.close();

}

}

 

public static <T> List<T> query(Predicate<T> predicate) {

List<T> result = new ArrayList<T>();

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(DB4O_FILE_PATH);

ObjectSet<T> oSet = db.query(predicate);

while (oSet.hasNext()) {

result.add(oSet.next());

}

return result;

} finally {

if (db != null)

db.close();

}

}

 

public static <T> boolean exists(Predicate<T> predicate) {

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(DB4O_FILE_PATH);

ObjectSet<T> oSet = db.query(predicate);

if (oSet.hasNext()) {

return true;

}

} finally {

if (db != null)

db.close();

}

 

return false;

}

 

public static <T> void store(T t) {

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(DB4O_FILE_PATH);

db.store(t);

} finally {

if (db != null)

db.close();

}

}

 

/**

* @deprecated

* the same with store() above

*/

public static <T> void cascadeStore(T t, List<Class<?>> cascadeClassList) {

EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();

cascadeClasses(configuration, cascadeClassList);

 

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(configuration, DB4O_FILE_PATH);

db.store(t);

} finally {

if (db != null)

db.close();

}

}

 

public static <T> void delete(Class<T> clazz) {

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(DB4O_FILE_PATH);

ObjectSet<T> oSet = db.query(clazz);

while (oSet.hasNext()) {

db.delete(oSet.next());

}

} finally {

if (db != null)

db.close();

}

}

 

public static <T> void delete(T t) {

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(DB4O_FILE_PATH);

ObjectSet<T> targetSet = db.queryByExample(t);

while (targetSet.hasNext()) {

db.delete(targetSet.next());

}

} finally {

if (db != null)

db.close();

}

}

 

public static <T> void delete(Predicate<T> predicate) {

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(DB4O_FILE_PATH);

ObjectSet<T> targetSet = db.query(predicate);

while (targetSet.hasNext()) {

db.delete(targetSet.next());

}

} finally {

if (db != null)

db.close();

}

}

 

public static <T> void cascadeDelete(T t, List<Class<?>> cascadeClassList) {

EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();

cascadeClasses(configuration, cascadeClassList);

 

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(configuration, DB4O_FILE_PATH);

ObjectSet<T> targetSet = db.queryByExample(t);

while (targetSet.hasNext()) {

db.delete(targetSet.next());

}

} finally {

if (db != null)

db.close();

}

}

 

public static <T> void cascadeDelete(Predicate<T> predicate, List<Class<?>> cascadeClassList) {

EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();

cascadeClasses(configuration, cascadeClassList);

 

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(configuration, DB4O_FILE_PATH);

ObjectSet<T> targetSet = db.query(predicate);

while (targetSet.hasNext()) {

db.delete(targetSet.next());

}

} finally {

if (db != null)

db.close();

}

}

 

public static <T> void update(T example, T target) {

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(DB4O_FILE_PATH);

ObjectSet<T> targetSet = db.queryByExample(example);

while (targetSet.hasNext()) {

db.delete(targetSet.next());

db.store(target);

}

} finally {

if (db != null)

db.close();

}

}

 

public static <T> void update(Predicate<T> predicate, T target) {

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(DB4O_FILE_PATH);

ObjectSet<T> targetSet = db.query(predicate);

while (targetSet.hasNext()) {

db.delete(targetSet.next());

db.store(target);

}

} finally {

if (db != null)

db.close();

}

}

 

public static <T> void cascadeUpdate(T example, T target, List<Class<?>> cascadeClassList) {

EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();

cascadeClasses(configuration, cascadeClassList);

 

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(configuration, DB4O_FILE_PATH);

ObjectSet<T> targetSet = db.queryByExample(example);

while (targetSet.hasNext()) {

db.delete(targetSet.next());

db.store(target);

}

} finally {

if (db != null)

db.close();

}

}

 

public static <T> void cascadeUpdate(Predicate<T> predicate, T target, List<Class<?>> cascadeClassList) {

EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();

cascadeClasses(configuration, cascadeClassList);

 

ObjectContainer db = null;

try {

db = Db4oEmbedded.openFile(configuration, DB4O_FILE_PATH);

ObjectSet<T> targetSet = db.query(predicate);

while (targetSet.hasNext()) {

db.delete(targetSet.next());

db.store(target);

}

} finally {

if (db != null)

db.close();

}

}

 

/*public static void main(String[] args) {

List<Class<?>> cascadeClassList = new ArrayList<Class<?>>();

cascadeClassList.add(User.class);

cascadeClassList.add(Ticket.class);

cascadeClassList.add(Passenger.class);

User u1 = createUser();

store(u1);

System.out.println("==========================stored");

printAll();

User u3 = createUser();

u3.setUserName("1111");

u3.getTickets().get(0).setFromStation("fromStation");

// update(u1, u3);

cascadeUpdate(u1, u3, cascadeClassList);

System.out.println("==========================updated");

printAll();

// delete(User.class);

// delete(Ticket.class);

// delete(Passenger.class);

cascadeDelete(u3, cascadeClassList);

System.out.println("==========================deleted");

printAll();

}

 

private static void printAll() {

printUsers();

printTickets();

printPassengers();

}

 

private static void printUsers() {

System.out.println("===Users===");

List<User> users = query(User.class);

for(User u:users){

System.out.println(u);

}

}

private static void printTickets() {

System.out.println("===Tickets===");

List<Ticket> tickets = query(Ticket.class);

for(Ticket t:tickets){

System.out.println(t);

}

}

private static void printPassengers() {

System.out.println("===Passengers===");

List<Passenger> passengers = query(Passenger.class);

for(Passenger p:passengers){

System.out.println(p);

}

}

 

private static User createUser() {

User u1 = new User();

Ticket t1 = new Ticket();

Passenger p1 = new Passenger();

u1.getTickets().add(t1);

t1.setPassenger(p1);

u1.setUserName("tom1");

u1.setPassword("123456");

t1.setFromStation("Suzhou");

t1.setToStation("Shanghai");

t1.setStartDate("2012-02-11");

t1.setStartTime("00:10");

p1.setName("tom1 deng");

p1.setIdNumber("3424000000000000");

p1.setPhone("13500000000");

return u1;

}*/

 

}


你可能感兴趣的:(面向对象)