提交 8ed0a3ff 编写于 作者: chenxiang@lab.ibiz5.com's avatar chenxiang@lab.ibiz5.com

NoSQL:MongoDB适配、更新空值处理

上级 8227c7fe
......@@ -2,5 +2,14 @@ package cn.ibizlab.util.domain;
public class EntityMongo extends EntityBase {
public void modify(String field, Object val) {
if (val == null) {
this.getFocusNull().add(field.toLowerCase());
} else {
this.getFocusNull().remove(field.toLowerCase());
}
}
}
package cn.ibizlab.util.helper;
import cn.ibizlab.util.domain.EntityBase;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class CachedBeanCopier {
public static void copy(Object srcObj, Object destObj) {
copy(srcObj, destObj, false);
}
public static void copy(Object srcObj, Object destObj, boolean useConverter) {
if (srcObj == null || destObj == null) {
return;
}
BeanUtils.copyProperties(srcObj, destObj);
}
public static void copyWithFocusNull(EntityBase source, EntityBase target) {
if (source == null || target == null) {
return;
}
Class<?> actualEditable = target.getClass();
Class<?> sourceClass = source.getClass();
PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
for (PropertyDescriptor targetPd : targetPds) {
if (targetPd.getWriteMethod() == null) {
continue;
}
PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(sourceClass, targetPd.getName());
if (sourcePd == null || sourcePd.getReadMethod() == null) {
continue;
}
try {
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
if (value == null) {
if (source.getFocusNull().contains(sourcePd.getName().toLowerCase())) {
setValue(target, targetPd, value);
}
} else {
setValue(target, targetPd, value);
}
} catch (Exception ex) {
throw new FatalBeanException("Could not copy properties from source to target", ex);
}
}
}
private static void setValue(Object target, PropertyDescriptor targetPd, Object value) throws IllegalAccessException, InvocationTargetException {
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
}
package cn.ibizlab.util.repository;
import cn.ibizlab.util.domain.EntityMongo;
import cn.ibizlab.util.filter.MongoQueryContext;
import cn.ibizlab.util.helper.CachedBeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
......@@ -10,8 +11,9 @@ import org.springframework.data.mongodb.repository.support.SimpleMongoRepository
import org.springframework.data.support.PageableExecutionUtils;
import java.util.List;
import java.util.Optional;
public class IBZMongoSimpleRepository<T, ID> extends SimpleMongoRepository<T, ID> implements IBZMongoRepository<T, ID> {
public class IBZMongoSimpleRepository<T extends EntityMongo, ID> extends SimpleMongoRepository<T, ID> implements IBZMongoRepository<T, ID> {
private final MongoOperations mongoOperations;
private final MongoEntityInformation<T, ID> entityInformation;
......@@ -22,10 +24,25 @@ public class IBZMongoSimpleRepository<T, ID> extends SimpleMongoRepository<T, ID
this.mongoOperations = mongoOperations;
}
@Override
public <S extends T> S save(S entity) {
//更新空值处理
ID id = entityInformation.getId(entity);
if (id != null) {
Optional<T> op = findById(id);
if (op.isPresent()) {
T t = op.get();
CachedBeanCopier.copyWithFocusNull(entity,t);
entity = (S) t;
}
}
return super.save(entity);
}
@Override
public <S extends T> Page<T> query(MongoQueryContext mongoQueryContext) {
Query query = mongoQueryContext.getSearchCond();
query.with(mongoQueryContext.getPageable());
query.with(mongoQueryContext.getPageable());
if(mongoQueryContext.getPageSort()!=null)
query.with(mongoQueryContext.getPageSort());
List<T> list = mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName());
......
package cn.ibizlab.util.helper;
import org.springframework.beans.BeanUtils;
public class CachedBeanCopier {
public static void copy(Object srcObj, Object destObj) {
copy(srcObj,destObj,false);
}
public static void copy(Object srcObj, Object destObj,boolean useConverter) {
if(srcObj==null||destObj==null) {
return;
}
BeanUtils.copyProperties(srcObj, destObj);
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册