一.Dozer
1.用到的依赖
<!--dozer-->
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>
2.DozerUtils.java
package com.nebula.module.organization.utils.dozer;
import com.google.common.collect.Lists;
import org.dozer.DozerBeanMapper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Function:DTO/VO/DO等之间的转换
* Date: 2018/9/14 15:40
*
*/
public class DozerUtils {
/**
* 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
*/
private static DozerBeanMapper dozer;
static {
if (dozer == null) {
dozer = new DozerBeanMapper();
List<String> mappingFileUrls = Lists.newArrayList("dozer-config.xml");
dozer.setMappingFiles(mappingFileUrls);
}
}
/**
* @param source 源对象
* @param destinationClass 目标对象
* @return
* @title: map
* @description: 单个对象相互转换
*/
public static <T> T map(Object source, Class<T> destinationClass) {
return dozer.map(source, destinationClass);
}
/**
* @param sourceList
* @param destinationClass
* @return
* @title mapList
* @description 集合对象相互转换
*/
public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
List<T> destinationList = new ArrayList<T>();
for (Object sourceObject : sourceList) {
T destinationObject = dozer.map(sourceObject, destinationClass);
destinationList.add(destinationObject);
}
return destinationList;
}
/**
* 基于Dozer将对象A的值拷贝到对象B中
*
* @param source 需要转换的对象
* @param toObj 转换后对象类型
*/
public static void copy(Object source, Object toObj) {
if (null != source) {
dozer.map(source, toObj);
}
}
}
3.dozer-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">
<configuration>
//customer节点可省略,后面的converter文件也可省略
<custom-converters> <!-- these are always bi-directional -->
<converter type="com.lhc.util.dozer.convert.LocalDateTimeToDateDozerConverter">
<class-a>java.time.LocalDateTime</class-a>
<class-b>java.util.Date</class-b>
</converter>
<converter type="com.lhc.util.dozer.convert.StringToDateConverter">
<class-a>java.lang.String</class-a>
<class-b>java.util.Date</class-b>
</converter>
<converter type="com.lhc.util.dozer.convert.LocalDateTimeToLocalDateTimeConverter">
<class-a>java.time.LocalDateTime</class-a>
<class-b>java.time.LocalDateTime</class-b>
</converter>
<converter type="com.lhc.util.dozer.convert.LocalDateToLocalDateConverter">
<class-a>java.time.LocalDate</class-a>
<class-b>java.time.LocalDate</class-b>
</converter>
<converter type="com.lhc.util.dozer.convert.LocalTimeToLocalTimeConverter">
<class-a>java.time.LocalTime</class-a>
<class-b>java.time.LocalTime</class-b>
</converter>
</custom-converters>
</configuration>
</mappings>
4. 五个Converter
dozer-date.xml custom-converters有几个convert就有几个自定义的转换对象
package com.nebula.module.organization.utils.dozer.converter;
import org.dozer.DozerConverter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class LocalDateTimeToDateDozerConverter extends DozerConverter<LocalDateTime, Date> {
public LocalDateTimeToDateDozerConverter() {
super(LocalDateTime.class, Date.class);
}
@Override
public LocalDateTime convertFrom(Date source, LocalDateTime destination) {
return LocalDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
}
@Override
public Date convertTo(LocalDateTime source, Date destination) {
return Date.from(source.atZone(ZoneId.systemDefault()).toInstant());
}
}
package com.nebula.module.organization.utils.dozer.converter;
import org.apache.commons.lang3.StringUtils;
import org.dozer.DozerConverter;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.util.Date;
public class StringToDateConverter extends DozerConverter<String, Date> {
public StringToDateConverter() {
super(String.class, Date.class);
}
@Override
public String convertFrom(Date source, String destination) {
if (source == null) {
return StringUtils.EMPTY;
}
DateTime dateTime = new DateTime(source);
destination = dateTime.toString("yyyy-MM-dd HH:mm:ss");
return destination;
}
@Override
public Date convertTo(String source, Date destination) {
DateTimeFormatter dateTimeFormatter;
if (StringUtils.isBlank(source)) {
return null;
}
if (10 == source.length()) {
dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
} else if (14 == source.length()) {
dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMddHHmmss");
} else {
dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
}
DateTime dateTime = dateTimeFormatter.parseDateTime(source);
destination = dateTime.toDate();
return destination;
}
}
public class LocalDateTimeToLocalDateTimeConverter extends DozerConverter<LocalDateTime, LocalDateTime> {
public LocalDateTimeToLocalDateTimeConverter() {
super(LocalDateTime.class, LocalDateTime.class);
}
@Override
public LocalDateTime convertFrom(LocalDateTime source, LocalDateTime destination) {
return source;
}
@Override
public LocalDateTime convertTo(LocalDateTime source, LocalDateTime destination) {
return source;
}
}
public class LocalDateToLocalDateConverter extends DozerConverter<LocalDate, LocalDate> {
public LocalDateToLocalDateConverter() {
super(LocalDate.class, LocalDate.class);
}
@Override
public LocalDate convertFrom(LocalDate source, LocalDate destination) {
return source;
}
@Override
public LocalDate convertTo(LocalDate source, LocalDate destination) {
return source;
}
}
public class LocalTimeToLocalTimeConverter extends DozerConverter<LocalTime, LocalTime> {
public LocalTimeToLocalTimeConverter() {
super(LocalTime.class, LocalTime.class);
}
@Override
public LocalTime convertFrom(LocalTime source, LocalTime destination) {
return source;
}
@Override
public LocalTime convertTo(LocalTime source, LocalTime destination) {
return source;
}
}
二.MapStruct
1、依赖
1、pom加入依赖
<!--mapstruct-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Final</version>
</dependency>
2、mapstruct接口
import org.mapstruct.Mapper;
@Mapper
public interface UserMapStruct {
// ClassLoader 加载方式
UserMapStruct INSTANCE = Mappers.getMapper(UserMapStruct .class);
UserEntity userAddDTO2UserEntity(UserAddDTO userAddDTO);
UserAddVO UserEntity2UserAddVO (UserEntity userEntity );
}
3、service层转换
UserEntity userEntity = UserMapStruct.INSTANCE.userAddDTO2UserEntity(userAddDto);