MapStruct的用法"/>
MapStruct的用法
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、依赖导入
- 二、简单使用
- 2.1、定义转换的接口
- 2.2、创建实体类
- 2.3、测试
- 2.4、底层实现
- 三、常用的注解
- 3.1、@Mapping(target = "xxx1",source = "xxx2")
- 3.2、@Mapping(target = "xxx", expression = "java(xxx1()")
- 3.4、@Mapping(target = "xxx",constant = "xxx")
- 3.5、@Mapping(target = "xx",defaultValue="x")
- 3.6、@Mapping(target = "id", ignore = true)
前言
最近工作中发现公司代码中频繁用到MapStruct,了解之后发现特别好用,特此记录一下
一、依赖导入
使用Mapstruct需要依赖的包如下,mapstruct、mapstruct-processor、lombok,可以去仓库中查看最新版本。
<dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>1.5.0.Final</version></dependency><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>1.5.0.Final</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version></dependency>
二、简单使用
2.1、定义转换的接口
@Mapper
public interface copy {copy INSTANCT= Mappers.getMapper(copy.class);UserPo po2entity(UserEntity userEntity);
}
2.2、创建实体类
import lombok.Data;
import java.util.Date;@Data
public class UserPo {private String name;private Integer age;private Date createTime;
}
import lombok.Data;
import java.util.Date;
@Data
public class UserEntity {private String name;private Integer age;private Date createTime;
}
2.3、测试
public static void main(String[] args) {UserEntity userEntity=new UserEntity();userEntity.setAge(18);userEntity.setName("汪汪");userEntity.setCreateTime(new Date());UserPo userPo=copy.INSTANCT.po2entity(userEntity);System.out.println(userEntity);System.out.println("-----------------------------------------");System.out.println(userPo);}
可以看到已经完美复制成功了
2.4、底层实现
那么这是怎么实现的呢?
从生成的代码可以看出,转化过程非常简单,只使用了UserPo的get方法和UserEntity的set方法,没有复杂的逻辑处理,清晰明了,所以性能很高。
三、常用的注解
3.1、@Mapping(target = “xxx1”,source = “xxx2”)
这个注解可以将 userEntity中name的值映射到UserPo中的bigname字段上
例如
首先修改UserPo
@Data
public class UserPo {private String bigname;private Integer age;private Date createTime;
}
然后修改对应方法
@Mapping(target = "bigname",source = "name")UserPo po2entity(UserEntity userEntity);
可以看到结果是预期的结果
3.2、@Mapping(target = “xxx”, expression = “java(xxx1()”)
这个注解可以把UserPo中createTime的值转换为java中的某个方法返回的值
@Mapping(target = "createTime",expression = "java(getdata())")UserPo po2entity(UserEntity userEntity);default Date getdata(){return new Date();}
在主函数中去掉userEntity.setCreateTime(new Date()
public static void main(String[] args) {UserEntity userEntity=new UserEntity();userEntity.setAge(18);userEntity.setName("汪汪");
// userEntity.setCreateTime(new Date());UserPo userPo=copy.INSTANCT.po2entity(userEntity);System.out.println(userEntity);System.out.println("-----------------------------------------");System.out.println(userPo);}
结果如下
可以看见符合预期结果
假如说一个字段的值需要从数据库里查 那么方法换成对应查询的方法就可以了
3.4、@Mapping(target = “xxx”,constant = “xxx”)
这个注解可以把某个值赋初始值
@Mapping(target = "age",constant = "1000")
UserPo po2entity(UserEntity userEntity);
可以看到符合预期结果
3.5、@Mapping(target = “xx”,defaultValue=“x”)
该注解判断xx字段有无接收到值,没有的话赋默认值x 有的话用接收到的值
@Mapping(target = "age",defaultValue="1")UserPo po2entity(UserEntity userEntity);
可以看到符合预期结果
3.6、@Mapping(target = “id”, ignore = true)
这个注解可以要指定映射的字段被忽略
@Mapping(target = "age",ignore = true)UserPo po2entity(UserEntity userEntity);
结果是符合预期的
更多推荐
MapStruct的用法
发布评论