/**
* <p>
* 將源對象的所有屬性值複製到目標對象,但是目標對象的屬性名需去除後綴或前綴,以轉換爲源對象的屬性名
* </p>
* @param to 目標拷貝對象
* @param from 拷貝源
* @param removestr 目標對象屬性名需去除的後綴或前綴
* @param isend 是否去除後綴,1爲去除後綴,其它去除前綴
* @param ignore 需要忽略的屬性
* @author zhanghj07409
*/
public static void copyRemoveString(Object to, Object from, String removestr, Integer isend, String[] ignore) {
List<String> list = null;
if (ignore == null) {
list = new ArrayList<String>();
} else {
list = Arrays.asList(ignore);
}
PropertyDescriptor[] descr = PropertyUtils.getPropertyDescriptors(to);
for (int i = 0; i < descr.length; i++) {
PropertyDescriptor d = descr[i];
if (d.getWriteMethod() == null)
continue;
if (list.contains(d.getName()))
continue;
try {
String name ="";
if(isend==1){
name =StringUtils.removeEnd(d.getName(), removestr);
}else{
name =StringUtils.removeStart(d.getName(), removestr);
}
Object value = PropertyUtils.getProperty(from, name);
PropertyUtils.setProperty(to, d.getName(), value);
} catch (Exception e) {
throw new RuntimeException("屬性名:" + d.getName() + " 在實體間拷貝時出錯", e);
}
}
}
/**
* <p>
* 將源對象的所有屬性值複製到目標對象,但是源對象的屬性名需添加後綴或前綴,以轉換爲源對象的屬性名
* </p>
* @param to 目標拷貝對象
* @param from 拷貝源
* @param addstr 源對象屬性名需添加的後綴或前綴
* @param isend 是否添加後綴,1爲添加後綴,其它添加前綴
* @param ignore 需要忽略的屬性
* @author zhanghj07409
*/
public static void copyAddString(Object to, Object from, String addstr, Integer isend, String[] ignore) {
List<String> list = null;
if (ignore == null) {
list = new ArrayList<String>();
} else {
list = Arrays.asList(ignore);
}
PropertyDescriptor[] descr = PropertyUtils.getPropertyDescriptors(to);
for (int i = 0; i < descr.length; i++) {
PropertyDescriptor d = descr[i];
if (d.getWriteMethod() == null)
continue;
if (list.contains(d.getName()))
continue;
try {
String name ="";
if(isend==1){
name =d.getName()+addstr;
}else{
name =addstr+d.getName();
}
Object value = PropertyUtils.getProperty(from, name);
PropertyUtils.setProperty(to, d.getName(), value);
} catch (Exception e) {
throw new RuntimeException("屬性名:" + d.getName() + " 在實體間拷貝時出錯", e);
}
}
}
兩個JavaBean之間的複製,但是bean的屬性名不一致。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.