这篇文章主要介绍了Java Spring怎么集成MapStruct的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Java Spring怎么集成MapStruct文章都会有所收获,下面我们一起来看看吧。
前言:
MapStruct支持Spring的依赖注入机制,只须要在@Mapper注解中添加componentModel配置项,并设置为“spring”便可。
待转换的类
@Getter@Setter@ToString@Builder@AllArgsConstructor@NoArgsConstructorpublic 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@NoArgsConstructorpublic 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”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Java Spring怎么集成MapStruct”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网行业资讯频道。