您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“MapStruct如何使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“MapStruct如何使用”吧!
批量轉(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); } # 測試 /** * 測試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
如果有多個屬性需要忽略的時候,需要些很多行
# 編寫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); } # 測試 @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); /** * 會繼承全部配置,包括@BeanMapping和@Mapping * @param carDTO * @param vehicleVO */ @InheritConfiguration(name = "carDTO2vehicleVO") public abstract void updateVehicleVO(CarDTO carDTO,@MappingTarget VehicleVO vehicleVO); } # 測試 @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("邁巴赫"); // 通過carDTO2的屬性值來更新已存在的vehicleVO對象 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); /** * 測試@InheritInverseConfiguration反向繼承,只會繼承@Mapping * 故需要再單獨(dú)寫一次 @BeanMapping * name:指定使用哪一個方法的配置,寫方法的名字 * @param vehicleVO */ @BeanMapping(ignoreByDefault = true) @InheritInverseConfiguration(name = "carDTO2vehicleVO") public abstract CarDTO vehicleVO2CarDTO(VehicleVO vehicleVO); } # 測試 @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); /** * 測試@InheritInverseConfiguration反向繼承,只會繼承@Mapping * 故需要再單獨(dú)寫一次 @BeanMapping * name:指定使用哪一個方法的配置,寫方法的名字 * @param vehicleVO */ @BeanMapping(ignoreByDefault = true) @InheritInverseConfiguration(name = "carDTO2vehicleVO") public abstract CarDTO vehicleVO2CarDTO(VehicleVO vehicleVO); } # 測試 @RunWith(SpringRunner.class) @SpringBootTest(classes = MapStructApplication.class) public class MapStructTest { // @Autowired @Resource private CarConvert carConvert; /** * 測試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); } }
到此,相信大家對“MapStruct如何使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。