Spring Dozer 使用:
使用Dozer映射复杂类型:
1. 数据类型不一致。
2. 级联映射。
3. 自定义映射。
Dozer其实底层使用了现成的BeanUtil,通过反射来映射,况且Dozer应用了Cache技术,应该比自个通过BeanUtils映射性能要好点。所以一般的应用应该不存在性能问题。
Dozer对于基本类型之间转换是不用配置的,比如Sting <------>Integer ,只要属性名称相同就Ok了。
而常用的Date与String映射配置如下:
birthday
dateOfBirth
指明 CustomerPo里面的birthday对应CustomerVo里面的dateOfBirth.并且是Date与String之间双向映射。
对于属性名称不一致,也仅仅需要一个配置文件,如下:
type
transferType
指明 CustomerPo里面的type 对应CustomerVo里面的transferType.
而对以级联,比如CustomerPo里面的一个属性映射为CustomerVo里么一个对象的属性,下面的配置就可以了
type
transferType.type
上面其实就是Dozer基本用法,也涵盖了大多数应用场景,可见基本不需要写代码,仅仅一个配置文件搞定,简单吧。
而对以更个性化的映射,就需要写代码了, 比如在CustomerPo中的into类型的transferId ,映射CustomerVo String 类型transferType, 如果transferId =1 对应transferType=“immediateTranfer” 如果transferId =2 对应transferType=“scheduleTransfer” 反之亦然。就要写一个Customer的Map了, 如下:
import org.dozer.CustomConverter;
public class CustomerMap implements CustomConverter {
public Object convert(Object destinationFieldValue,
Object sourceFieldValue, Class> destinationClass,
Class> sourceClass) {
Object returnVale = null;
if(sourceFieldValue!=null){
if(sourceFieldValue instanceof Integer ){
if((Integer)sourceFieldValue == 1){
returnVale ="immediateTranfer";
}
if((Integer)sourceFieldValue == 2){
returnVale ="scheduleTransfer";
}
}
if(sourceFieldValue instanceof String ){
if("immediateTranfer".equals(destinationFieldValue)){
returnVale =1;
}
if("scheduleTransfer".equals(destinationFieldValue)){
returnVale =2;
}
}
}
return returnVale;
}
}
然后在配置文件配置就Ok了 如下:
type
transferType
下面就是一个完整的配置文件:
xmlns="http://dozer.sourceforge.net">
colleage.name
schoolName
type
transferType
2.加载dozer组件工具:
List mappingfilelist=new ArrayList();
mappingfilelist.add(Constants.dozer。Xml);
mif = new DozerBeanMapper(mappingfilelist);
mif.map(source,destination);
3.注入配置Injecting Custom Mapping Files
Dozer同样支持IOC容器的注入(现在很难找到不支持spring容器的项目了),同样也支持代码方式的注入:
The Dozer mapping xml file(s) define any custom mappings that can't be automatically performed by the Dozer mapping engine. Any custom Dozer mapping files need to be injected into the Mapper implementation(org.dozer.DozerBeanMapper). Both setter-based and constructor-based injection are supported.
Preferably, you will be using an IOC framework such as Spring for these Dozer injection requirements. Alternatively, the injection of mapping files can be done programatically. Below is a programmatic approach to creating a bean mapper. Note that this is NOT the recommended way to retrieve the bean mapper . Each new instance needs to be initialized and this consumes time as well as resources. If you are using the mapper this way just wrap it using the singleton pattern.
先看spring方式的注入:
dozer-global-configuration.xml
dozer-bean-mappings.xml
more-dozer-bean-mappings.xml
再看代码方式的注入(!!!非推荐方式)
Each new instance needs to be initialized and this consumes time as well as resources. If you are using the mapper this way just wrap it using the singleton pattern.
List myMappingFiles = new ArrayList();
myMappingFiles.add("dozerBeanMapping.xml");
myMappingFiles.add("someOtherDozerBeanMappings.xml");
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.setMappingFiles(myMappingFiles);
DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class);
springside里面实现的Dozer单例: