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.*;
/**
* @Author: yuc
* @Description:
* @Date:
*/
/**
// db4o 版本
<dependency>
<groupId>com.db4o</groupId>
<artifactId>db4o-core-java5</artifactId>
<version>8.0.184.15484</version>
</dependency>
*/
@Component
public class Db4oUtil {
private static Logger logger = LoggerFactory.getLogger(Db4oUtil.class);
// 通过此方式向静态变量赋值
@Value("${db4o.database.name}")
private void setDatabaseName(String databaseName) {
Db4oUtil.databaseName = databaseName;
}
// 数据库文件名(全路径: ./.db/test_DB.yap)
private static String databaseName;
/**
* 条件查询
*
* @param clazz 返回对象
* @param descend 列名
* @param constrain 查询的值
* @param <T>
* @return
*/
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();
}
}
/**
* 查询所有
*
* @param clazz 返回对象
* @param <T>
* @return
*/
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();
}
}
/**
* 插入数据
*
* @param T
* @return
*/
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();
}
}
/**
* 更新数据,需要先查询到数据,再对数据进行修改
*
* @param clazz 返回对象
* @param descend 查询的列名
* @param constrain 查询的值
* @param map 要更新的数据,格式为 (要更新的列名, 要更新的值)
* @param <T>
* @return
*/
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();
}
}
/**
* 删除对象并校验,和更新操作一样,需要先查询
*
* @param clazz 返回对象
* @param descend 查询的列名
* @param constrain 查询的值
* @param confirmFiledName 要删除的列名
* @param confirmData 要删除列名对应的值
* @param <T>
* @return
*/
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();
}
}
}