两个JavaBean之间的复制,但是bean的属性名不一致。

/**
	 * 

* 将源对象的所有属性值复制到目标对象,但是目标对象的属性名需去除后缀或前缀,以转换为源对象的属性名 *

* @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 list = null; if (ignore == null) { list = new ArrayList(); } 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); } } } /** *

* 将源对象的所有属性值复制到目标对象,但是源对象的属性名需添加后缀或前缀,以转换为源对象的属性名 *

* @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 list = null; if (ignore == null) { list = new ArrayList(); } 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); } } }

你可能感兴趣的:(java常用函数)