本篇內(nèi)容主要講解“MapStruct如何使用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“MapStruct如何使用”吧!
創(chuàng)新互聯(lián)專注于孝感企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城網(wǎng)站開發(fā)。孝感網(wǎng)站建設(shè)公司,為孝感等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站策劃,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
批量轉(zhuǎn)換
# 編寫convert import org.mapstruct.factory.Mappers; import java.util.List; @Mapper public abstract class CarConvert { public static CarConvert INSTANCE = Mappers.getMapper(CarConvert.class); public abstract List<CarVO> dtos2vos(List<CarDTO> carDTO); } # 測(cè)試 /** * 測(cè)試mapstruct批量轉(zhuǎn)換 * List<CarDto>--> List<CarVo> */ @Test public void test3() { CarDTO carDTO = buildCarDTO(); List<CarDTO> carDTOList = new ArrayList<>(); carDTOList.add(carDTO); // source // target List<CarVO> carVOList = CarConvert.INSTANCE.dtos2vos(carDTOList); System.out.println(carVOList); }
@BeanMapping
如果有多個(gè)屬性需要忽略的時(shí)候,需要些很多行
# 編寫convert import org.mapstruct.factory.Mappers; import java.util.List; @Mapper public abstract class CarConvert { public static CarConvert INSTANCE = Mappers.getMapper(CarConvert.class); /** * 配置忽略mapstruct的默認(rèn)映射行為,只映射那些配置了@Mapping的屬性 * @param carDTO */ @BeanMapping(ignoreByDefault = true) @Mapping(source = "id",target = "id") public abstract VehicleVO carDTO2vehicleVO(CarDTO carDTO); } # 測(cè)試 @Test public void test4() { CarDTO carDTO = buildCarDTO(); VehicleVO vehicleVO = CarConvert.INSTANCE.carDTO2vehicleVO(carDTO); System.out.println(vehicleVO); }
映射繼承
# 編寫convert import org.mapstruct.factory.Mappers; import java.util.List; @Mapper public abstract class CarConvert { public static CarConvert INSTANCE = Mappers.getMapper(CarConvert.class); /** * 配置忽略mapstruct的默認(rèn)映射行為,只映射那些配置了@Mapping的屬性 * @param carDTO */ @BeanMapping(ignoreByDefault = true) @Mapping(source = "id",target = "id") @Mapping(source = "brand",target = "brandName") public abstract VehicleVO carDTO2vehicleVO(CarDTO carDTO); /** * 會(huì)繼承全部配置,包括@BeanMapping和@Mapping * @param carDTO * @param vehicleVO */ @InheritConfiguration(name = "carDTO2vehicleVO") public abstract void updateVehicleVO(CarDTO carDTO,@MappingTarget VehicleVO vehicleVO); } # 測(cè)試 @Test public void test5() { CarDTO carDTO = buildCarDTO(); VehicleVO vehicleVO = CarConvert.INSTANCE.carDTO2vehicleVO(carDTO); CarDTO carDTO2 = new CarDTO(); carDTO2.setId(330L); carDTO2.setPrice(789d); carDTO2.setBrand("邁巴赫"); // 通過(guò)carDTO2的屬性值來(lái)更新已存在的vehicleVO對(duì)象 CarConvert.INSTANCE.updateVehicleVO(carDTO2,vehicleVO); System.out.println(vehicleVO); }
反向繼承
# 編寫convert import org.mapstruct.factory.Mappers; import java.util.List; @Mapper public abstract class CarConvert { public static CarConvert INSTANCE = Mappers.getMapper(CarConvert.class); /** * 配置忽略mapstruct的默認(rèn)映射行為,只映射那些配置了@Mapping的屬性 * @param carDTO */ @BeanMapping(ignoreByDefault = true) @Mapping(source = "id",target = "id") @Mapping(source = "brand",target = "brandName") public abstract VehicleVO carDTO2vehicleVO(CarDTO carDTO); /** * 測(cè)試@InheritInverseConfiguration反向繼承,只會(huì)繼承@Mapping * 故需要再單獨(dú)寫一次 @BeanMapping * name:指定使用哪一個(gè)方法的配置,寫方法的名字 * @param vehicleVO */ @BeanMapping(ignoreByDefault = true) @InheritInverseConfiguration(name = "carDTO2vehicleVO") public abstract CarDTO vehicleVO2CarDTO(VehicleVO vehicleVO); } # 測(cè)試 @Test public void test6() { VehicleVO vehicleVO = new VehicleVO(); vehicleVO.setId(9999L); vehicleVO.setBrandName("別克"); vehicleVO.setPrice(66554322d); CarDTO carDTO = CarConvert.INSTANCE.vehicleVO2CarDTO(vehicleVO); System.out.println(carDTO); }
結(jié)合spring使用
# 編寫convert import org.mapstruct.factory.Mappers; import java.util.List; @Mapper(componentModel = "spring") <----- public abstract class CarConvert { public static CarConvert INSTANCE = Mappers.getMapper(CarConvert.class); /** * 配置忽略mapstruct的默認(rèn)映射行為,只映射那些配置了@Mapping的屬性 * @param carDTO */ @BeanMapping(ignoreByDefault = true) @Mapping(source = "id",target = "id") @Mapping(source = "brand",target = "brandName") public abstract VehicleVO carDTO2vehicleVO(CarDTO carDTO); /** * 測(cè)試@InheritInverseConfiguration反向繼承,只會(huì)繼承@Mapping * 故需要再單獨(dú)寫一次 @BeanMapping * name:指定使用哪一個(gè)方法的配置,寫方法的名字 * @param vehicleVO */ @BeanMapping(ignoreByDefault = true) @InheritInverseConfiguration(name = "carDTO2vehicleVO") public abstract CarDTO vehicleVO2CarDTO(VehicleVO vehicleVO); } # 測(cè)試 @RunWith(SpringRunner.class) @SpringBootTest(classes = MapStructApplication.class) public class MapStructTest { // @Autowired @Resource private CarConvert carConvert; /** * 測(cè)試mapstruct和spring整合 */ @Test public void test7() { VehicleVO vehicleVO = new VehicleVO(); vehicleVO.setId(9999L); vehicleVO.setBrandName("別克"); vehicleVO.setPrice(66554322d); CarDTO carDTO = carConvert.vehicleVO2CarDTO(vehicleVO); System.out.println(carDTO); } }
到此,相信大家對(duì)“MapStruct如何使用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)頁(yè)題目:MapStruct如何使用
瀏覽路徑:http://aaarwkj.com/article32/pjdgpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、網(wǎng)站收錄、、建站公司、響應(yīng)式網(wǎng)站、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)