在项目中要经常转换model, 有了自动转换无疑是最优雅, 最有效率的工具
maven pom.xml
net.sf.dozer dozer-spring 5.5.1 net.sf.dozer dozer 5.5.1
DozerConfig.java
package com.xxx.xxx.config.dozer;import org.dozer.spring.DozerBeanMapperFactoryBean;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.Resource;@Configurationpublic class DozerConfig { @Value("classpath*:dozer/*.xml") private Resource[] resources; @Bean public DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean() { DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean(); dozerBeanMapperFactoryBean.setMappingFiles(this.resources); return dozerBeanMapperFactoryBean; }}
DozerEnumConverter.java (对枚举的支持)
package com.xxx.xxx.config.dozer;import com.xxx.xxx.enums.BaseEnum;import org.dozer.CustomConverter;import org.dozer.MappingException;import java.util.Objects;public class DozerEnumConverter implements CustomConverter { @Override public Object convert(Object destination, Object source, Class destinationClass, Class sourceClass) { if (source == null) { return null; } if (source instanceof BaseEnum) { return ((BaseEnum) source).getCode(); } else if (source instanceof Integer && destinationClass.isEnum()) { BaseEnum[] enums = (BaseEnum[]) destinationClass.getEnumConstants(); for (final BaseEnum e : enums) { if (Objects.equals(e.getCode(), source)) { return e; } } } else { throw new MappingException(String.format( "Converter %s was used incorrectly. Arguments were: %s and %s", this.getClass().getSimpleName(), destinationClass.getName(), source )); } return null; }}
DozerMapperFactory.java (封装一个转换工具, 便于使用)
package com.xxx.xxx.config.dozer;import org.dozer.Mapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.lang.reflect.Array;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;@Componentpublic class DozerMapperFactory { @Autowired protected Mapper dozerMapper; /** * 单个对象的深度复制及类型转换 * @param source 数据对象 * @param targetClass 复制目标类型 */ publicT convert(S source, Class targetClass) { if (source == null) { return null; } return this.dozerMapper.map(source, targetClass); } /** * list深度复制 * @param sources 数据对象 * @param targetClass 复制目标类型 */ public List convert(List sources, ClasstargetClass) { if (sources == null) { return null; } List list = new ArrayList (); for (S source : sources) { list.add(this.dozerMapper.map(source, targetClass)); } return list; } /** * set深度复制 * @param sources 数据对象 * @param targetClass 复制目标类型 */ public Set convert(Set sources, ClasstargetClass) { if (sources == null) { return null; } Set set = new HashSet (); for (S source : sources) { set.add(this.dozerMapper.map(source, targetClass)); } return set; } /** * 数组深度复制 * @param sources 数据对象 * @param targetClass 复制目标类型 */ public T[] convert(S[] sources, Class targetClass) { if (sources == null) { return null; } @SuppressWarnings("unchecked") T[] array = (T[]) Array.newInstance(targetClass, sources.length); for (int i = 0; i < sources.length; i++) { array[i] = this.dozerMapper.map(sources[i], targetClass); } return array; }}
resource中xml配置文件
dozer/global-configuration.xml
yyyy-MM-dd HH:mm:ss true false com.xxx.xxx.enums.BaseEnum java.lang.Integer
dozer/bean-mappings.xml
com.xxx.xxx.entity.User com.xxx.xxx.model.UserModel id userId
dozer的配置到这里就结束了