Spring集成MapStruct

Spring集成MapStructMapStruct支持Spring的依赖注入机制,只须要在@Mapper注解中添加componentModel配置项,并设置为“spring”便可。

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;
    }
}

今天的文章Spring集成MapStruct分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/8824.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注