📄 easyjdb.java
字号:
DBObject tmpO = (DBObject) list.get(i);
Object cid = tmpO.get(table.getId());
list.set(i, get(cls, cid));
}
}
} else { // 若没有主键同,中直接从数据库中查询
String sql =DBObject.class.isAssignableFrom(cls)?scope:"select * from " + table.getName() + " where " + scope;
list = dbEngine.query(sql, params, begin, max);
if (list != null) {
for (int i = 0; i < list.size(); i++) {
list.set(i, dbo2obj((DBObject) list.get(i), cls));
}
}
}
ret = list;
if (enableCache && cache && (ret != null)) {
innerCache.put(cls, scope, params, begin, max, ret);
}
return ret;
}
/**
* 根据查询条件从数据表中读出一个对象,若符合条件的记录有多个,则该方法只返回第一个对象,其它的将被忽略。
*
* @param cls
* Class 对象类型
* @param scope
* String 查询条件
* @return Object 返回cls类型的对象
*/
public Object read(Class cls, String scope) {
Object obj;
/**
* 先从缓存中取数据,若找到数据则直接返回
*/
if (enableCache && (cls != DBObject.class)) {
obj = innerCache.get(cls, scope);
if (obj != null) {
return obj;
}
}
DBTable tempTable = findTable(cls);
if (tempTable.getLazy() == true) {
// 设置类型
tempTable.setTypes("Class");
tempTable.setTypes("String");
// 设置参数
tempTable.setParams(cls);
tempTable.setParams(scope);
try {
return cls.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
DBObject dbo = dbEngine.get(new DBObject(tempTable), scope, null);
obj = dbo2obj(dbo, cls);
/**
* 把结果保存到缓存中
*/
if (enableCache && (obj != null)) {
innerCache.put(cls, scope, obj);
}
return obj;
}
/**
* 根据查询条件及具体的参数从数据表中读出一个对象,若符合条件的记录有多个,则该方法只返回第一个对象,其它的将被忽略。
*
* @param cls
* Class 对象类型
* @param scope
* String 查询条件
* @param params
* Collection 查询参数值
* @return Object 返回cls类型的对象
*/
public Object read(Class cls, String scope, Collection params) {
Object obj;
/**
* 先从缓存中读取对象,若找到则直接返回
*/
if (enableCache && (cls != DBObject.class)) {
obj = innerCache.get(cls, scope, params, 0, 0);
if (obj != null) {
return obj;
}
}
// 这里改成先读取条件Scope条件的ID
DBTable table = findTable(cls);
String sql = "select " + table.getId() + " from " + table.getName()
+ " where " + scope;
Object idValue = dbEngine.uniqueResult(sql, params);
if (idValue == null)
return null;
// DBObject dbo = db.get(new DBObject(findTable(cls)), scope, params);
// obj = dbo2obj(dbo, cls);
obj = get(cls, idValue);
if (enableCache && (obj != null)) {
innerCache.put(cls, scope, params, 0, 0, obj);
}
return obj;
}
/**
* 把对象持久化到数据库中,先偿试添加,若无法保存则执行修改操作
*
* @param obj
* Object 要保存的对象
* @return boolean 返回操作是否成功
*/
public boolean saveOrUpdate(Object obj) {
logger.info("把对象持久化到数据库中,先偿试添加,若无法保存则执行修改操作");
boolean ret = false;
try {
DBTable table = findTable(obj.getClass());
BeanWrapper wrapper = new BeanWrapper(obj);
Object id = wrapper.getPropertyValue(table.getKeyFiled().getName());
if (id == null)
ret = add(obj);
else {
if (get(obj.getClass(), id) != null)
ret = update(obj);
else
ret = add(obj);
}
} catch (Exception e) {
// 出错,进一步尝试使用原始的方法进行更改
// e.printStackTrace();
if (add(obj)) {
return true;
}
return update(obj);
}
return ret;
}
//
/**
* 采用不缓存的方式,执行一条sql语句,返回唯一的对象。如select count(*) from message,将返回一个Number对象
*
* @param sql
* String 用户自定义的sql语句
* @return Object 返回的对象
*/
public Object uniqueResult(String sql) {
return uniqueResult(sql, false);
}
/**
* 执行一条sql语句,返回唯一的对象。如select count(*) from message,将返回一个Number对象
*
* @param sql
* String 用户自定义的sql语句
* @param cache
* boolean 是否使用缓存
* @return Object 返回的结果
*/
public Object uniqueResult(String sql, boolean cache) {
Object obj;
if (enableCache && cache) {
obj = innerCache.get(sql);
if (obj != null) {
return obj;
}
obj = dbEngine.uniqueResult(sql);
if (obj != null) {
innerCache.put(sql, obj);
}
} else {
obj = dbEngine.uniqueResult(sql);
}
return obj;
}
/**
* 不使用缓存,根据自定义sql语句及查询参数执行查询,返回唯一的对象。如select count(*) from message where
* belongUser=?,将返回一个Number对象
*
* @param sql
* String 用户自定义sql语句
* @param params
* Collection 查询参数值
* @return Object 查询的结果
*/
public Object uniqueResult(String sql, Collection params) {
return uniqueResult(sql, params, false);
}
/**
* 根据自定义sql语句及查询参数执行查询,返回唯一的对象。如select count(*) from message where
* belongUser=?,将返回一个Number对象
*
* @param sql
* String 用户自定义sql语句
* @param params
* Collection 查询参数值
* @param cache
* boolean 是否使用缓存
* @return Object 查询的结果
*/
public Object uniqueResult(String sql, Collection params, boolean cache) {
Object obj;
if (enableCache && cache) {
obj = innerCache.get(sql, params);
if (obj != null) {
return obj;
}
obj = dbEngine.uniqueResult(sql, params);
if (obj != null) {
innerCache.put(sql, params, obj);
}
} else {
obj = dbEngine.uniqueResult(sql, params);
}
return obj;
}
/**
* 根据主键加载一个表中的某一个列
*
* @param table
* 表
* @param field
* 字段名
* @param value
* 主键值
* @return
*/
public Object uniqueResult(DBTable table, DBField field,
java.io.Serializable value) {
String sql = "select " + field.getName() + " from " + table.getName()
+ " where " + table.getKeyFiled() + "=?";
java.util.Collection paras = new java.util.ArrayList();
paras.add(value);
return uniqueResult(sql, paras);
}
/**
* 调用存储过程的例子
*
* @param procedureName
*/
public void callUpdate(String procedureName) {
this.dbEngine.callQuery(procedureName);
}
public void callUpdate(String procedureName, Object[] parameter) {
this.dbEngine.callQuery(procedureName, parameter);
}
public List callQuery(String procedureName) {
return this.dbEngine.callQuery(procedureName);
}
public List callQuery(String procedureName, Object[] parameter) {
return this.dbEngine.callQuery(procedureName, parameter);
}
/**
* 更新持久层中的数据表对象
*
* @param obj
* Object 要更新的对象
* @return boolean 更新是否成功
*/
public boolean update(Object obj) {
logger.info("更新持久层中的数据表对象");
boolean ret = dbEngine.update(obj2dbo(obj));
ret = ret & this.addRelativeObject(obj);
return ret;
}
// ~--- get methods --------------------------------------------------------
/**
* 根据对象类型cls及主键值从特久设备中读读取对象,若找不到对象则返回null.
*
* @param cls
* Class 对象类型
* @param id
* Object 主键值
* @return Object 找到数据则返回cls类型的对象,否则返回null
*/
public Object get(Class cls, Object id) {
Object obj;
if (enableCache && (cls != DBObject.class)) {
obj = innerCache.get(cls, id);
if (obj != null) {
return obj;
}
}
DBObject dbo = dbEngine.get(new DBObject(findTable(cls)), id);
obj = dbo2obj(dbo, cls);
if (enableCache && (obj != null)) {
innerCache.put(cls, id, obj);
}
return obj;
}
// 释放数据源
public void close() {
dbEngine.releaseConnection();
}
public void setAutoCommit(boolean auto) {
dbEngine.setAutoCommit(auto);
}
public void commit() {
dbEngine.commit();
}
public EasyJDBEngine getDbEngine() {
return dbEngine;
}
public boolean isEnableCache() {
return enableCache;
}
public void setDataSource(javax.sql.DataSource dataSource) {
this.dbEngine.setDataSource(dataSource);
}
public void setSqlQuery(ISqlQuery sqlQuery) {
this.dbEngine.setSqlQuery(sqlQuery);
}
public void setEnableCache(boolean enableCache) {
this.enableCache = enableCache;
if (this.enableCache && this.innerCache == null)
this.innerCache = new DboCache(cacheName);
}
public DboCache getInnerCache() {
return innerCache;
}
public void setInnerCache(DboCache innerCache) {
this.innerCache = innerCache;
}
public DBMapping getMapping() {
return mapping;
}
public void setMapping(DBMapping mapping) {
this.mapping = mapping;
}
public void setShowSql(boolean showSql) {
this.dbEngine.setShowSql(showSql);
}
public void setMaxSize(int size) {
this.dbEngine.setMaxSize(size);
}
public String getCacheName() {
return cacheName;
}
public void setConfigFiles(java.util.List configFile) {
this.configFiles = configFile;
if (configFile != null && configFile.size() > 0)
loadConfigFile();
}
public void setCacheName(String cacheName) {
this.cacheName = cacheName;
if (cacheName == null || "".equals(cacheName)) {
this.enableCache = true;
this.cacheName = cacheName;
this.innerCache = new DboCache(cacheName);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -