Skip to content

1.0.2版本详情

agapple edited this page Sep 27, 2013 · 4 revisions

版本变更内容

  1. convetor包名重构,拼写错误
  2. ScriptExecutor接口重构,加上ScriptContext操作
  3. FiledSetExecutor去除对args的强制匹配依赖
  4. mapping支持name定义
  5. mapping支持嵌套映射/集合映射对象的name定义
  6. 增加NullSymbolGetExecutor的实现,支持无源属性的映射
主要增加的两个特性:

特性1:mapping支持别名定义

可以解决的需求:

  • map<->map之间可以有不同的映射规则,服务化接口用map这样弱类型的情况会比较多.
  • 嵌套映射,集合映射都可以指定特定的映射规则,改变原先的默认按照class类型查找的方式

@Test
    public void testCollectionAndNestedNameMapping() {// 测试嵌套collection mapping处理
        BeanMappingBuilder builder = new BeanMappingBuilder() {
        protected void configure() {
            behavior().debug(true);// 设置行为
            mapping(HashMap.class, HashMap.class);
            fields(srcField(ONE, String.class), targetField(ONE_OTHER, BigInteger.class));
            fields(srcField(TWO, List.class).componentClasses(HashMap.class),
                   targetField(TWO_OTHER, List.class).componentClasses(HashMap.class)).nestName("nestedMapping");
        }

    };

    // 第三层mapping
    final BeanMappingBuilder nextNestedMapping = new BeanMappingBuilder() {

        protected void configure() {
            behavior().debug(true);// 设置行为
            mapping("nextNestedMapping", HashMap.class, HashMap.class);
            fields(srcField(THREE), targetField(THREE_OTHER));
        }

    };

    // 第二层mapping
    BeanMappingBuilder nestedMapping = new BeanMappingBuilder() {

        protected void configure() {
            behavior().debug(true);// 设置行为
            mapping("nestedMapping", HashMap.class, HashMap.class);
            fields(srcField(TWO), targetField(TWO_OTHER));
            fields(srcField(THREE), targetField(THREE_OTHER)).recursiveMapping(true).nestObject(nextNestedMapping);
        }

    };

    BeanMappingConfigHelper.getInstance().register(nextNestedMapping);
    BeanMappingConfigHelper.getInstance().register(nestedMapping);
    BeanMappingConfigHelper.getInstance().register(builder);

    BeanMapping mapping = new BeanMapping(builder);
    Map nextNestedMap1 = new HashMap();
    nextNestedMap1.put(THREE, "nextNestedMap three1");

    Map nestedMap1 = new HashMap();
    nestedMap1.put(TWO, "nestedMap two1");
    nestedMap1.put(THREE, nextNestedMap1);

    Map nextNestedMap2 = new HashMap();
    nextNestedMap2.put(THREE, "nextNestedMap three2");

    Map nestedMap2 = new HashMap();
    nestedMap2.put(TWO, "nestedMap two2");
    nestedMap2.put(THREE, nextNestedMap2);

    Map src = new HashMap();
    src.put(ONE, "10");
    src.put(TWO, Arrays.asList(nestedMap1, nestedMap2)); // 映射集合

    Map dest = new HashMap();
    mapping.mapping(src, dest);
    assertEquals(dest.get(ONE_OTHER), BigInteger.valueOf(10));

    List nestedOtherMap = (List) dest.get(TWO_OTHER);
    assertEquals(nestedOtherMap.size(), 2);

    Map nestedOtherMap1 = (Map) nestedOtherMap.get(0);
    assertEquals(nestedOtherMap1.get(TWO_OTHER), "nestedMap two1");
    Map nextNestedOtherMap1 = (Map) nestedOtherMap1.get(THREE_OTHER);
    assertEquals(nextNestedOtherMap1.get(THREE_OTHER), "nextNestedMap three1");

    Map nestedOtherMap2 = (Map) nestedOtherMap.get(1);
    assertEquals(nestedOtherMap2.get(TWO_OTHER), "nestedMap two2");
    Map nextNestedOtherMap2 = (Map) nestedOtherMap2.get(THREE_OTHER);
    assertEquals(nextNestedOtherMap2.get(THREE_OTHER), "nextNestedMap three2");
}</pre>

特性2:script上下文隔离

可以解决的需求:

  • 在嵌套映射时,在子映射时需要访问parent映射时的script context,不同映射时又需要保持隔离。简单点说:就是在一个映射过程,包括嵌套的递归映射都可以共享一个script context,通过el表达式可以正确访问

特性3:支持null symbol

可以解决的需求:

  • 强制设置目标的某属性为null
  • 源属性不存在,针对目标对象的属性进行赋值操作。 比如给目标属性设置初始默认值
testcase:
@Test
    public void testNullSymbol() { // 测试下null的特殊属性
        BeanMappingBuilder builder = new BeanMappingBuilder() {
        protected void configure() {
            behavior().debug(true).mappingEmptyStrings(false).mappingNullValue(false).trimStrings(true);// 设置行为
            mapping(HashMap.class, HashMap.class);
            fields(srcField("null"), targetField(ONE_OTHER, String.class)).defaultValue("10");
        }

    };

    BeanMapping mapping = new BeanMapping(builder);
    Map dest = new HashMap();
    mapping.mapping(new HashMap(), dest);
    assertEquals(dest.get(ONE_OTHER), "10");
}</pre>

后续计划:
  1. 支持object factory模式,允许自己控制嵌套对象的创建过程,目前默认是系统通过构造函数进行newInstance.
  2. fel script实现支持 (http://www.iteye.com/news/24711-Fel-java-script)
目前在阿里公司内部正在逐步使用,也欢迎大家试用和给我提需求,可以非常方便的替换原先的BeanUtils,性能上也会有一定的惊喜。