📄 easyjdb.java
字号:
private boolean delRelativeObject(Object obj) {
boolean ret = true;
if(obj==null)return ret;
DBTable table = findTable(obj.getClass());
if (table != null) {
BeanWrapper wrapper = new BeanWrapper(obj);
java.util.Iterator it = table.getClassField().entrySet().iterator();
while (it.hasNext()) {
// 尝试处理其它字段
Map.Entry en = (Map.Entry) it.next();
String propertyName = (String) en.getKey();
ClassField classField = (ClassField) en.getValue();
if (classField.getClass()==ManyToManyField.class
&& wrapper.isReadableProperty(propertyName)) {
// 处理多对多
Object value = wrapper.getPropertyValue(propertyName);
if (value != null && value instanceof Collection) {
java.util.Iterator es = ((Collection) value).iterator();
while (es.hasNext()) {
Object element = es.next();
// ret=ret&this.del(element);不能删除
// 保存关联的第三方表
ManyToManyField field = (ManyToManyField) classField;
String sql = "delete from " + field.getTableName()
+ " where " + classField.getColumn()
+ "=? and " + field.getTagColumn() + "=?";
java.util.Collection paras = new java.util.ArrayList();
paras.add(wrapper.getPropertyValue(classField
.getKey()));
BeanWrapper wrapper2 = new BeanWrapper(element);
paras.add(wrapper2.getPropertyValue(field
.getTagKey()));
try {
this.execute(sql, paras);
} catch (Exception e) {
logger.error("删除多对多关系时出错!");
e.printStackTrace();
}
// }
}
}
} else if ((classField.getClass()==ManyToOneField.class)
&& wrapper.isReadableProperty(propertyName)) {
// 处理一对多
Object value = wrapper.getPropertyValue(propertyName);
if (value != null && value instanceof Collection) {
java.util.Iterator es = ((Collection) value).iterator();
while (es.hasNext()) {
ret = ret & this.del(es.next());
}
}
}
}
}
return ret;
}
/**
* 执行sql语句的接口
*
* @param sql
* String 具体的sql语句
* @return int 返回影响的行数
* @throws Exception
*/
public int execute(String sql) throws Exception {
return execute(sql, null);
}
/**
* 执行sql语句的接口
*
* @param sql
* String 具体的sql语句
* @param params
* Collection 参数值
* @return int 返回影响的行数
* @throws Exception
*/
public int execute(String sql, Collection params) throws Exception {
return dbEngine.execute(sql, params);
}
/**
* 根据表名找到其相应的对象类
*
* @param tableName
* String 关系表的名称
* @return Class 返回关系表的对应对象类
*/
public Class findClass(String tableName) {
String className = this.mapping.findClass(tableName);
Class cls = null;
try {
cls = Class.forName(className);
} catch (Exception e) {
logger.error("配置文件中的类不存在!" + e);
}
return cls;
}
/**
* 根据对象类型返回表的定义信息
*
* @param cls
* Class 对象类型
* @return DBTable 关系表定义信息
*/
private DBTable findTable(Class cls) {
DBTable table = this.mapping.findTable(cls);
return table;
}
/**
* 产生对象obj的主键值
*
* @param obj
* Object 需要产生主键的对象
* @param table
* DBTable 对应关系表的定义信息
*/
private void generatorID(Object obj, DBTable table) {
/**
* 若obj已经有主键值则直接返回
*/
if ((table.getIdGenerator() == null)
|| "".equals(table.getIdGenerator())) {
return;
}
try {
BeanWrapper wrapper = new BeanWrapper(obj);
Object tmpId = wrapper.getPropertyValue(table.getId());
Class keyType=wrapper.getPropertyType(table.getId());
if (tmpId == null || "".equals(tmpId)) { // 只有ID为空的才产生ID
Class cls = Class.forName(table.getIdGenerator());
if (cls == NullIdGenerator.class)
return;// 不需要设置ID键值
IIdGenerator idGenerator = (IIdGenerator) cls.newInstance();
//设置db
if (idGenerator instanceof IdGenerator)
((IdGenerator) idGenerator).setDb(this);
//产生key
Object key = idGenerator.generator(obj.getClass());
//转换key
if(idGenerator instanceof IdGenerator)
key=com.easyjf.util.NumberUtils.convertNumberToTargetClass((Number)key, keyType);
logger.debug("产生了key值:" + key);
if (key != null) {
wrapper.setPropertyValue(table.getId(), key);
}
}
} catch (Exception e) {
logger.error("主键产生错误!" + e);
e.printStackTrace();
}
// Method tableNameMethod =
// obj.getClass().getMethod("getTableName",null);
}
/**
* 把对象obj转换成泛数据表DBObject对象
*
* @param obj
* Object 需要转换的对象
* @return DBObject 转换的结果
*/
private DBObject obj2dbo(Object obj) {
/**
* 若obj本来就是泛数据表DBOjbect对象,则直接返回
*/
if (obj.getClass() == DBObject.class) {
return (DBObject) obj;
}
DBTable table = findTable(obj.getClass());
DBObject dbo = new DBObject(table);
dbo.setValue(obj2dboMap(obj, table));
dbo.setIdValue(dbo.get(table.getId()));
return dbo;
}
/**
* 把对象obj中的数据保存到Map中 table只保存table中定义的字段
*
* @param obj
* 要转换的对象
* @param table
* 表的定义信息
* @return
*/
private Map obj2dboMap(Object obj, DBTable table) {
Map map = new java.util.HashMap();
generatorID(obj, table);
BeanWrapper wrapper = new BeanWrapper(obj);
java.util.Iterator it=table.getAllFields().entrySet().iterator();
PropertyDescriptor descriptors[] = wrapper.getPropertyDescriptors();
while(it.hasNext()){
Map.Entry en=(Map.Entry)it.next();
String name = (String)en.getKey();
if(wrapper.isReadableProperty(name)){
DBField field = table.findField(name);
try {
Object value = wrapper.getPropertyValue(name);
//if ((descriptors[i].getReadMethod() != null) && (value != null)) {
if (field != null) {
// 原始类型直接保存
map.put(field.getName(), value);
} else {
// 其它关联类型
ClassField classField = table.findClassField(name);
if ((classField != null) && !BeanUtils.checkLazyloadNull(value)) {
// System.out.println("其它类型:" + name);
if (classField instanceof OneToOneField) {
BeanWrapper innerWrapper = new BeanWrapper(
value);
String innerPoperty = this.findTable(
classField.getType()).getPoperty(
classField.getKey());
if(innerPoperty==null)throw new EasyDBOException("找不到名为"+classField.getKey()+"的映射属性!");
Object realValue = innerWrapper.getPropertyValue(innerPoperty);
map.put(classField.getColumn(), realValue);
} else if (classField instanceof ManyToOneField) {
// 这里需要添加一对多的保存代码,把其下属对象也保存起来
}
}
}
//}
} catch (Exception e) {
throw new EasyDBOException("把PO转换成数据层对象出错!",e);
}}
}
return map;
}
/**
* 执行用户自定义sql语句,直接返回泛数据表DBObject对象列表
*
* @param sql
* String 自定义的sql语句
* @return List 泛数据表DBObject对象列表
*/
public List query(String sql) {
return dbEngine.query(sql);
}
//
/**
* 执行条件查询,返回类型为cls的对象列表
*
* @param cls
* Class 对象类型(即PO的类型)
* @param scope
* String 查询条件
* @return List 类型为cls的对象列表
*/
public List query(Class cls, String scope) {
return query(cls, scope, null);
}
/**
* 根据用户自定义的sql语句及查询参数执行查询,返回泛数据表DBObject对象列表
*
* @param sql
* String 用户自定义sql语句
* @param params
* Collection 查询参数集合
* @return List 泛数据表DBObject对象列表
*/
public List query(String sql, Collection params) {
return query(sql, params, 0, -1);
}
/**
* 根据查询条件及参数执行查询,返回类型为cls的对象列表
*
* @param cls
* Class 对象类型(即PO的类型)
* @param scope
* String 查询条件
* @param params
* Collection 查询条件参数值
* @return List 类型为cls的对象列表
*/
public List query(Class cls, String scope, Collection params) {
return query(cls, scope, params, 0, -1);
}
/**
* 根据用户自定义的sql语句执行查询操作,返回从begin开始,max条记录的泛数据表DBObject对象列表
*
* @param sql
* String 用户自定义sql语句
* @param params
* Collection 查询参数值
* @param begin
* int 返回结果集的起始位置
* @param max
* int 最多返回的条数
* @return List 泛数据表DBObject对象列表
*/
public List query(String sql, Collection params, int begin, int max) {
return query(sql, params, begin, max, false);
}
/**
* 根据用户自定义的sql语句执行查询操作,返回从begin开始,max条记录,类型为cls的对象列表
*
* @param cls
* Class 对象类型
* @param scope
* String 查询条件
* @param params
* Collection 查询条件参数值
* @param begin
* int 返回结果集的开始位置
* @param max
* int 最多返回的条数
* @return List 类型为cls的对象列表
*/
public List query(Class cls, String scope, Collection params, int begin,
int max) {
return query(cls, scope, params, begin, max, false);
}
/**
* 根据用户自定义的sql语句执行查询操作,返回从begin开始,max条记录的泛数据表DBObject对象列表
*
* @param sql
* String 用户自定义的查询sql
* @param params
* Collection 查询参数值
* @param begin
* int 返回记录集的起始位置
* @param max
* int 最多返回的记录数
* @param cache
* boolean 是否使用缓存功能
* @return List 泛数据表DBObject对象列表
*/
public List query(String sql, Collection params, int begin, int max,
boolean cache) {
List ret;
if (cache && enableCache) {
ret = (List) innerCache.get(sql, params, begin, max);
if (ret != null) {
return ret;
}
ret = dbEngine.query(sql, params, begin, max);
if (ret != null) {
innerCache.put(sql, params, begin, max, ret);
}
} else {
ret = dbEngine.query(sql, params, begin, max);
}
return ret;
}
/**
* 根据用户自定义的sql语句执行查询操作,返回从begin开始,max条记录,类型为cls的对象列表
*
* @param cls
* Class 对象类型
* @param scope
* String 查询条件
* @param params
* Collection 查询条件参数值
* @param begin
* int 记录起始位置
* @param max
* int 最多返回的记录数
* @param cache
* boolean 是否使用缓存功能
* @return List 类型为cls的对象列表
*/
public List query(Class cls, String scope, Collection params, int begin,
int max, boolean cache) {
List ret;
if (enableCache && cache && (cls != DBObject.class)) {
ret = (List) innerCache.get(cls, scope, params, begin, max);
if (ret != null) {
return ret;
}
}
DBTable table = findTable(cls);
List list = null;
if (table!=null && table.getId() != null) { // 先查ID,然后根据ID从缓存中读取数据
String sql = "select " + table.getId() + " from " + table.getName()
+ " where " + scope;
list = dbEngine.query(sql, params, begin, max);
if (list != null) {
for (int i = 0; i < list.size(); i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -