考虑以下POJO:
public class SchedulePayload {
public String name;
public String scheduler;
public PeriodPayload notificationPeriod;
public PeriodPayload schedulePeriod;
}
private class Lecture {
public ZonedDateTime start;
public ZonedDateTime end;
}
public class XmlSchedule {
public String scheduleName;
public String schedulerName;
public DateTime notificationFrom;
public DateTime notificationTo;
public DateTime scheduleFrom;
public DateTime scheduleTo;
}
public class PeriodPayload {
public DateTime start;
public DateTime finish;
}
Run Code Online (Sandbox Code Playgroud)
使用MapStruct,我创建了一个映射XmlSchedule到a 的映射器SchedulePayload.由于"业务""逻辑",我需要约束notificationPeriod和schedulePeriod一个Lecture's start和end字段值.以下是我使用另一个课程:
@Mapper(imports …Run Code Online (Sandbox Code Playgroud) 我创建如下的映射.如何将平面对象属性(如街道,城市等)映射到域对象中的嵌套地址.当我尝试我有一个错误:
[ERROR]诊断:返回类型中的未知属性"address.postalCode".@Mapping(source ="city",target ="address.city"),
@Mapper(componentModel = "spring", uses = {})
public interface CompanyMapper {
@Mappings({
@Mapping(source = "id", target = "id"),
@Mapping(source = "street", target = "address.street"),
@Mapping(source = "city", target = "address.city"),
@Mapping(source = "postalCode", target = "address.postalCode"),
@Mapping(source = "province", target = "address.province"),
})
DomainObject map(DtoObject dto);
Run Code Online (Sandbox Code Playgroud)
和班级......
public class Address {
private String street;
private Integer streetNumber;
private String city;
private String postalCode;
private String province;
//getters and setters
}
public class DomainObject {
private String id; …Run Code Online (Sandbox Code Playgroud) 我有User.java2个类作为父类:和FacebookUser.java,TwitterUser.java它们是返回的实体,取决于使用的数据库中的type列DiscriminatorColumn,我想编写正确的映射器来映射可以是FacebookUser或TwitterUser实例的User。我有以下似乎无法按预期工作的映射器,仅映射User父级而不是子级:
@Mapper
public interface UserMapper {
public static UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
User map(UserDTO userDTO);
@InheritInverseConfiguration
UserDTO map(User user);
List<UserDTO> map(List<User> users);
FacebookUser map(FacebookUserDTO userDTO);
@InheritInverseConfiguration
FacebookUserDTO map(FacebookUser user);
TwitterUser map(TwitterUserDTO userDTO);
@InheritInverseConfiguration
TwitterUserDTO map(TwitterUser user);
}
Run Code Online (Sandbox Code Playgroud)
然后我用:
UserDTO userDto = UserMapper.INSTANCE.map(user);
Run Code Online (Sandbox Code Playgroud)
要映射的类:
@Entity
@Table(name = "users")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 10)
@DiscriminatorValue(value = "Local")
public class User {
@Column
private String firstName; …Run Code Online (Sandbox Code Playgroud) 我想检查方法中的空值
@Override
public void updateFooFromNonNullAttributesOfDto(FooDto fooDto, Foo foo) {
if ( fooDto== null ) {
return;
}
if ( fooDto.getBar() != null ) {
site.setBar( fooDto.getBar() );
}
if ( fooDto.getBaz() != null ) {
site.setBar( fooDto.getBaz() );
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用
@Mapper( NullValueCheckStrategy.ALWAYS)
Run Code Online (Sandbox Code Playgroud)
它检查所有方法,但我只想检查一种......如何解决这个问题?
我想使用 Mapstruct 将内部模型映射到 Kotlin 项目中由 OpenApi3 codegen 生成的模型。
当我编译项目时,Mapstruct 似乎无法找到 OpenApi3 codegen 插件生成的源,因为生成的实现包含 aNonExistentClass而不是我的 OpenApi 模型。
我的插件配置是
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>kapt</id>
<phase>process-sources</phase>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Spring 注入映射器对象(类是 TypeMapper)依赖项,如下所示,
@Mapper(componentModel = "spring",
uses = {TypeMapper.class})
public interface AttachmentMapper {
AttachmentMapper MAPPER = Mappers.getMapper(AttachmentMapper.class);
@Mappings({
@Mapping(source = "type", target = "type") })
AttachmentDTO toDTO(Attachment attachment);
}
Run Code Online (Sandbox Code Playgroud)
TypeMapper的代码如下,
@Component
@Mapper
public abstract class TypeMapper {
public abstract Type mapType(DtoType DtoType);
@InheritConfiguration(name = "mapType")
public abstract DtoType mapDtoType(Type type);
}
Run Code Online (Sandbox Code Playgroud)
生成的AttachmentMapperImpl代码如下,
public class AttachmentMapperImpl implements AttachmentMapper {
@Autowired
private TypeMapper typeMapper;
public AttachmentDto toDTO(Attachment attachment) {
if ( attachment == null) {
return null;
}
attachmentDTO.setType(typeMapper.mapDtoType(attachment.getType())); …Run Code Online (Sandbox Code Playgroud) 我有一个带有默认值的 kotlin 数据类,当我尝试使用 MapStruct 映射它时,它会在运行时抛出错误,因为它会尝试将 null 值分配给具有默认值的属性的不可空类型。我知道在 @Mapping 注释中分配默认值,但是 MapStruct 有没有办法考虑数据类默认值,而不是我必须这样做两次?
这是例子:
data class A(val property1: String = "prop 1", val property2: String)
data class B(val property2: String)
@Mapper
interface SomeMapper {
...
fun mapBtoA(b: B): A
}
val b = B("prop 2 val")
val a: A = SomeMapper.INSTANCE.mapBtoA(b)
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,它将尝试将 null 分配给 property1 而不是 prop 1 默认值。
我有车:
还有CarDTO:
在我的服务类中,我传递了附加参数“所有者”,并且我需要转换列表。
是否可以将“所有者”添加到Mapper?
如果是,那么我想它应该与此类似(不起作用)。
@Mapper
public interface CarMapper {
@Mapping(target = "owner", source = "owner")
List<Car> mapCars(List<CarDTO> cars, String owner);
}
Run Code Online (Sandbox Code Playgroud) 有两个源类 A 和 B
class A {
public Double x;
public Double y;
}
class B {
public Double x;
public Double y;
}
Run Code Online (Sandbox Code Playgroud)
和另一个目标类 C
class C {
public Double x;
public Double y;
}
Run Code Online (Sandbox Code Playgroud)
很清楚如何将 A 映射到 C 或 B 到 C。
是否可以将某些功能(例如,源对象的添加或 pow)映射到目标对象,以便生成的代码如下所示
C.x = A.x + B.x
C.y = A.y + B.y
Run Code Online (Sandbox Code Playgroud)
或者
C.x = Math.pow(A.x, B.x)
C.y = Math.pow(A.y, B.y)
Run Code Online (Sandbox Code Playgroud) 我一直在浏览 MapStruct 的文档,似乎无法弄清楚何时@BeanMapping应该使用注释。该文档似乎也广泛用于@Mapping大多数情况。@BeanMapping有人可以解释一下注释的用法吗?也许有一个例子?
mapstruct ×10
java ×7
kotlin ×2
mapping ×2
spring ×2
data-class ×1
inheritance ×1
javabeans ×1
oop ×1
openapi ×1
polymorphism ×1
spring-boot ×1