您现在的位置是:首页 > 后台技术 > JavaJava

mapstruct(图文)

第十三双眼睛2022-03-05【Java】人已围观

简介mapstruct的简单介绍及使用

要使用mapstruct,请确保你的jdk版本是1.8+。

1:导入依赖
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.2.0.Final</version>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>${mapstruct.version}</version>
</dependency>
2:准备几个实体类
public class UserAddRequest {

    private String name;

    private Integer age;

    private Long birthday;

    private Double salary;

    private List<HouseAddRequest> houseList;

}
public class HouseAddRequest {

    private String estateName;

    private String address;

    private Integer building;

    private Integer unit;

    private Integer floor;

    private String houseNumber;
    
}
public class UserAddParam {

    private String name;

    private Integer age;

    private Date birthday;

    private Double salary;

    private List<HouseAddParam> houseList;
    
}
public class HouseAddParam {

    private String estateName;

    private String address;

    private Integer building;

    private Integer unit;

    private Integer floor;

    private String houseCode;
    
}
定义一个映射器
@Mapper(componentModel = "spring")
public abstract class HouseConverter {
    
    @Mappings({
        @Mapping(target = "houseCode",source = "houseNumber")
    })
    public abstract HouseAddParam request2param(HouseAddRequest request);
    
}
@Mapper(componentModel ="spring",imports = {Date.class})
public abstract class UserConverter {

    @Autowired
    private HouseConverter houseConverter;

    @Mappings({
        @Mapping(target = "birthday",expression = "java(new Date(request.getBirthday()))")
    })
    public abstract UserAddParam request2param(UserAddRequest request);
    
    public HouseAddParam houseRequest2param(HouseAddRequest request){
        return houseConverter.request2param(request);
    }
    
}
定义一个定时任务,我们来测试一下
@Scheduled(cron = "0/10 * * * * *")
public void converterUserAddRequest2AddParam(){
    UserAddRequest userAddRequest = new UserAddRequest();
    userAddRequest.setName("张三");
    userAddRequest.setAge(1);
    userAddRequest.setBirthday(new Date().getTime());
    userAddRequest.setSalary(5434.56);
    List<HouseAddRequest> houseAddRequestList = new ArrayList<>();
    HouseAddRequest houseAddRequest1 = new HouseAddRequest();
    houseAddRequest1.setAddress("杭州滨江区");
    houseAddRequest1.setEstateName("缤纷东苑");
    houseAddRequest1.setBuilding(3);
    houseAddRequest1.setUnit(1);
    houseAddRequest1.setFloor(4);
    houseAddRequest1.setHouseNumber("401");
    houseAddRequestList.add(houseAddRequest1);
    userAddRequest.setHouseList(houseAddRequestList);
    UserAddParam userAddParam = userConverter.request2param(userAddRequest);
    System.out.println("=================================================");
    System.out.println(userAddParam);
}
结果成功输出了
UserAddParam(name=张三, age=1, birthday=Sat Mar 05 12:27:10 CST 2022, salary=5434.56, 
houseList=[HouseAddParam(estateName=缤纷东苑, address=杭州滨江区, building=3, unit=1, floor=4, houseCode=401)])

数字格式化:
我们现在把目标对象的薪资属性salary改为字符串,并且要保留一位小数,做如下配置就可以做到
@Mapping(target = "salary",source = "salary",numberFormat = "#0.0")
日期格式化
我们现在把目标对象的生日属性birthday改为字符串,格式要求2012-12-12 12:12:12这种 做如下配置就可以做到
我们首先需要将源对象的生日属性birthday类型改为Date
@Mapping(target = "birthday",dateFormat = "yyyy-MM-dd HH:mm:ss")

忽略目标对象的某个属性赋值
@Mapping(target = "age",ignore = true)

自定义处理方法
@AfterMapping
public void afterMapping(@MappingTarget UserAddParam userAddParam,UserAddRequest request){
    
}
@AfterMapping 注解的作用是,当UserAddRequest 到 UserAddParam 转换方法完成后,会来调用这个方法
@MappingTarget 注解用来标记目标对象

当属性很多,并且名字都相同,但是只想映射少量属性时该怎么办呢,如果一个一个设置忽略,则会很多。用如下注解就可以解决
@BeanMapping(ignoreByDefault = true)
此注解的作用是,先禁用默认映射。再手动设置我们想映射的属性就可以了。

@InheritConfiguration
MapStruct允许对映射关系进行继承,使用@InheritConfiguration标记当前方法继承其他映射方法的映射关系。会自动查找相同类型映射源、映射目标的方法进行继承,如果存在多个相同类型的方法,则需要手工指定

@InheritInverseConfiguration
除了正向继承规则外,还可以进行规则逆向继承,从被继承方法的目标对象映射到源对象








 

Tags:

很赞哦! ()

文章评论

    共有条评论来说两句吧...

    用户名:

    验证码:

站点信息

  • 网站名称:JavaStudy
  • 建站时间:2019-1-14
  • 网站程序:帝国CMS7.5
  • 文章统计130篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们