mapstruct的简单使用(可能会出现的问题)

guardyou3
2023-10-15 / 76 评论 / 332 阅读 / 正在检测是否收录...

MapStruct是什么

简介: 在我们日常的Java开发工作中,经常会遇到模型对象的转化,例如将DTO转化为PO,进行数据模型转换的方式有很多种,与动态映射框架相比,MapStruct通过使用普通方法getter、setter方法进行快速模型转换的实现,而不是反射的形式,效率很高,这里简单介绍一下其用法。

以分页查询作为一个例子,将Req和Dto进行转换,示例所使用的的类:
UserPo:

@TableName("user")
@Data
public class UserPo extends BaseEntity implements Serializable {
    @TableId(value = "id",type = IdType.AUTO)
    private Long id;

    private String name;

    private Integer age;

}

UserDTO:

@Data
public class UserDto {

    private String name;

    private Integer age;

    private Integer pageSize;

    private Integer pageIndex;
}

UserListReq:

@Data
public class UserListReq {

    private Integer pageSize;

    private Integer pageIndex;
}

封装返回对象 PageResult

@Data
public class PageResult<T>  implements Serializable {
    private Long total;

    private Long size;

    private Long current;

    private Long pages;

    private List<T> records = Collections.emptyList();

    public void loadData(IPage<T> pageData) {
        this.setCurrent(pageData.getCurrent());
        this.setSize(pageData.getSize());
        this.setTotal(pageData.getTotal());
        this.setRecords(pageData.getRecords());
        this.setPages(pageData.getPages());
    }
}

UserService 层中的代码:

 @Override
    public PageResult<UserPo> getUserPage(UserDto userDto) {
        IPage<UserPo> userPoPage = new Page<>(userDto.getPageIndex(),userDto.getPageSize());
        IPage<UserPo> userPage = userMapper.selectPage(userPoPage, null);
        PageResult<UserPo> pageResult = new PageResult<>();
        pageResult.loadData(userPage);
        return pageResult;
    }

传统的BeanUtils的模型转换用法:
controller层中的代码,Result是封装的一个统一返回类,这里不展示:

 @GetMapping("/page")
    public Result page(@RequestBody UserListReq userListReq) {
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userListReq,userDto);
        return Result.ok(userService.getUserPage(userDto));
    }

使用BeanUtils的方式直接进行模型转换,虽然看上去很简洁,但实际因为使用反射效率较低。


下面我们采用MapStruct的方式进行转换
基础用法:
引入依赖:

<dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.4.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.4.2.Final</version>
        </dependency>
    </dependencies>

编写一个convert接口

@Mapper
public interface UserDtoConvert {
    UserDtoConvert INSTANCE = Mappers.getMapper(UserDtoConvert.class);
//    @Mapping(source = "pageIndex",target = "pageNo") 
    UserDto convertReqToUserDTO(UserListReq userListReq);
}
如果说要转换的模型中的属性名称不同,可以通过@Mapping注解进行映射关系的配置,这里不进行演示。

controller层中使用

  @GetMapping("/page")
    public Result page(@RequestBody UserListReq userListReq) {
        UserDto userDto = UserDtoConvert.INSTANCE.convertReqToUserDTO(userListReq);
        return Result.ok(userService.getUserPage(userDto));
    }

只需要调用接口方法即可,使用方式非常简介,效率也很高。

注意事项:导入@Mapper的时候不要导成mybatisPlus的包了

lnra6grl.png
另一个可能会导致的问题:

lombok和mapstruct之间的顺序问题

lnstsjtz.png
lombok一定要在mapstruct之前,不然返回的对象属性值仍然为null
查看编译后的文件
lnstvwl6.png
这是lombok在mapstruct之前生成的实现类

如果lombok在mapstruct之后,那么生成的实现类就没有上图红色圆圈框起来的部分,直接返回new UserDto对象导致并没有进行属性复制。
210

评论 (76)

取消
  1. 头像
    1
    Windows 10 · Google Chrome

    画图

    回复
  2. 头像
    1
    Windows 10 · Google Chrome

    555

    回复
  3. 头像
    1
    Windows 10 · Google Chrome

    555

    回复
  4. 头像
    1
    Windows 10 · Google Chrome

    555

    回复
    1. 头像
      1
      Windows 10 · Google Chrome
      @ 1

      表情表情

      回复
  5. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  6. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  7. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  8. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  9. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  10. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  11. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  12. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  13. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  14. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  15. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  16. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  17. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  18. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  19. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  20. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  21. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  22. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  23. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  24. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  25. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  26. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  27. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  28. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  29. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  30. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  31. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  32. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  33. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  34. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  35. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  36. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  37. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  38. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  39. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  40. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  41. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  42. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  43. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  44. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  45. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  46. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  47. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  48. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  49. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  50. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  51. 头像
    plqebvjxqr
    Windows 10 · Google Chrome

    看的我热血沸腾啊

    回复
  52. 头像
    rgkkllylya
    Windows 10 · Google Chrome

    怎么收藏这篇文章?

    回复
  53. 头像
    jasogvyasx
    Windows 10 · Google Chrome

    看的我热血沸腾啊https://www.ea55.com/

    回复
  54. 头像
    pjuydoltkn
    Windows 10 · Google Chrome

    看的我热血沸腾啊www.jiwenlaw.com

    回复
  55. 头像
    gdkdcgosiw
    Windows 10 · Google Chrome

    哈哈哈,写的太好了https://www.cscnn.com/

    回复
  56. 头像
    jtljryccde
    Windows 10 · Google Chrome

    《巨怪猎人第一季》欧美动漫高清在线免费观看:https://www.jgz518.com/xingkong/50922.html

    回复
  57. 头像
    zplbovhnuz
    Windows 10 · Google Chrome

    《大地恩情之古都惊雷粤语》韩国剧高清在线免费观看:https://www.jgz518.com/xingkong/114195.html

    回复
  58. 头像
    qslqwigsju
    Windows 10 · Google Chrome

    《比特(经典重制版)》欧美剧高清在线免费观看:https://www.jgz518.com/xingkong/149548.html

    回复
  59. 头像
    dvmvlnacca
    Windows 10 · Google Chrome

    《逃出魔掌》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/63118.html

    回复
  60. 头像
    kppxfyxzns
    Windows 10 · Google Chrome

    《标价我》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/78902.html

    回复
  61. 头像
    uxyiatrkwr
    Windows 10 · Google Chrome

    《冰血暴第五季》欧美剧高清在线免费观看:https://www.jgz518.com/xingkong/15304.html

    回复
  62. 头像
    jadpoozytn
    Windows 10 · Google Chrome

    《转生大作战劇場版(完整版)》动画片高清在线免费观看:https://www.jgz518.com/xingkong/136486.html

    回复
  63. 头像
    izgbxpxefc
    Windows 10 · Google Chrome

    《比特(经典重制版)》欧美剧高清在线免费观看:https://www.jgz518.com/xingkong/149548.html

    回复
  64. 头像
    pnkqxnybyu
    Windows 10 · Google Chrome

    《爱心小天使2011》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/42017.html

    回复
  65. 头像
    glkpmlppzb
    Windows 10 · Google Chrome

    《逃出魔掌》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/63118.html

    回复
  66. 头像
    tpoaeukhqx
    Windows 10 · Google Chrome

    《婚礼发癫后奶狗总裁追着我宠》短片剧高清在线免费观看:https://www.jgz518.com/xingkong/160307.html

    回复
  67. 头像
    vipeqoahzj
    Windows 10 · Google Chrome

    哈哈哈,写的太好了https://www.lawjida.com/

    回复
  68. 头像
    kwnjhmejro
    Windows 10 · Google Chrome

    每个标点都承载着思考的重量。

    回复
  69. 头像
    bzstgterlf
    Windows 10 · Google Chrome

    字里行间流露出真挚的情感,让人感同身受,共鸣不已。

    回复
  70. 头像
    gqwcdgnfpm
    Windows 10 · Google Chrome

    操作步骤清晰,指导性强,易于实践。

    回复
  71. 头像
    1
    Windows 10 · Google Chrome

    555

    回复
  72. 头像
    inxlzalxje
    Windows 10 · Google Chrome

    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com

    回复
  73. 头像
    jwnsrlqfoy
    Windows 10 · Google Chrome

    新项目准备上线,寻找志同道合的合作伙伴coinsrore.com

    回复
  74. 头像
    zzyxvpavxo
    Windows 10 · Google Chrome

    2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

    回复
  75. 头像
    nlvfjjdhsn
    Windows 10 · Google Chrome

    2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

    回复