Java Spring集成MapStruct詳情
前言:
MapStruct支持Spring的依賴注入機制,只須要在@Mapper注解中添加componentModel配置項,并設(shè)置為“spring”便可。
待轉(zhuǎn)換的類
@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; }
目標(biāo)類
@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; }
轉(zhuǎn)換接口
@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; } }
到此這篇關(guān)于Java Spring集成MapStruct詳情的文章就介紹到這了,更多相關(guān)Spring集成MapStruct內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Spring Boot應(yīng)用ApplicationEvent案例場景
這篇文章主要介紹了基于Spring Boot應(yīng)用ApplicationEvent,利用Spring的機制發(fā)布ApplicationEvent和監(jiān)聽ApplicationEvent,需要的朋友可以參考下2023-03-03EasyExcel實現(xiàn)導(dǎo)入+各種數(shù)據(jù)校驗功能
這篇文章主要介紹了EasyExcel實現(xiàn)導(dǎo)入+各種數(shù)據(jù)校驗,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05SpringBoot+jsp項目啟動出現(xiàn)404的解決方法
這篇文章主要介紹了SpringBoot+jsp項目啟動出現(xiàn)404的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03