MapStruct是什么
简介: 在我们日常的Java开发工作中,经常会遇到模型对象的转化,例如将DTO转化为PO,进行数据模型转换的方式有很多种,与动态映射框架相比,MapStruct通过使用普通方法getter、setter方法进行快速模型转换的实现,而不是反射的形式,效率很高,这里简单介绍一下其用法。
- 引入依赖
- 编写Mapper接口,配置转换方法
- 直接调用mapper接口中的方法
- 直接看文章末尾
以分页查询作为一个例子,将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的包了
另一个可能会导致的问题:
lombok一定要在mapstruct之前,不然返回的对象属性值仍然为null
查看编译后的文件
这是lombok在mapstruct之前生成的实现类
555
555
555
看的我热血沸腾啊
怎么收藏这篇文章?
看的我热血沸腾啊https://www.ea55.com/
看的我热血沸腾啊www.jiwenlaw.com
哈哈哈,写的太好了https://www.cscnn.com/
《巨怪猎人第一季》欧美动漫高清在线免费观看:https://www.jgz518.com/xingkong/50922.html
《大地恩情之古都惊雷粤语》韩国剧高清在线免费观看:https://www.jgz518.com/xingkong/114195.html
《比特(经典重制版)》欧美剧高清在线免费观看:https://www.jgz518.com/xingkong/149548.html
《逃出魔掌》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/63118.html
《标价我》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/78902.html
《冰血暴第五季》欧美剧高清在线免费观看:https://www.jgz518.com/xingkong/15304.html
《转生大作战劇場版(完整版)》动画片高清在线免费观看:https://www.jgz518.com/xingkong/136486.html
《比特(经典重制版)》欧美剧高清在线免费观看:https://www.jgz518.com/xingkong/149548.html
《爱心小天使2011》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/42017.html
《逃出魔掌》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/63118.html
《婚礼发癫后奶狗总裁追着我宠》短片剧高清在线免费观看:https://www.jgz518.com/xingkong/160307.html
哈哈哈,写的太好了https://www.lawjida.com/
每个标点都承载着思考的重量。
字里行间流露出真挚的情感,让人感同身受,共鸣不已。
操作步骤清晰,指导性强,易于实践。