springboot整合mapstruct

2020年开始改变,从记录博客开始。

当程序员很多年,都没有写博客的习惯,原因很多,感觉没时间写,平常自己也有写笔记,新的一年,开始改变自己,养成写博客的习惯开始吧。

第一篇 记录一下springboot整合mapstruct,感觉还挺好用,放一段介绍,不懂的自行百度

MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach.

The generated mapping code uses plain method invocations and thus is fast, type-safe and easy to understand.

废话不多说,直接上代码,亲测有效

1、springboot 版本 2.0.6.RELEASE 添加依赖,

<!--mapstruct-->
		<dependency>
		    <groupId>org.mapstruct</groupId>
		    <artifactId>mapstruct-jdk8</artifactId>
		    <version>1.3.0.Final</version>
		</dependency>
		<dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
             <version>1.3.0.Final</version>
        </dependency>

2、新建一个接口

@Mapper(componentModel = "spring")
public interface PolicConverter {
	
	PolicConverter INSTANCE = Mappers.getMapper(PolicConverter.class);
	
    @Mappings({})
    PolicDto toDTO(Polic entity);
    
    @Mappings({})
    Polic formDto(PolicForm form);
    
    List<PolicDto> toDTOList(List<Polic> polics);
}

3、maven clean 接口所在项目

4、maven install 接口所在项目

5、注入PolicConverter ,调用即可

@Autowired
private PolicConverter converter;


	@GetMapping()
	public Object get() {
		Polic p = policService.select();
		return converter.toDTO(p);
	}

看完觉得对你有帮助别忘点个赞哦

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值