pom.xml
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mapstruct.version>1.5.3.Final</mapstruct.version>
</properties>
<dependencies>
<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>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {
private Integer id;
private String name;
private BigDecimal price;
}
vo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductVO {
private Integer productId;
private String productName;
private BigDecimal productPrice;
}
convert
package com.qbb.convert;
import com.qbb.entity.Product;
import com.qbb.vo.ProductVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @author QIUQIU&LL (个人博客:https://www.cnblogs.com/qbbit)
* @date 2022-11-14 20:42
* @tags 我爱的人在很远的地方, 我必须更加努力
*/
@Mapper
public interface ProductConvert {
ProductConvert convert = Mappers.getMapper(ProductConvert.class);
@Mappings({
@Mapping(source = "id", target = "productId"),
@Mapping(source = "name", target = "productName"),
@Mapping(source = "price", target = "productPrice")
})
ProductVO toVO(Product product);
}
测试
@Test
public void test01(){
Product qiuqiu = new Product(1, "qiuqiu", new BigDecimal(9999));
ProductVO productVO = ProductConvert.convert.toVO(qiuqiu);
System.out.println("productVO = " + productVO);
}
解决办法
修改lombok的版本
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>