`

VO与PO之间相互转换

 
阅读更多

转换基类:

public abstract class BaseConverter<V, P> {

    private static Logger logger = LoggerFactory.getLogger(BaseConverter.class);

    /**
     * 值对象与域对象之间属性复制
     * 
     * @param dto
     *            值对象
     * @param domain
     *            域对象
     * @param flag
     *            复制方向
     */
    protected void copyProperties(V dto, P domain, ConverterFlag flag) {

        switch (flag) {

            case PO2VO:
                copySameProperties(dto, domain);
                copyDiffPropertiesFromPO2VO(dto, domain);
                break;

            case VO2PO:
                copySameProperties(domain, dto);
                copyDiffPropertiesFromVO2PO(domain, dto);
                break;

            default:
                break;
        }

    }

    /**
     * 同名属性复制
     * 
     * @param target
     *            目标对象
     * @param source
     *            来源对象
     */
    protected void copySameProperties(Object target, Object source) {

        try {
            RtsBeanUtil.copyProperties(target, source);
        } catch (IllegalAccessException e) {
            logger.error("对象属性值复制出错:原数据为{}, 目标数据为{}。", source, target);
        } catch (InvocationTargetException e) {
            logger.error("对象属性值复制出错:原数据为{}, 目标数据为{}。", source, target);
        }
    }

    /**
     * VO非同名属性复制到PO属性
     * 
     * @param target
     *            域对象
     * @param source
     *            值对象
     */
    public abstract void copyDiffPropertiesFromVO2PO(P target, V source);

    /**
     * PO非同名属性复制到VO属性
     * 
     * @param target
     *            值对象
     * @param source
     *            域对象
     */
    public abstract void copyDiffPropertiesFromPO2VO(V target, P source);

}


日期转换工具:
public class DateConverter implements Converter {

    @SuppressWarnings("rawtypes")
    @Override
    public Object convert(Class type, Object value) {

        if (value == null) {
            return null;
        }

        // 相同类型不需要转换
        if (type.equals(value.getClass())) {
            return value;
        }

        // 字符串转日期格式
        if (value instanceof String) {
            return DateUtil.getDateFormatStr(((String) value).trim(),
                DateUtil.DATA_FORMAT_PATTERN);
        }

        // 其他不支持
        return null;
    }
}


注册自定义转换机制:
public class CustomerBeanUtil extends BeanUtils {

    private RtsBeanUtil() {

    }

    static {
        // 注册sql.date的转换器,即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空
        ConvertUtils.register(new SqlDateConverter(), java.util.Date.class);
        // 注册util.date的转换器,即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
        ConvertUtils.register(new DateConverter(), java.util.Date.class);
    }

    public static void copyProperties(Object target, Object source)
        throws InvocationTargetException, IllegalAccessException {

        BeanUtils.copyProperties(target, source);
    }

}


转换例子:
public class ParamConverter extends BaseConverter<ParamDTO, ParamInfo> {

    public ParamDTO getParamDTO(ParamInfo apply) {

        ParamDTO paramDTO = new ParamDTO();
        copyProperties(paramDTO, apply, ConverterFlag.PO2VO);

        return paramDTO;
    }

    public ParamInfo getParamInfo(ParamDTO paramDTO) {

        ParamInfo apply = new ParamInfo();
        copyProperties(paramDTO, apply, ConverterFlag.VO2PO);

        return apply;
    }

    @Override
    public void copyDiffPropertiesFromVO2PO(ParamInfo target, ParamDTO source) {

        target.setParamCode(source.getParam().getCode());
        target.setParamName(source.getParam().getText());

    }

    @Override
    public void copyDiffPropertiesFromPO2VO(ParamDTO target, ParamInfo source) {

        target.setParam(ParamCode.findByCode(source.getParamCode()));

    }

}




至此,VO与PO之间的转换完成,其中主要用的第三份工具 BeanUtil
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics