您好,登錄后才能下訂單哦!
本篇內容主要講解“MapStruct如何使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“MapStruct如何使用”吧!
批量轉換
# 編寫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批量轉換 * 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的默認映射行為,只映射那些配置了@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的默認映射行為,只映射那些配置了@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的默認映射行為,只映射那些配置了@Mapping的屬性 * @param carDTO */ @BeanMapping(ignoreByDefault = true) @Mapping(source = "id",target = "id") @Mapping(source = "brand",target = "brandName") public abstract VehicleVO carDTO2vehicleVO(CarDTO carDTO); /** * 測試@InheritInverseConfiguration反向繼承,只會繼承@Mapping * 故需要再單獨寫一次 @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); }
結合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的默認映射行為,只映射那些配置了@Mapping的屬性 * @param carDTO */ @BeanMapping(ignoreByDefault = true) @Mapping(source = "id",target = "id") @Mapping(source = "brand",target = "brandName") public abstract VehicleVO carDTO2vehicleVO(CarDTO carDTO); /** * 測試@InheritInverseConfiguration反向繼承,只會繼承@Mapping * 故需要再單獨寫一次 @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如何使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。