<dependency>
<groupId>net.sf.dozergroupId>
<artifactId>dozer-springartifactId>
<version>5.5.1version>
dependency>
<dependency>
<groupId>net.sf.dozergroupId>
<artifactId>dozerartifactId>
<version>5.5.1version>
dependency>
bean-mappings.xml
和 global-configuration.xml
<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">
mappings>
<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>
<date-format>yyyy-MM-dd HH:mm:ssdate-format>
<wildcard>truewildcard>
<trim-strings>falsetrim-strings>
configuration>
mappings>
org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件 其内容为 com.shop.config.DozerMapperConfig
package com.system.config;
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;
@Configuration
public class DozerMapperConfig {
@Bean
public DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean(@Value("classpath*:dozer/*.xml") Resource[] resources) throws Exception {
DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean();
dozerBeanMapperFactoryBean.setMappingFiles(resources);
return dozerBeanMapperFactoryBean;
}
}
/**
* Copyright (C) 2018-2022
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制
*/
package com.system.common.dozer;
import com.system.entity.base.PagEntity;
import java.util.List;
import java.util.Set;
public interface Generator {
/**
* 转换
*
* @param s 数据对象
* @param clz 复制目标类型
* @return {@link T}
* @Description: 单个对象的深度复制及类型转换,vo/domain , po
* @author [email protected]
* @Time 2018年5月9日 下午3:53:24
*/
<T, S> T convert(S s, Class<T> clz);
/**
* @Description: 深度复制结果集(ResultSet为自定义的分页结果集)
* @param s 数据对象
* @param clz 复制目标类型
* @return
* @author [email protected]
* @Time 2018年5月9日 下午3:53:24
*/
// Result convert(Result s, Class clz);
/**
* 转换
*
* @param s 数据对象
* @param clz 复制目标类型
* @return {@link List}
* @Description: list深度复制
* @author [email protected]
* @Time 2018年5月9日 下午3:54:08
*/
<T, S> List<T> convert(List<S> s, Class<T> clz);
/**
* @param s
* @param clz
* @param
* @param
* @return
*/
<T, S> PagEntity<T> convertPaging(PagEntity<S> s, Class<T> clz);
/**
* @param s 数据对象
* @param clz 复制目标类型
* @return
* @Description: set深度复制
* @author [email protected]
* @Time 2018年5月9日 下午3:54:39
*/
<T, S> Set<T> convert(Set<S> s, Class<T> clz);
/**
* @param s 数据对象
* @param clz 复制目标类型
* @return
* @Description: 数组深度复制
* @author [email protected]
* @Time 2018年5月9日 下午3:54:57
*/
<T, S> T[] convert(S[] s, Class<T> clz);
}
/**
* Copyright (C) 2018-2022
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制
*/
package com.system.common.dozer;
import com.system.entity.base.PagEntity;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Component
@Lazy(true)
public class GeneratorImpl implements Generator {
@Autowired
protected Mapper dozerMapper;
@Override
public <T, S> T convert(S s, Class<T> clz) {
return s == null ? null : dozerMapper.map(s, clz);
}
@Override
public <T, S> List<T> convert(List<S> s, Class<T> clz) {
return s == null ? null : s.stream().map(vs -> dozerMapper.map(vs, clz)).collect(Collectors.toList());
}
@Override
public <T, S> PagEntity<T> convertPaging(PagEntity<S> paging, Class<T> clz) {
PagEntity<T> pagingVo = new PagEntity<>();
pagingVo.setRecords(convert(paging.getRecords(), clz));
pagingVo.setTotal(paging.getTotal());
return pagingVo;
}
@Override
public <T, S> Set<T> convert(Set<S> s, Class<T> clz) {
return s == null ? null : s.stream().map(vs -> dozerMapper.map(vs, clz)).collect(Collectors.toSet());
}
@Override
public <T, S> T[] convert(S[] s, Class<T> clz) {
if (s == null) {
return null;
}
@SuppressWarnings("unchecked")
T[] arr = (T[]) Array.newInstance(clz, s.length);
for (int i = 0; i < s.length; i++) {
arr[i] = dozerMapper.map(s[i], clz);
}
return arr;
}
}
@Resource
private Generator generator;
public Long addBanner(BannerVo banner) throws ServerExcption {
Banner convert = generator.convert(banner, Banner.class);
convert.setType(0);
Date date = new Date();
convert.setCreateTime(date);
convert.setUpdateTime(date);
bannerMapper.insert(convert);
return convert.getId();
}
至此完成