美文网首页Spring Boot 2.0JAVA开源框架-SpringBoot系列
Spring Boot 集成 MapStruct 实现类型转换

Spring Boot 集成 MapStruct 实现类型转换

作者: AaronSimon | 来源:发表于2019-12-12 18:03 被阅读0次

    在项目中经常会用到DO,BO,DTO,VO等对象的相互转化,这就需要一个高效通用的转化工具,毕竟每个字段get/set方法会很麻烦。

    MapStruct 就是这样的一个属性映射工具,只需要定义一个 Mapper 接口,MapStruct就会自动实现这个映射接口,避免了复杂繁琐的映射实现。MapStruct官网地址: http://mapstruct.org/

    本文主要实现Spring Boot 2.0 与 MapStruct 的整合。

    一、添加依赖

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

    二、 编写转换Mapper

    VO对象:

    package pers.simon.boot.validator.po;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    import pers.simon.boot.validator.annotate.Create;
    import pers.simon.boot.validator.annotate.IdCard;
    import pers.simon.boot.validator.annotate.Update;
    
    import javax.validation.constraints.*;
    import java.util.Date;
    
    /**
     * @author simon
     * @date 2019/12/10 10:28
     */
    @ApiModel(description = "用户信息表单")
    @Data
    public class UserVo {
    
        @ApiModelProperty(value = "用户ID", name = "userId", required = true, example = "123")
        @Null(message = "用户ID必须为空",groups = Create.class)
        @NotBlank(message = "用户ID不能为空",groups = Update.class)
        private String userId;
    
        @ApiModelProperty(value = "用户名称", name = "userName", required = true, example = "admin")
        @NotBlank(message = "用户名不能为空")
        private String userName;
    
        @ApiModelProperty(value = "生日", name = "birthday", required = true, example = "2019-11-02")
        @Past(message = "生日必须为过去日期")
        @NotNull(message = "生日不能为空")
        private Date birthday;
    
        @ApiModelProperty(value = "年龄", name = "age", required = true, example = "11", dataType = "int")
        @Min(value = 10,message = "年龄必须大于10")
        @Max(value = 30,message = "年龄必须小于30")
        @NotNull(message = "年龄不能为空")
        private Integer age;
    
        @ApiModelProperty(value = "邮箱", name = "email", required = true, example = "11@qq.com")
        @NotBlank(message = "邮箱不能为空")
        @Email(message = "邮箱格式错误")
        private String email;
    
        @ApiModelProperty(value = "手机号", name = "phone", required = true, example = "12345678900")
        @NotNull(message = "手机号不能为空")
        @NotBlank(message = "手机号不能为空")
        @Pattern(regexp ="^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误")
        @Max(value = 11,message = "手机号只能为11位")
        @Min(value = 11,message = "手机号只能为11位")
        private String phone;
    
        @ApiModelProperty(value = "身份证号", name = "idCard", required = true, example = "12345678900")
        @NotBlank(message = "身份证不能为空")
        @IdCard(message = "身份证不合法")
        private String idCard;
    }
    
    

    DTO对象:

    package pers.simon.boot.validator.po;
    
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    import pers.simon.boot.validator.annotate.Create;
    import pers.simon.boot.validator.annotate.IdCard;
    import pers.simon.boot.validator.annotate.Update;
    
    import javax.validation.constraints.*;
    import java.util.Date;
    
    /**
     * @author simon
     * @date 2019/12/12 16:38
     */
    @Data
    public class UserDto {
        private String userId;
    
        private String userName;
    
        private String password;
    
        private Date birthday;
    
        private Integer age;
    
        private String email;
    
        private String telPhone;
    
        private String idCard;
    
        private Date updateTime;
    }
    
    

    转换Mapper

    package pers.simon.boot.validator.convert;
    
    import org.mapstruct.Mapper;
    import org.mapstruct.Mapping;
    import org.mapstruct.Mappings;
    import pers.simon.boot.validator.po.UserDto;
    import pers.simon.boot.validator.po.UserVo;
    
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.util.Date;
    
    /**
     * 用户信息
     *
     * @author simon
     * @date 2019/12/12 16:40
     */
    @Mapper(componentModel = "spring",imports = {LocalDateTime.class,Date.class, ZoneId.class})//交给spring管理
    public interface UserConvert {
        // 用来调用实例 实际开发中可使用注入Spring  不写
        //UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);
        @Mappings({
                @Mapping(target = "updateTime",expression = "java(Date.from( LocalDateTime.now().atZone( ZoneId.systemDefault()).toInstant()))"),
                @Mapping(target = "password",constant = "admin")
        })
        UserDto convert(UserVo userVo);
    
        @Mapping(target = "phone",source = "telPhone")
        UserVo convert(UserDto userDto);
    
    }
    
    
    1. 用@org.mapstruct.Mapper(不要跟mybatis注解混淆)标记类,说明这是一个实体类型转换接口
    2. 转换Mapper提供了两个转换方法,分别是UserDto--->UserVo和UserVo---->UserDto
    3. componentModel = "spring" 指定 将转换Mapper交给Spring 管理,可实现spring bean 注入
    4. imports 用于导入Java 表达式所需要的类,expression 接收一个 java() 包括的表达式
    5. source 代表转换的源,target 代表转换的目标
    6. MapStruct 最终调用的是 setter 和 getter 方法,而非反射
    7. 对常量赋值不能指定 source 属性,@Mapping(target = "password",constant = "admin")

    三、测试类

    package pers.simon.boot.validator.convert;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import pers.simon.boot.validator.po.UserDto;
    import pers.simon.boot.validator.po.UserVo;
    import pers.simon.boot.validator.utils.JsonUtil;
    
    import java.util.Date;
    
    /**
     * @author simon
     * @date 2019/12/12 16:56
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class UserConvertTest {
    
        private UserDto userDto;
        private UserVo userVo;
    
        @Autowired
        private UserConvert userConvert;
    
        @Before
        public void setUp() throws Exception {
            userDto = new UserDto();
            userDto.setUserId("1111");
            userDto.setUserName("admin");
            userDto.setPassword("password");
            userDto.setEmail("11@qq.com");
            userDto.setTelPhone("12345678900");
            userDto.setAge(12);
            userDto.setBirthday(new Date());
            userDto.setIdCard("123321");
            userDto.setUpdateTime(new Date());
    
            userVo = new UserVo();
            userVo.setUserId("1111");
            userVo.setUserName("admin");
            userVo.setEmail("11@qq.com");
            userVo.setAge(12);
            userVo.setBirthday(new Date());
            userVo.setIdCard("123321");
        }
    
        @Test
        public void convert() {
            UserVo convert = userConvert.convert(userDto);
            System.out.println(JsonUtil.toJsonString(convert));
        }
    
        @Test
        public void testConvert() {
            UserDto convert = userConvert.convert(userVo);
            System.out.println(JsonUtil.toJsonString(convert));
        }
    }
    

    相关文章

      网友评论

        本文标题:Spring Boot 集成 MapStruct 实现类型转换

        本文链接:https://www.haomeiwen.com/subject/zpitnctx.html