当我运行命令时出现以下错误mvn clean install:
[ERROR] /Users/xxx/xxx/xxx/xxx.xxx/target/generated-sources/kapt/compile/com/xxx/xxx/xxx/xxx/DataMapperImpl.java:[10,40] cannot find symbol
[ERROR] symbol: class DataMapperDecorator
[ERROR] /Users/xxx/xxx/xxx/xxx.xxx/target/generated-sources/kapt/compile/com/xxx/xxx/xxx/xxx/DataMapperImpl.java:[10,74] cannot find symbol
[ERROR] symbol: class DataMapper
[ERROR] /Users/xxx/xxx/xxx/xxx.xxx/xxx/generated-sources/kapt/compile/com/xxx/xxx/xxx/api/DataMapperImpl.java:[12,19] cannot find symbol
[ERROR] symbol: class DataMapper
[ERROR] location: class com.xxx.xxx.xxx.xxx.DataMapperImpl
Run Code Online (Sandbox Code Playgroud)
似乎在 mapstruct 生成DataMapperImpl.java类后,它无法找到类DataMapper和DataMapperDecoretor。
与mapstruct相关的代码在一个xxx.kt文件中:
//Other stuff
...
@Mapper
@DecoratedWith(DataMapperDecorator::class)
interface DataMapper {
@Mappings(
Mapping(source = "data.value", target = "dataValue"),
Mapping(source = "data.current.value", target = "currentValue"),
)
fun toDto(data: Data) : RefDataDto
}
abstract class DataMapperDecorator : …Run Code Online (Sandbox Code Playgroud) 如果源中的相应属性为空,我想将目标对象中的属性映射到默认值(例如,字符串为“”)。我怎样才能做到这一点?我看到
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT
Run Code Online (Sandbox Code Playgroud)
仅在更新时有效。
在创建目标对象的同时是否有可能实现某些目标?
我正在尝试使用 Lombok 和 MapStruct 配置 SpringBoot (v2.6.2),已经配置了 Maven 编译器插件和 lombok-mapstruct-binding (annotationProcessorPaths),但没有创建 lombok 类:
pom.xml
*<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.knowledge</groupId>
<artifactId>knowledge</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>knowledge</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<version.mapstruct>1.4.1.Final</version.mapstruct>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${version.mapstruct}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude> …Run Code Online (Sandbox Code Playgroud) 我正在尝试自定义映射,使用字符串来确定对象属性,因此我写了以下内容:
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public abstract class ProductMapper {
public abstract ProductInput asProductInputFromIdentifier(String identifier);
@AfterMapping
protected void determineIdentifier(String identifier, @MappingTarget ProductInput out) {
if (StringUtils.contains(identifier, '?')) {
out.setExternalId(identifier);
} else {
out.setInernalId(identifier);
}
}
}
Run Code Online (Sandbox Code Playgroud)
生成的类不会调用确定标识符方法。我通过直接在asProductInputFromIdentifier方法上使用 Java 表达式找到了另一个解决方案,但我真的想使用@AfterMapping编写清晰的代码。
@Mapping(target = "externalId", expression = "java( org.apache.commons.lang3.StringUtils.contains(identifier, '|') ? identifier : null )")
@Mapping(target = "internalId", expression = "java( !org.apache.commons.lang3.StringUtils.contains(identifier, '|') ? identifier : null )")
public abstract ProductInput asProductDetailInputFromIdentifier(String identifier);
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它不起作用是因为我在方法参数中没有对象吗?
我似乎无法正确使用mapstruct
@Mapping(target = "products", source = "itemBookType")
SearchBookingResult backToTp(ItemBook itemBook);
Run Code Online (Sandbox Code Playgroud)
运行此代码时,我收到以下错误:
Can't map property "ProductType itemBookType" to "List<ProductOverview> products". Consider to declare/implement a mapping method: "List<ProductOverview> map(ProductType value)".
Run Code Online (Sandbox Code Playgroud)
我在底部添加了以下代码:
List<ProductOverview> map(ProductType value);
Run Code Online (Sandbox Code Playgroud)
但它仍然返回以下错误:
Can't generate mapping method from non-iterable type to iterable type from java stdlib.
Run Code Online (Sandbox Code Playgroud)
物品簿类别:
public class ItemBook {
private ProductType itemBookType; //ProductType class
private Integer idref;
private String reference;
}
Run Code Online (Sandbox Code Playgroud)
产品类型类别:
public enum ProductType {
BOOK, PHONE, GAME
}
Run Code Online (Sandbox Code Playgroud)
搜索预订结果类:
public class SearchBookingResult extends BaseResponse<SearchBookingResult> {
private …Run Code Online (Sandbox Code Playgroud) 我使用MapStruct库来映射对象,但是我收到了这个错误:
无法将属性"java.util.Date aDate"映射到"javax.xml.bind.JAXBElement ADATE".考虑声明/实现映射方法:"javax.xml.bind.JAXBElement map(java.util.Date value)".
我的问题:我应该在哪里取消这种映射方法?
我是MapStruct API的新手,任何人都可以说如何做嵌套映射。我有两个类,一个是我的实际purchaseOrder类,即我的目标类,另一个是EDPurchaseOrder类,即源文件,这里不必担心我使用的命名约定,只需处理源文件和目标文件即可。
源类
源类EDCustomerOrder及其参考类
public class EDCustomerOrder{
private Integer orderID;
private String orderNumber;
private BigDecimal orderTotalQty;
private String UOM;
private PickupDTO pickupPoints;
private Integer supplierID;
private String supplierName;
private String supplierNature;
private EDAddress supplierEDAddress;
}
public class EDPickup{
private List<EDPOItem> items;
}
public class EDAddress{
private String addressLine1;
private String addressLine2;
private String addressLine3;
private String city;
private String state;
private string countryCode;
private String country;
private String postalCode;
}
public class EDPOItem{
private Integer itemID;
private String itemCode;
private …Run Code Online (Sandbox Code Playgroud) 我正在使用mapstruct映射我的实体和dto类...我在mapper类上存在循环问题...
我没有意识形态该怎么办...这是我的映射器课程
@Mapper(componentModel = "spring", uses = {BrandMapper.class})
public interface VehicleTypeMapper {
VehicleTypeDTO vehicleTypetoVehicleTypeDTO(VehicleType vehicleType);
Iterable<VehicleTypeDTO> vehicleTypetoVehicleTypeDTO(Iterable<VehicleType> vehicleTypes);
VehicleType vehicleTypeDTOtoVehicleType(VehicleTypeDTO vehicleTypeDTO);
}
@Mapper(componentModel = "spring", uses = { VehicleTypeMapper.class, ModelMapper.class })
public interface BrandMapper {
BrandDTO brandtoBrandDTO(Brand brand);
Iterable<BrandDTO> brandtoBrandDTO(Iterable<Brand> brands);
Brand brandDTOtoBrand(BrandDTO brandDTO);
}
Run Code Online (Sandbox Code Playgroud)
我的实体类... DTO与我的实体类具有相同的属性...
@Entity
@Table(name = "tb_brand")
public class Brand implements Serializable {
private static final long serialVersionUID = 1506494747401320985L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@ManyToOne
@JoinColumn(name = …Run Code Online (Sandbox Code Playgroud) 在以下情况下,如何使用MapStruct进行bean映射。
class Source {
private String sourceId;
private List<Course> courses; //always returns only one course
}
class Course {
private String courseName;
private List<Student> students;
}
class Student {
private String studentName;
}
class Target {
private String targetId;
private String subjectName;
private List<Member> members;
}
class Member {
private String memberName;
}
Run Code Online (Sandbox Code Playgroud)
现在我想将sourceId映射到targetId,将courseName映射到subjectName,将StudentName映射到memberName(从列表到列表)。
MapStruct 1.2是否可以将具有特定值的源属性映射到目标中的特定不同值?
我想这样的事情:
public abstract class JiraKpmMapper {
@Mappings({
@Mapping(source = "mySource.propA", target = "myTarget.propX")
})
@ValueMappings({
@ValueMapping(source = "ABC", target = "XYZ"),
@ValueMapping(source = "123", target = "789")
})
public abstract MyTarget source2Target(final MySource mySource);
}
Run Code Online (Sandbox Code Playgroud)
因此,当MapStruct在映射期间看到当mySource.propA具有值"ABC"时,myTarget.propX需要设置为值"XYZ",依此类推.
更确切地说,我甚至想要一些更详细的东西:目标应该是一个阶级避风港的三个属性,其中必须将得到的目标值分成.例如,如果mySource.propA的值为"ABC",则目标myTarget应该获得类似"V01.123.456.AB"的值.该值又应分为preValue,middleValue和endValue:
preValue ="V01"
middleValue ="123.456"
endValue ="AB"
因此,没有包含完整结果字符串的属性.
这就是为什么我已经编写了一个自定义映射器,并告诉MyMapper通过它来使用它
@Mapper(componentModel = "spring", uses = MyCustomMapper.class)
Run Code Online (Sandbox Code Playgroud)
这个工作到目前为止,但我无法实现它告诉MyCustomMapper将"V01.123.456.AB"放入目标,当souzrce带有"ABC"时.
mapstruct ×10
java ×4
spring ×3
mapping ×2
spring-boot ×2
conditional ×1
kotlin ×1
lombok ×1
maven ×1