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

/**
	 * <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);
			}
		}
	}
	

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值