我有一个映射器UserMapper,它映射过滤器以供进一步的数据库选择。
@Mapper(uses = {RoleMapper.class})
public interface UserMapper {
@Mapping(source = "roleIds", target = "roles")
UserFilter toUserFilter(UserFilterDto dto);
}
@Mapper
public interface RoleMapper {
default List<Role> roleIdListToRoleList(List<Long> roleIds) {
return Objects.isNull(roleIds)
? Lists.newArrayList()
: roleIds.stream().map(id -> Role.builder().id(id).build()).collect(Collectors.toList());
}
}
}
Run Code Online (Sandbox Code Playgroud)
源DTO
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserFilterDto extends FilterDto {
@JsonProperty("username")
private String username;
@JsonProperty("isEnabled")
private Boolean isEnabled;
@JsonProperty("roleIds")
private List<Long> roleIds;
}
Run Code Online (Sandbox Code Playgroud)
目标对象
@Data
@Builder
class UserFilter {
private String username;
private …Run Code Online (Sandbox Code Playgroud) 我刚刚克隆了一个带有一些映射结构映射器的存储库(1.3.1.Final),由于以下原因,构建失败:
java: No property named "" exists in source parameter(s). Did you mean "sourceField1"?
我将 mapstruct 更新为 version 1.4.2.Final,但结果相同。
查看代码,我发现我们有以下情况:
SourceClass与字段sourceField1, sourceField2,sourceField3TargetClass与字段targetField1, targetField2,notInSourceField @Mapper(componentModel = "spring")
public interface TheMapper {
@Mapping(target = "targetField1", source = "sourceField1")
@Mapping(target = "targetField2", source = "sourceField2")
@Mapping(target = "notInSourceField", source = "")
TargetClass mapToTarget(SourceClass source);
}
Run Code Online (Sandbox Code Playgroud)
为了解决上述错误,我将行更改source = ""为:
@Mapping(target = "notInSourceField", expression = "java(\"\")") //means that notInSourceField …
我尝试将 MapStruct 与 QueryDsl、Spring Boot 3 和 Java 17 一起使用,但maven-compiler-plugin我用于 MapStruct 的似乎阻止了 QueryDsl 资源的生成。
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>5.0.0</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.0.0</version>
<classifier>jakarta</classifier>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
如果我删除maven-compiler-pluginQueryDsl 资源,则会生成但 MapStruct 不会生成。
我还尝试添加 QueryDsl 注释处理器,但没有任何运气。
<path>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.0.0</version>
</path>
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
我正在使用mapstrcut将实体映射到dto,现在面临一个问题,在DTO中有一个额外的字段“折扣”,并且我需要mapstruct来管理填充此字段,并且该字段是根据2计算的值(salePrice和retailPrice)。
我的问题是如何像使用mapstruct一样进行这种映射。
@Mapping(target="discount", source="retailPrice-salePrice")
Run Code Online (Sandbox Code Playgroud)
当我尝试添加此行时,构建时的代码中断表示:
error: No property named "salePrice*retailPrice" exists in source parameter(s).
@Mapping(source="salePrice*retailPrice", target="discount")
Run Code Online (Sandbox Code Playgroud)
那么我该如何进行映射呢?
我正在尝试使用 MapStruct 来映射两个对象。我一直在寻找一段时间,但我什么也没找到,尽管我是编程新手,所以我确信它比我制作它更容易。
这是一些剥离的代码(请注意,实际代码更复杂,来自 arraylist 的子对象与此处的目标对象子变量的类型不同):
源对象
public class SourceObject {
public ArrayList<ListObject> list = new ArrayList<ListObject>();
public SourceObject() {
list.add(new ListObject());
}
}
Run Code Online (Sandbox Code Playgroud)
列表对象
public class ListObject {
public DetailsObject details = new DetailsObject();
public ListObject() {
details.forename="SourceForename";
details.surname="SourceSurname";
}
}
Run Code Online (Sandbox Code Playgroud)
目标对象
public class DestinationObject {
public DetailsObject details = new DetailsObject();
public DestinationObject() {
details.forename="DestinationForename";
details.surname="DestinationSurname";
}
}
Run Code Online (Sandbox Code Playgroud)
详细信息对象
public class DetailsObject {
public String forename;
public String surname;
}
Run Code Online (Sandbox Code Playgroud)
映射器
@Mappings({
@Mapping(target="details.forename", source="list.get(0).details.forename"),
@Mapping(target="details.surname", source="list.get(0).details.surname"),
})
DestinationObject …Run Code Online (Sandbox Code Playgroud) 考虑以下 POJO:
public class PersonVo {
private String firstName;
private String lastName;
}
private class PersonEntity {
private String fullName;
}
Run Code Online (Sandbox Code Playgroud)
使用 MapStruct,我想创建映射PersonVo到PersonEntity.
我需要映射多个源字段firstName,lastName一个目标归档fullName。
这是我想要的伪代码。
[想要解决方案A]
@Mapper
public interface PersonMapper {
@Mapping(target = "fullName", source = {"firstName", "lastName"}, qualifiedByName="toFullName")
PersonEntity toEntity(PersonVo person);
@Named("toFullName")
String translateToFullName(String firstName, String lastName) {
return firstName + lastName;
}
}
Run Code Online (Sandbox Code Playgroud)
[想要解决方案B]
@Mapper
public interface PersonMapper {
@Mapping(target = "fullName", source = PersonVo.class, qualifiedByName="toFullName")
PersonEntity toEntity(PersonVo …Run Code Online (Sandbox Code Playgroud) 尝试使用@Data 和@Builder 映射嵌套对象时,mapStruct 会抛出以下错误:“在目标类型中找不到属性“profile”的读取访问器。”
@Mapper(componentModel = "spring")
public interface AuthMapper {
// only for testing mapping is working
@Mapping(target = "profile.organization", source = "organization")
RequestCreateOktaUser toEntity(Integer organization);
// only for testing mapping is working
@Mapping(target = "profile.login", source = "request.profile.email")
RequestCreateOktaUser toEntity(RequestMobilePreRegisterLocation.User request);
// Throw error "No read accessor found for property "profile" in target type" at compile time
@Mapping(target = "profile.organization", source = "organization")
@Mapping(target = "profile.login", source = "request.profile.email")
RequestCreateOktaUser toEntity(RequestMobilePreRegisterLocation.User request, Integer organization);
}
Run Code Online (Sandbox Code Playgroud)
为简单起见,使用 Lombok 简化模型 …
我想在带有 gradle 的 groovy 类上使用 Mapstruct 映射器。build.gradle 中的配置看起来像 Java 项目中的配置。
dependencies {
...
compile 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
testAnnotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final' // if you are using mapstruct in test code
}
Run Code Online (Sandbox Code Playgroud)
问题是没有生成映射器的实现类。我还尝试为 groovy 编译任务应用不同的选项,但它不起作用。
compileGroovy {
options.annotationProcessorPath = configurations.annotationProcessor
// if you need to configure mapstruct component model
options.compilerArgs << "-Amapstruct.defaultComponentModel=default"
options.setAnnotationProcessorGeneratedSourcesDirectory( file("$projectDir/src/main/generated/groovy"))
}
Run Code Online (Sandbox Code Playgroud)
有谁知道 Mapstruct 是否可以与 groovy 类一起工作以及我必须如何配置它?
I have a spring boot application with mapstruct where I get this warning on the maven-compiler-plugin testCompile phase:
[WARNING] The following options were not recognized by any processor: '[mapstruct.suppressGeneratorTimestamp]'
As I don't want warnings in my code I have been searching to solve this issue but the only thing that worked this far is to use <showWarnings>false</showWarnings>
我应该怎么做才能解决此警告而不将其静音?
申请文件:
pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> …Run Code Online (Sandbox Code Playgroud)