-
Notifications
You must be signed in to change notification settings - Fork 113
1.0.1版本详情
agapple edited this page Sep 27, 2013
·
4 revisions
- 支持(int,long,short,byte,char,boolean,float,double)8种原型和其对应的基本类型 + (BigInteger , BigDecimal) + String 允许互相之间转化
- 支持(java.util.Date , java.util.Calendar)和字符串的转化,提供默认的day("yyyy-MM-dd") , time("yyyy-MM-dd HH:mm:ss")两种格式的转化
- 支持8种类型的Array + List + Set,允许互相之间的转化
对应的issue : http://code.google.com/p/mapping4java/issues/detail?id=4 , http://code.google.com/p/mapping4java/issues/detail?id=9
个人对Array<->Collection的转化比较满意,看一段测试代码片段就知道其强悍之处。
// 来一个变态的,嵌套多维数组 List<Map> firstList = Arrays.asList(first); List<List<Map>> twoList = Arrays.asList(firstList); List<List<List<Map>>> threeList = Arrays.asList(twoList); List<List<List<List<Map>>>> fourList = Arrays.asList(threeList); Convertor arrayConvertor = helper.getConvertor(List.class, ConvertorModel[][][][].class); Convertor setConvertor = helper.getConvertor(List.class, Set.class); ConvertorModel[][][][] oldValue = (ConvertorModel[][][][]) arrayConvertor.convert(fourList, ConvertorModel[][][][].class);ConvertorModel[][][][] newValue = (ConvertorModel[][][][]) arrayConvertor.convertCollection( fourList, ConvertorModel[][][][].class, new Class[] { ConvertorModel[][][].class, ConvertorModel[][].class, ConvertorModel[].class, ConvertorModel.class }); Set<Set<Set<Set<ConvertorOtherModel>>>> newSetValue = (Set) setConvertor.convertCollection( fourList, Set.class, new Class[] { Set.class, Set.class, Set.class, ConvertorOtherModel.class }); assertEquals(oldValue.length, 1); assertEquals(oldValue[0][0][0][0].getI(), first.get("i")); assertEquals(oldValue[0][0][0][0].getInteger(), first.get("integer")); assertEquals(oldValue[0][0][0][0].getBigDecimal(), first.get("bigDecimal")); assertEquals(newValue.length, 1); assertEquals(newValue[0][0][0][0].getI(), first.get("i")); assertEquals(newValue[0][0][0][0].getInteger(), first.get("integer")); assertEquals(newValue[0][0][0][0].getBigDecimal(), first.get("bigDecimal")); assertEquals(newSetValue.size(), 1); ConvertorOtherModel model = newSetValue.iterator().next().iterator().next().iterator().next().iterator().next(); assertEquals(model.getI(), first.get("i")); assertEquals(model.getInteger(), first.get("integer")); assertEquals(model.getBigDecimal(), first.get("bigDecimal"));</pre>
说明: 该段测试代码主要是对List<List<List<List<Map>>>> fourList的一个嵌套对象,转成ConvertorModel[][][][] , Set<Set<Set<Set<ConvertorOtherModel>>>>的结构,支持任意深度的嵌套
新增了一些配置定义,更加方便客户端更加灵活的使用。
- global-configurations
<global-configurations debug="false" mappingNullValue="true" mappingEmptyStrings="true" trimStrings="true" />
- class-alias-configurations
<class-alias-configurations> <classAlias alias="commonClass" class="com.agapple.mapping.process.convetor.CommonAndCommonConvertor$CommonToCommon" /> </class-alias-configurations>
- convetors-configurations
<convetors-configurations> <convertor alias="common" class="commonClass" /> </convetors-configurations>
说明下:
* global-configurations: 允许定义一些行为控制,比如针对null value , emtpy string是否需要mapping到目标对象上。 issue : http://code.google.com/p/mapping4java/issues/detail?id=8
* class-alias-configurations : 允许定义class alias的别名,借鉴于ibatis的功能。 默认支持的alias (8种原生类型,string, map, list, set)等,这里也允许客户自己定义
* convetors-configurations : 允许定义convertor配置,避免每次都需要手工向repository进行注册。 issue : http://code.google.com/p/mapping4java/issues/detail?id=1
针对Object存在继承情况,类似于:http://dozer.sourceforge.net/documentation/baseattributes.html
新的配置:
BeanMappingBuilder builder = new BeanMappingBuilder() {protected void configure() { behavior().debug(true);// 设置行为 mapping(TwoObject.class, HashMap.class); fields(srcField("name").locatorClass(FirstObject.class), targetField("name"));// name从父类中获取 fields(srcField("firstValue").locatorClass(FirstObject.class), targetField("firstValue"));// 也从父类中获取 fields(srcField("twoValue"), targetField("twoValue")); } };</pre>
可以指定locatorClass,默认为当前mapping class。允许指定其子类做为field查找的class。 对应issue : http://code.google.com/p/mapping4java/issues/detail?id=10
目前暂不支持子父属性同名方法,因为按照java动态性的原则,针对父类的Method,当前实例为子类时,会自动调用当前子类的方法。
ps : 针对同名的处理,会该issue中同步解决 : http://code.google.com/p/mapping4java/issues/detail?id=6
mapping规则定义,允许使用java api进行定义, 对应issue : http://code.google.com/p/mapping4java/issues/detail?id=5
mapping API使用例子:
BeanMappingBuilder builder = new BeanMappingBuilder() {protected void configure() { mapping(HashMap.class, HashMap.class).batch(false).reversable(true).keys("src", "target"); fields(srcField("one"), targetField("oneOther")).convertor("convertor").defaultValue("ljh"); fields(srcField("two").clazz(String.class), targetField("twoOther")).script("1+2").convertor( StringToCommon.class); fields(srcField("three").clazz(ArrayList.class), targetField("threeOther").clazz(HashSet.class)).recursiveMapping( true); } };
//根据Builder构造mapping对象 BeanMapping mapping = new BeanMapping(builder);
对应的代码类:
目前单元测试总体覆盖率已经达到78.9%以上,来一张截图看一下。