您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容主要讲解“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。