Db4o一个轻巧的、面向对象的数据库。为了方便调用,将新建、查询、更新、删除操作封装到了Db4oUtil工具类,懒人可一键调用哦哈哈哈
import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.*;
@Component
public class Db4oUtil {
private static Logger logger = LoggerFactory.getLogger(Db4oUtil.class);
@Value("${db4o.database.name}")
private void setDatabaseName(String databaseName) {
Db4oUtil.databaseName = databaseName;
}
private static String databaseName;
public synchronized static <T> List<T> searchLimit(Class clazz, String descend, String constrain) {
ObjectContainer db = null;
try {
db = Db4o.openFile(databaseName);
Query query = db.query();
query.constrain(clazz);
query.descend(descend).constrain(constrain);
List<T> results = query.execute();
if (results == null || results.size() <= 0) {
return null;
} else {
return new ArrayList<>(results);
}
} catch (Exception e) {
logger.error("查询操作失败", e);
return null;
} finally {
if (db != null)
db.close();
}
}
public synchronized static <T> List<T> search(Class clazz) {
ObjectContainer db = null;
try {
db = Db4o.openFile(databaseName);
Query query = db.query();
query.constrain(clazz);
List<T> results = query.execute();
if (results == null || results.size() <= 0) {
return null;
} else {
return new ArrayList<>(results);
}
} catch (Exception e) {
logger.error("查询操作失败", e);
return null;
} finally {
if (db != null)
db.close();
}
}
public synchronized static <T> boolean create(T T) {
ObjectContainer db = null;
try {
db = Db4o.openFile(databaseName);
db.store(T);
db.commit();
return true;
} catch (Exception e) {
logger.error("创建操作失败", e);
return false;
} finally {
if (db != null)
db.close();
}
}
public synchronized static <T> boolean updateLimit(Class clazz, String descend, String constrain, Map<Object, Object> map) {
ObjectContainer db = null;
try {
db = Db4o.openFile(databaseName);
Query query = db.query();
query.constrain(clazz);
query.descend(descend).constrain(constrain);
List<T> results = query.execute();
if (results == null || results.size() <= 0) {
return false;
} else {
Object object = results.get(0);
Set<Map.Entry<Object, Object>> entries = map.entrySet();
for (Map.Entry entry : entries) {
Field declaredField = clazz.getDeclaredField(entry.getKey().toString());
declaredField.setAccessible(true);
declaredField.set(object, entry.getValue());
}
db.store(object);
db.commit();
return true;
}
} catch (Exception e) {
logger.error("更新操作失败", e);
return false;
} finally {
if (db != null)
db.close();
}
}
public synchronized static <T> boolean delete(Class clazz, String descend, String constrain, String confirmFiledName, String confirmData) {
ObjectContainer db = null;
try {
db = Db4o.openFile(databaseName);
Query query = db.query();
query.constrain(clazz);
query.descend(descend).constrain(constrain);
List<T> results = query.execute();
if (results == null || results.size() <= 0) {
return false;
} else {
Object object = results.get(0);
Field declaredField = clazz.getDeclaredField(confirmFiledName);
declaredField.setAccessible(true);
if (declaredField.get(object).equals(confirmData)) {
db.delete(object);
db.commit();
return true;
} else
return false;
}
} catch (Exception e) {
logger.error("删除操作失败", e);
return false;
} finally {
if (db != null)
db.close();
}
}
}
|