重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容主要讲解“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 Listdtos2vos(List carDTO); } # 测试 /** * 测试mapstruct批量转换 * List --> List */ @Test public void test3() { CarDTO carDTO = buildCarDTO(); List carDTOList = new ArrayList<>(); carDTOList.add(carDTO); // source // target List 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如何使用”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!