首页 > 其他分享 >使用MapStruct出现了No property named "productId" exists in source parameter(s). Type "Prod

使用MapStruct出现了No property named "productId" exists in source parameter(s). Type "Prod

时间:2022-11-14 21:12:39浏览次数:49  
标签:named exists No mapstruct Product private source import org

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);
    }

image

解决办法

修改lombok的版本

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.10</version>
</dependency>

解决了

image

标签:named,exists,No,mapstruct,Product,private,source,import,org
From: https://www.cnblogs.com/qbbit/p/16890400.html

相关文章