这篇文章主要介绍了JavaSpring集成MapStruct详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
前言:
MapStruct支持Spring的依赖注入机制,只须要在@Mapper注解中添加componentModel配置项,并设置为“spring”便可。
待转换的类
@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Spu {
private Integer id;
private String name;
private String caption;
private String pics;
private String specs;
private String unit;
private Integer hot;
private Integer comments;
private Integer agrees;
private Integer recommend;
private Integer status;
private String service;
private String info;
private Integer countryId;
private String addr;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
目标类
@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SpuVO {
private Integer id;
private String name;
private String caption;
private String pics;
private String specs;
private String unit;
private String service;
private String addr;
}
转换接口
@Mapper(componentModel = "spring")
public interface SpuTrans {
List<SpuVO> spuList2SpuVOList(List<Spu> spuList);
}
测试类
@Controller
@RequestMapping()
public class IndexController {
@Resource
private SpuTrans spuTrans;
@Resource
private SpuService spuService;
@GetMapping("/list")
public ModelAndView list(ModelAndView mav) {
//轮播图
List<Spu> spuList = spuService.selectAll();
List<SpuVO> spuVOList = spuTrans.spuList2SpuVOList(spuList);
mav.addObject("spuVOList", spuVOList);
mav.setViewName("spu_list");
return mav;
}
}
到此这篇关于Java Spring集成MapStruct详情的文章就介绍到这了,更多相关Spring集成MapStruct内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:Java Spring集成MapStruct详情
基础教程推荐
猜你喜欢
- springboot自定义starter方法及注解实例 2023-03-31
- Java实现查找文件和替换文件内容 2023-04-06
- Java实现线程插队的示例代码 2022-09-03
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- Java文件管理操作的知识点整理 2023-05-19
- Java数据结构之对象比较详解 2023-03-07
- Java并发编程进阶之线程控制篇 2023-03-07
- java实现多人聊天系统 2023-05-19
- java基础知识之FileInputStream流的使用 2023-08-11