modelMapper使用,将数据库查询对象直接转成DTO对象
2024-09-06 16:13:42
1.pom引入
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.8</version>
</dependency>
2.将DAO对象转为DTO对象
User user = new User();
user.setId(1L);
user.setNickname("张三");
user.setEmail("101@qq.com");
user.setHonor("测试荣誉");
ModelMapper modelMapper = new ModelMapper();
UserDTO userDTO = modelMapper.map(user, UserDTO.class);
System.out.println(userDTO);
3.工具类
public class ModuleHelper {
private static final ModelMapper modelMapper;
static {
modelMapper = new ModelMapper();
// modelMapper.createTypeMap(String.class, Date.class);
// modelMapper.addConverter(toStringDate);
// modelMapper.getTypeMap(String.class, Date.class).setProvider(localDateProvider);
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}
private ModuleHelper() {
}
public static <D, T> D map(final T entity, Class<D> outClass) {
return modelMapper.map(entity, outClass);
}
public static <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
return entityList.stream()
.map(entity -> map(entity, outCLass))
.collect(Collectors.toList());
}
public static <S, D> D map(final S source, D destination) {
modelMapper.map(source, destination);
return destination;
}
}
最新文章
- 移动站应该尝试百度MIP的五个原因
- MAC下反編譯安卓APK
- android4.x获取(也可监测)外置sd路径和读写
- (引用)web安全测试
- [转]CodeSmith和PowerDesigner的使用安装和数据库创建
- php判断爬虫
- explicit构造函数的作用
- 为什么我们使用192.168.0.1作为内网ip
- ibatis面试笔记
- 验证docker的Redis镜像也存在未授权访问漏洞
- 初识Treap
- Python 收集Twitter时间序列数据
- ubuntu如何进入local、bin目录
- JS语句
- Abp(.NetCore)开发与发布过程
- python实战--数据结构二叉树
- HTML&;CSS_基础04
- 《团队-爬取豆瓣top250-开发文档》
- Windows Server2008、IIS7启用CA认证及证书制作完整过程
- Vue(六)过滤器