Spring BeanUtils.copyProperties nullでない属性のみコピー
一:問題 BeanUtils.copyPropertiesコピー属性nullは除外されません
apacheとspringのツールパッケージにはBeanUtilsがあり、その中のcopyPropertiesメソッドを使用するとこれらの作業を非常に便利に行うことができますが、実際の応用ではnullの処理は個人のニーズに合わないことがわかりました.例えば、修正操作を行うにはmodelのいずれかを修正する必要があります.では、一般的にはmodelのIDと変更が必要な値だけをページに提出します.この場合、BeanUtils.copyPropertiesを使用して他のnullをpojoにバインドします.
BeanUtils.copyPropertiesの使用をインポートするには、次の手順に従います.
二:問題解決処理 BeanUtils.copyProperties null属性のcopyを除外
私たちは自分で加工クラスを書いて、みんなは直接私たちの加工クラスのcopyPropertiesIgnoreNull()方法を呼び出すことができてnull値を無視することができて、古いデータがnullに覆われる気まずいことを避けることができます:
apacheとspringのツールパッケージにはBeanUtilsがあり、その中のcopyPropertiesメソッドを使用するとこれらの作業を非常に便利に行うことができますが、実際の応用ではnullの処理は個人のニーズに合わないことがわかりました.例えば、修正操作を行うにはmodelのいずれかを修正する必要があります.では、一般的にはmodelのIDと変更が必要な値だけをページに提出します.この場合、BeanUtils.copyPropertiesを使用して他のnullをpojoにバインドします.
BeanUtils.copyPropertiesの使用をインポートするには、次の手順に従います.
org.springframework.beans.BeanUtils;
import com.hourumiyue.system.SpringUtil;
import org.springframework.beans.BeanUtils;
public class TestBeanUtiles {
public static void main(String[] args) {
NewPerson newPerson = new NewPerson();
newPerson.setName("miyue");// ,
//
OldPerson oldPerson = new OldPerson();
oldPerson.setSex("nv");
oldPerson.setAge(5);
// , BeanUtils.copyProperties() :
BeanUtils.copyProperties(newPerson, oldPerson);
System.out.println(newPerson.toString());
System.out.println(oldPerson.toString());
}
}
:
NewPerson{name='miyue', sex='null', age=0}
OldPerson{name='miyue', sex='null', age=0}
上から分かるように、新しいオブジェクトは確かに修正されたデータを古いオブジェクトに更新したが、nullまたはintタイプのデフォルトが0の値を古いオブジェクトに与えたのは合理的ではないに違いない.二:問題解決処理 BeanUtils.copyProperties null属性のcopyを除外
私たちは自分で加工クラスを書いて、みんなは直接私たちの加工クラスのcopyPropertiesIgnoreNull()方法を呼び出すことができてnull値を無視することができて、古いデータがnullに覆われる気まずいことを避けることができます:
import com.hourumiyue.system.SpringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.util.HashSet;
import java.util.Set;
public class TestBeanUtiles {
public static String[] getNullPropertyNames (Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set emptyNames = new HashSet();
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 src, Object target){
BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
}
public static void main(String[] args) {
NewPerson newPerson = new NewPerson();
newPerson.setName("miyue");// ,
//
OldPerson oldPerson = new OldPerson();
oldPerson.setSex("nv");
oldPerson.setAge(5);
// , BeanUtils.copyProperties() :
//BeanUtils.copyProperties(newPerson, oldPerson);
copyPropertiesIgnoreNull(newPerson, oldPerson);
System.out.println(newPerson.toString());
System.out.println(oldPerson.toString());
}
}
:
NewPerson{name='miyue', sex='null', age=0}
OldPerson{name='miyue', sex='nv', age=0}