Java Spring集成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內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: