添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

玩转MapStruct,手把手带你学会!

在平时CRUD的工作中,经常需要做PO、VO、DTO之间的转换。简单的对象转换,使用BeanUtils基本上是够了,但是复杂的转换,如果使用它的话又得写一堆Getter、Setter方法了。 BeanUtils 就是一个大老粗,只能同属性映射,或者在属性相同的情况下,允许被映射的对象属性少;但当遇到被映射的属性数据类型被修改或者被映射的字段名被修改,则会导致映射失败。而 mapstruct 就是一个巧媳妇儿了,她心思细腻,把我们可能会遇到的情况都给考虑到了,所以今天给大家推荐一款对象自动映射工具 MapStruct ,接下来我们一起学习这个吧!

关于BeanUtils

平时我经常使用Hutool中的BeanUtil类来实现对象转换,用多了之后就发现有些缺点:

  • 对象属性映射使用反射来实现,性能比较低;
  • 对于不同名称或不同类型的属性无法转换,还得单独写Getter、Setter方法;
  • 对于嵌套的子对象也需要转换的情况,也得自行处理;
  • 集合对象转换时,得使用循环,一个个拷贝。
  • 对于这些不足,MapStruct都能解决,不愧为一款功能强大的对象映射工具!

    一、什么是MapStruct?

    MapStruct是一款基于Java注解的对象属性映射工具,使用的时候我们只要在接口中定义好对象属性映射规则,它就能自动生成映射实现类,不使用反射,性能优秀,能实现各种复杂映射。

    二、如何使用MapStruct?

    1、引入MapStruct依赖

      <lombok.version>1.18.12</lombok.version>
      <mapstruct.version>1.4.2.Final</mapstruct.version>
    <!--MapStruct相关依赖-->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
            <scope>compile</scope>
        </dependency>
    

    2、创建我们所需要的案例实体类

    * @author JavaAlliance * @version 1.0 * @description: TODO * @date 2021/11/23 9:37 @Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class User { private Integer id ;//用户id private String userName;//用户名 private String password; //密码 private Date birthday;//生日 private String tel;//电话号码 private String email; //邮箱 private String idCardNo;//身份证号 private String icon; //头像 private Integer gender;//性别

    3、创建VO对象

    * @author JavaAlliance * @version 1.0 * @description: TODO * @date 2021/11/23 9:37 @Data @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true) @EqualsAndHashCode(callSuper = false) public class UserVo { private Long id ;//用户id private String userName;//用户名 private String password; //密码 //与PO类型不同的属性 private Date birthday;//生日 //与PO名称不同的属性 private String telNumber;//电话号码 private String email; //邮箱 private String idCardNo;//身份证号 private String icon; //头像 private Integer gender;//性别

    那我们现在要做的就是需要将User对象转换为UserVo对象;

    4、创建映射接口

    (目的:实现同名同类型属性、不同名称属性、不同类型属性的映射;)

    @Mapper
    public interface UserMapper {
        UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
        @Mapping(source = "tel",target = "telNumber")
        @Mapping(source = "birthday",target = "birthday",dateFormat = "yyyy-MM-dd")
        UserVo convertToVo(User user);
    

    5、效果演示

    @RestController
    @RequestMapping("/user")
    public class UserController {
        @GetMapping("/mapStructToVo")
        public Result mapStructToVo() {
            User user = new User();
            user.setId(1).setEmail("1964327885@qq.com").setUserName("小慕").setBirthday(new Date()).setTel("18772563087");
            UserVo userVo = userMapper.convertToVo(user);
            System.out.println(JSON.toJSONString(userVo));
            return Result.success(userVo);
    

    打印结果:

    {"birthday":"2021-11-26","email":"1964327885@qq.com","id":1,"telNumber":"18772563087","userName":"小慕"}
    

    正如运行效果所示,User对象中的tel字段的值被映射到UserVo对象的telNumber字段上了,User对象中的Date类型的birthday被映射到UserVo中的String类型的birthday上了,完全OK!

    6、MapStruct实现原理

    其实MapStruct的实现原理很简单,就是根据我们在Mapper接口中使用的@Mapper@Mapping等注解,在运行时生成接口的实现类,我们可以打开项目的target目录看下; <img src="https://pic2.zhimg.com/80/v2-0024313fcb6d16306447610776a6fbbd_1440w.jpg">

    下面是MapStruct为UserMapper生成好的对象映射代码,可以和手写Getter、Setter说再见了!

    public class UserMapperImpl implements UserMapper {
        public UserMapperImpl() {
          public UserVo convertToVo(User user) {
            if (user == null) {
                return null;
            } else {
                UserVo userVo = new UserVo();
                userVo.setTelNumber(user.getTel());
                if (user.getBirthday() != null) {
                    userVo.setBirthday((new SimpleDateFormat("yyyy-MM-dd")).format(user.getBirthday()));
                if (user.getId() != null) {
                    userVo.setId(user.getId().longValue());
                userVo.setUserName(user.getUserName());
                userVo.setPassword(user.getPassword());
                userVo.setEmail(user.getEmail());
                userVo.setIdCardNo(user.getIdCardNo());
                userVo.setIcon(user.getIcon());
                userVo.setGender(user.getGender());
                return userVo;
    

    7、MapStruct对集合进行映射

    MapStruct也提供了集合映射的功能,可以直接将一个PO列表转换为一个VO列表,再也不用一个个对象转换了!

    UserMapper接口中添加toVoList方法用于列表转换;

    @Mapper
    public interface UserMapper {
        UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
        @Mapping(source = "tel", target = "telNumber")
        @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd")
        List<UserVo> toVoList(List<User> list);
    

    在Controller中创建测试接口,直接通过Mapper接口中的INSTANCE实例调用转换方法toVoList

    @RestController
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        UserMapper userMapper;
        @GetMapping("/mapStructToList")
        public Result mapStructToList() {
     User user1 = new User().setId(1).setEmail("1964327885@qq.com").setUserName("小慕")
                    .setBirthday(new Date()).setTel("18772563087");
     User user2 = new User().setId(2).setEmail("1664687767@qq.com").setUserName("小王")
                    .setBirthday(new Date()).setTel("13455332134");
     User user3 = new User().setId(3).setEmail("1323243433@qq.com").setUserName("小张")
                    .setBirthday(new Date()).setTel("1534323232");
     List<User> userList = new ArrayList<>();
       userList.add(user1);
       userList.add(user2);
       userList.add(user3);
       List<UserVo> userVoList = UserMapper.INSTANCE.toVoList(userList);
       System.out.println(JSON.toJSONString(userVoList));
       return Result.success(userVoList);
    

    打印结果:

    [{"birthday":"2021-11-24","email":"1964327885@qq.com","id":1,"telNumber":"18772563087","userName":"小慕"},{"birthday":"2021-11-24","email":"1664687767@qq.com","id":2,"telNumber":"13455332134","userName":"小王"},{"birthday":"2021-11-24","email":"1323243433@qq.com","id":3,"telNumber":"1534323232","userName":"小张"}]
    

    可见集合映射完全OK

    8、子集和映射

    MapStruct对于对象中包含子对象也需要转换的情况也是有所支持的。

  • 例如我们有一个订单PO对象Order,嵌套有UserProduct对象;
  • @Data
    @EqualsAndHashCode(callSuper = false)
    public class Order {
        private Long id;
        private String orderNo;//订单号
        private Date createTime;
        private String receiverAddress; //收货地址
        private User user;//订单所属的用户
        private List<Product> productList; //商品集合
    
    @Data
    @Accessors(chain = true)
    @EqualsAndHashCode(callSuper = false)
    public class Product {
        private Long id;
        private String productSn;
        private String name;
        private String subTitle;
        private String brandName;
        private BigDecimal price;
        private Integer count;//商品数量
        private Date createTime;
    
  • 我们需要转换为OrderDo对象,OrderDo中包含UserVoProductVo两个子对象同样需要转换;
  • @Data
    @EqualsAndHashCode(callSuper = false)
    public class OrderVo {
        private Long id;
        private String orderNo; //订单号
        private Date createTime;
        private String receiverAddress; //收货地址
        //子对象映射Dto
        private UserVo userVo;//订单所属的用户
        //子对象数组映射Dto
        private List<ProductVo> productVoList; //商品集合
    
    @Data
    @EqualsAndHashCode(callSuper = false)
    public class ProductVo {
        //使用常量
        private Long id;
        //使用表达式生成属性
        private String productSn;
        private String name;
        private String subTitle;
        private String brandName;
        private BigDecimal price;
        //使用默认值
        private Integer number;//商品数量
        private Date createTime;
    
  • 我们只需要创建一个Mapper接口,然后通过使用uses将子对象的转换Mapper注入进来,然后通过@Mapping设置好属性映射规则即可;
  • @Mapper(uses = {UserMapper.class,ProductMapper.class})
    public interface OrderMapper {
        OrderMapper INSTANCE = Mappers.getMapper(OrderMapper.class);
        @Mapping(source = "user",target = "UserVo")
        @Mapping(source = "productList",target = "productVoList")
        OrderVo convertToVo(Order order);
    
    @Mapper(imports = {UUID.class})
    public interface ProductMapper {
        ProductMapper INSTANCE = Mappers.getMapper(ProductMapper.class);
        @Mapping(target = "id",constant = "-1L")
        @Mapping(source = "count",target = "number",defaultValue = "1")
        @Mapping(target = "productSn",expression = "java(UUID.randomUUID().toString())")
        ProductVo convertToVo(Product product);
    
  • 接下来在Controller中创建测试接口,直接通过Mapper中的INSTANCE实例调用转换方法toDto
  • @RestController
    @RequestMapping("/order")
    public class OrderController {
        @ApiOperation(value = "子对象映射")
        @GetMapping("/mapStructToSubVo")
        public Result mapStructToSubVo() {
            //创建一个user对象
            User user = new User();
            user.setId(1).setEmail("1964327885@qq.com").setUserName("小慕")
                .setBirthday(new Date()).setTel("18772563087");
            //创建productList
            List<Product> productList = new ArrayList<>();
            productList.add(new Product().setCount(3));
            productList.add(new Product().setCount(7));
            Order order = new Order();
            order.setUser(user).setProductList(productList);
            OrderVo orderVo = OrderMapper.INSTANCE.convertToVo(order);
            System.out.println(JSON.toJSONString(orderVo));
            return Result.success(orderVo);
    

    打印结果:

    {"productVoList":[{"id":-1,"number":3,"productSn":"a27c7f07-7f5b-45e1-ae99-cea741b35d85"},{"id":-1,"number":7,"productSn":"75012846-bdc2-4dc1-849b-47442bba70c8"}],"userVo":{"birthday":"2021-11-24","email":"1964327885@qq.com","id":1,"telNumber":"18772563087","userName":"小慕"}}
    

    从运行结果来看,可以发现子对象属性已经被转换了。 Product对象中count字段的值映射到ProductVo的number字段上了,完全OK

    9、合并映射

    MapStruct也支持把多个对象属性映射到一个对象中去。

  • 例如这里把UserOrder的部分属性映射到UserOrderDto中去;
  • @Data
    @EqualsAndHashCode(callSuper = false)
    public class UserOrderVo {
        private Long id ;//用户id
        private String userName;//用户名
        private String password; //密码
        //与PO类型不同的属性
        private String birthday;//生日
        //与PO名称不同的属性
        private String telNumber;//电话号码
        private String email;
        private String idCardNo;//身份证号
        private String icon; //头像
        private Integer gender;//性别
        private String orderNo; //订单号
        private String receiverAddress; //用户收货地址
    
  • 然后在Mapper中添加toUserOrderVo方法,这里需要注意的是由于参数中具有两个属性,需要通过参数名称.属性的名称来指定source来防止冲突(这两个参数中都有id属性);
  • @Mapper
    public interface UserMapper {
        UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
        @Mapping(source = "user.tel",target = "telNumber")
        @Mapping(source = "user.birthday",target = "birthday",dateFormat = "yyyy-MM-dd")
        @Mapping(source = "user.id",target = "id")
        @Mapping(source = "order.orderNo", target = "orderNo")
        @Mapping(source = "order.receiverAddress", target = "receiverAddress")
        UserOrderVo toUserOrderVo(User user, Order order); 
    

    接下来在Controller中创建测试接口,直接通过Mapper中的INSTANCE实例调用转换方法toUserOrderDto

      @ApiOperation(value = "组合映射")
        @GetMapping("/compositeMapping")
        public Result compositeMapping() {
            //新建一个user对象
            User user = new User();
            user.setId(1).setEmail("1964327885@qq.com").setUserName("小慕")
                .setBirthday(new Date()).setTel("18772563087");
            //新建一个Order对象
            Order order = new Order();
            order.setReceiverAddress("湖北省武汉市洪山区").setOrderNo("323121213232");
            UserOrderVo userOrderVo = UserMapper.INSTANCE.toUserOrderVo(user,order);
            System.out.println(JSON.toJSONString(userOrderVo));
            return Result.success(userOrderVo);
    

    打印结果:

    {"birthday":"2021-11-24","email":"1964327885@qq.com","id":1,"orderNo":"323121213232","receiverAddress":"湖北省武汉市洪山区","telNumber":"18772563087","userName":"小慕"}
    

    从打印结果来看,可以发现User和Order中的属性已经被映射到userOrderVo中去了。

    三、MapStruct的进阶玩法

    通过上面的基本使用,大家已经可以玩转MapStruct了,下面我们再来介绍一些进阶的用法。

    1、使用Spring依赖注入

    上面的例子我们都是通过Mapper接口中的INSTANCE实例来调用方法的,在Spring中我们也是可以使用依赖注入的。

  • 想要使用依赖注入,我们只要将@Mapper注解的componentModel参数设置为spring即可,这样在生成接口实现类时,MapperStruct会为其添加@Component注解; (<font color=red>注意:这些mapper文件不要被mybatis的MapScan给扫包扫到了,不然生成的代理对象就是mybatis代理的对象,而不是mapstruct代理的对象了,所以一定要注意</font>)
  • @Mapper(componentModel = "spring")  //使用spring依赖注入
    public interface UserMapper {
        // UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
        @Mapping(source = "tel", target = "telNumber")
        @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd")
        UserVo convertToVo(User user);
    
  • 接下来在Controller中使用@Autowired注解注入即可使用;
  • @RestController
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        UserMapper userMapper;
        @GetMapping("/mapStructToVo")
        public Result mapStructToVo() {
            User user = new User();
            user.setId(1).setEmail("1964327885@qq.com").setUserName("小慕").setBirthday(new Date()).setTel("18772563087");
            UserVo userVo = userMapper.convertToVo(user);
            System.out.println(JSON.toJSONString(userVo));
            return Result.success(userVo);
    

    打印结果:

    {"birthday":"2021-11-24",
     "email":"1964327885@qq.com","id":1,"telNumber":"18772563087","userName":"小慕"}
    

    2、使用常量、默认值和表达式

    使用MapStruct映射属性时,我们可以设置属性为常量或者默认值,也可以通过Java中的方法编写表达式来自动生成属性。

    @Data
    @Accessors(chain = true)
    @EqualsAndHashCode(callSuper = false)
    public class Product {
        private Long id;
        private String productSn;
        private String name;
        private String subTitle;
        private String brandName;
        private BigDecimal price;
        private Integer count;//商品数量
        private Date createTime;
    
  • 我们想把Product转换为ProductVo对象,id属性设置为常量,count设置默认值为1,productSn设置为UUID生成;
  • @Data
    @EqualsAndHashCode(callSuper = false)
    public class ProductVo {
        //使用常量
        private Long id;
        //使用表达式生成属性
        private String productSn;
        private String name;
        private String subTitle;
        private String brandName;
        private BigDecimal price;
        //使用默认值
        private Integer number;//商品数量
        private Date createTime;
    
  • 创建ProductMapper接口,通过@Mapping注解中的constantdefaultValueexpression设置好映射规则;
  • @Mapper(imports = {UUID.class})
    public interface ProductMapper {
        ProductMapper INSTANCE = Mappers.getMapper(ProductMapper.class);
        @Mapping(target = "id",constant = "-1L")  //给转换后的productVo的id字段设置为常量-1
        @Mapping(source = "count",target = "number",defaultValue = "1")
        @Mapping(target = "productSn",expression = "java(UUID.randomUUID().toString())")
        ProductVo convertToVo(Product product);
    
  • 接下来在Controller中创建测试接口,直接通过接口中的INSTANCE实例调用转换方法convertToVo
  • @RestController
    @RequestMapping("/product")
    public class ProductController {
        @GetMapping("/defaultMapping")
        public Result defaultMapping() {
            Product product = new Product();
            product.setId(100L);
            product.setCount(null);
            ProductVo productVo = ProductMapper.INSTANCE.convertToVo(product);
            System.out.println(JSON.toJSONString(productVo));
            return Result.success(productVo);
    

    打印结果:

    {"id":-1,"number":1,"productSn":"5673a313-fde6-450c-8c55-f8242b57af2a"}
    

    3、在MapStruct映射前后进行自定义切面处理

    MapStruct也支持在映射前后做一些自定义操作,类似Spring的AOP中的切面。

  • 由于此时我们需要创建自定义处理方法,创建一个抽象类ProductRoundMapper,通过@BeforeMapping注解自定义映射前操作,通过@AfterMapping注解自定义映射后操作;
  • @Mapper(imports = {UUID.class})
    public abstract class ProductRoundMapper {
        public static ProductRoundMapper INSTANCE = Mappers.getMapper(ProductRoundMapper.class);
        @Mapping(target = "id",constant = "-1L")
        @Mapping(source = "count",target = "number",defaultValue = "1")
        @Mapping(target = "productSn",expression = "java(UUID.randomUUID().toString())")
        public abstract ProductVo convertToVo(Product product);
        @BeforeMapping
        public void beforeMapping(Product product){
            //映射前当price<0时设置为0
            if(product.getPrice().compareTo(BigDecimal.ZERO)<0){
                product.setPrice(BigDecimal.ZERO);
        @AfterMapping
        public void afterMapping(@MappingTarget ProductVo productVo){
            //映射后设置当前时间为createTime
            productVo.setCreateTime(new Date());
    
  • 接下来在Controller中创建测试接口,直接通过Mapper中的INSTANCE实例调用转换方法convertToVo
  • @RestController
    @RequestMapping("/product")
    public class ProductController {
        @GetMapping("/defaultMapping")
        public Result defaultMapping() {
            Product product = new Product();
            product.setId(100L);
            product.setCount(null);
            product.setPrice(new BigDecimal(-100) );
            ProductVo productVo = ProductRoundMapper.INSTANCE.convertToVo(product);
            System.out.println(JSON.toJSONStringWithDateFormat(productVo,"yyyy-MM-dd HH:mm:ss"));
            return Result.success(productVo);
    

    打印结果:

    {"createTime":"2021-11-26 11:26:11","id":-1,"number":1,"price":0,"productSn":"cf387cf1-8750-4f5a-adaa-2037ac7d719a"}
    

    4、MapStruct里的验证器

  • 我们先创建一个验证类,当Userd对象的tel超过11位时就抛出异常;
  • public class UserValidator {
        public String validatePrice(String tel) throws Exception {
            if(StringUtils.isNotBlank(tel)&&tel.length()>11){
                throw new Exception("手机号位数超过11位了");
            return tel;
    
  • 之后我们通过@Mapper注解的uses属性运用验证类;
  • @Mapper(uses = {UserValidator.class},imports = {UUID.class})
    public interface UserExceptionMapper {
        UserExceptionMapper INSTANCE = Mappers.getMapper(UserExceptionMapper.class);
        @Mapping(source = "tel", target = "telNumber")
        @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd")
        UserVo convertToVo(User user) throws Exception;
    

    最后我们在Controller层验证下效果

    @RestController
    @RequestMapping("/user")
    public class UserController {
        @GetMapping("/mapStructToVo")
        public Result mapStructToVo() {
            User user = new User();
            user.setId(1).setEmail("1964327885@qq.com").setUserName("小慕").setBirthday(new Date()).setTel("18772563087");
            try {
                UserVo userVo = UserExceptionMapper.INSTANCE.convertToVo(user);
            }catch (Exception e) {
                System.out.println(e.getMessage());
            return Result.success("");
    

    打印结果:

    手机号位数超过11位了
    

    专注Java技术进阶,系统设计,计算机网络,数据结构算法,操作系统,设计模式,计算机组成原理等等更多精彩内容,尽情期待

    <img src="https://img-blog.csdnimg.cn/20210528194309470.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMjQxOTU3,size_16,color_FFFFFF,t_70">