Python 官方文档:入门教程 => 点击学习
目录java复制非空对象属性值然后封装一下得到以下方法测试方法就不提供了,自行测试即可java对象属性复制的几种方式1.使用java反射机制2.使用spring-beans5.0.8
很多时候,我们需要通过对象拷贝,比如说VO类与数据库实体bean类、更新时非空对象不更新,对同一对象不同数据分开存储等
用于对象拷贝,spring 和 Apache都提供了相应的工具类方法,BeanUtils.copyProperties
但是对于非空属性拷贝就需要自己处理了
在这里借用spring中org.springframework.beans.BeanUtils类提供的方法
copyProperties(Object source, Object target, String... ignoreProperties)
public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {
copyProperties(source, target, null, ignoreProperties);
private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclarinGClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}
public static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null)
emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
public static void copyPropertiesIgnoreNull(Object source, Object target) {
BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
}
如果项目中使用的框架有Hibernate的话,则可以通过在实体类上添加下面两条注解
@DynamicInsert(true)
@DynamicUpdate(true)
如果想对该注解进一步了解的话,那么可以去官网看英文文档,文档解释的很清楚,在此不再赘述了
获取对象的属性和get、set方法进行复制;
import org.springframework.beans.BeanUtils;
SourceObject sourceObject = new SourceObject();
TargetObject targetObject = new TargetObject();
BeanUtils.copyProperties(sourceObject, targetObject);
import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.core.Converter;
SourceObject sourceObject = new SourceObject();
TargetObject targetObject = new TargetObject();
BeanCopier beanCopier = BeanCopier.create(SourceObject.class, TargetObject.class, true);--第三个参数表示是否使用转换器,false表示不使用,true表示使用
Converter converter = new CopyConverter();--自定义转换器
beanCopier.copy(sourceObject, targetObject, converter);
转换器(当源对象属性类型与目标对象属性类型不一致时,使用转换器):
import net.sf.cglib.core.Converter;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.util.Date;
public class CopyConverter implements Converter {
@Override
public Object convert(Object value, Class target, Object context) {
{
String s = value.toString();
if (target.equals(int.class) || target.equals(Integer.class)) {
return Integer.parseInt(s);
}
if (target.equals(long.class) || target.equals(Long.class)) {
return Long.parseLong(s);
}
if (target.equals(float.class) || target.equals(Float.class)) {
return Float.parseFloat(s);
}
if (target.equals(double.class) || target.equals(Double.class)) {
return Double.parseDouble(s);
}
if(target.equals(Date.class)){
while(s.indexOf("-")>0){
s = s.replace("-", "/");
}
return new Date(s);
}
if(target.equals(BigDecimal.class)){
if(!StringUtils.isEmpty(s)&&!s.equals("NaN")){
return new BigDecimal(s);
}
}
return value ;
}
}
}
中的org.springframework.cglib.beans.BeanCopier类(用法与第三种一样)
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.core.Converter;
SourceObject sourceObject = new SourceObject();
TargetObject targetObject = new TargetObject();
Converter converter = new SpringCopyConverter();
BeanCopier beanCopier = BeanCopier.create(SourceObject.class, TargetObject.class, true);
beanCopier.copy(sourceObject, targetObject, converter);
经过循环复制测试(源对象与目标对象各160个属性):
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: java 如何复制非空对象属性值
本文链接: https://lsjlt.com/news/134248.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0