给出一组四个对象,如:
A{String one, B b}
B{String two, String three}
C{String one, String two}
D{String three}
Run Code Online (Sandbox Code Playgroud)
我希望生成如下映射:
A cAndDToA(C c , D d);
Run Code Online (Sandbox Code Playgroud)
我目前无法找到一种方法来填充A内部的B对象与来自C和D的数据.
有没有人知道这个问题的解决方案,或者有更好的方法?
我的模型类中有几个Boolean字段(源)。我的 DTO 类中的目标字段是String. 我需要映射trueasY和falseas N。有超过 20 个Boolean字段,现在我正在使用 20 多个@Mapping带有expression选项的注释,这是开销。一定有一个我不知道的简单方法或解决方案。任何人都可以帮助简化这个吗?
我正在使用mapstruct版本1.2.0.Final
源码.java
class Source{
private Boolean isNew;
private Boolean anyRestriction;
// several Boolean fields
}
Run Code Online (Sandbox Code Playgroud)
目标.java
class Target{
private String isNew;
private String anyRestriction;
}
Run Code Online (Sandbox Code Playgroud)
助手.java
class Helper{
public String asString(Boolean b){
return b==null ? "N" : (b ? "Y" : "N");
}
}
Run Code Online (Sandbox Code Playgroud)
MyMapper.java
@Mapper interface MyMapper{
@Mappings(
@Mapping(target="isNew", expression="java(Helper.asString(s.isNew()))" …Run Code Online (Sandbox Code Playgroud) 在mapstruct 中,1.3.0.Final我们通过构造函数进行依赖注入。文档说:
生成的映射器将注入uses 属性中定义的所有类
(...)
对于抽象类或装饰器,应使用 setter 注入。
我有以下示例:
@Mapper
public abstract class VehicleMapper {
@Autowired
private CarMapper carMapper;
@Autowired
private BikeMapper bikeMapper;
@Override
public VehicleDTO toDto(final Vehicle source) {
if (source instanceof Car) {
return carMapper.toDto((Car) source);
} else if (source instanceof Bike) {
return bikeMapper.toDto((Bike) source);
} else {
throw new IllegalArgumentException();
}
}
(...)
Run Code Online (Sandbox Code Playgroud)
所以在我的例子中它应该看起来像这样(componentModel在maven中定义):
@Mapper
public abstract class VehicleMapper {
private CarMapper carMapper;
private BikeMapper bikeMapper;
@Autowired
public void setCarMapper(final CarMapper carMapper) …Run Code Online (Sandbox Code Playgroud) 我的所有实体都扩展了一个具有Customer createdByandString createdById字段的基本实体,还有一些具有相同功能的实体。我想Customer在传输数据时排除整个对象。
其他一些实体扩展了另一个实体,该实体本身扩展了基本实体,因此有以下映射器:
@MapperConfig(componentModel = "spring", mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG)
public interface AuditingEntityMapper<Dto, Entity> {
@Mapping(target = "createdBy", source = "createdById")
@Mapping(target = "lastModifiedBy", source = "lastModifiedById")
Dto toDto(Entity entity);
@Mapping(target = "createdBy", ignore = true)
@Mapping(target = "lastModifiedBy", ignore = true)
Entity toEntity(Dto dto);
}
@MapperConfig(componentModel = "spring", mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG)
public interface ManageableEntityMapper<ManageableDtoType extends ManageableDTO, ManageableType extends Manageable> {
ManageableDtoType toDto(ManageableType manageable);
@Mapping(target = "project", ignore = true)
ManageableType toEntity(ManageableDtoType dto);
} …Run Code Online (Sandbox Code Playgroud) 我尝试自动装配我的映射结构映射器:
@Mapper(uses = {
A.class,
B.class,
C.class
})
public interface WindowDtoMapper {
WindowDtoMapper INSTANCE = Mappers.getMapper(WindowDtoMapper.class);
DetailedDto mapToDetailedDto(Window window);
ReadDto mapToReadDto(Window window);
}
Run Code Online (Sandbox Code Playgroud)
这有效:
return WindowDtoMapper.INSTANCE.mapToDetailedDto(window)
Run Code Online (Sandbox Code Playgroud)
但为什么我不能使用:
@RequiredArgsConstructor
public class AAA(){
private final WindowDtoMapper windowDtoMapper;
windowDtoMapper.mapToDetailedDto(window)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
引起原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“pl.comp.window.application.mapper.WindowDtoMapper”类型的合格 bean:预计至少有 1 个符合自动装配候选资格的 bean。依赖注释:{} 位于 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717) 位于 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273)
也许我应该继续使用第一个可行的解决方案?这是不好的解决方案吗?
是否可以从application.properties文件中的字段设置值?
我正在寻找类似的东西
@Mapping(target="version", expression="${application.version}")
StateDto stateToStateDto(State state);
Run Code Online (Sandbox Code Playgroud)
其中application.version=v1来自application.properties文件。
我有一个映射器,对于目标类的特定属性,需要从源对象内的对象列表中选择一个,并使用不同的映射器类来映射它。
简化很多,该类Game包含一个对象列表Transaction,我的GameMapper类如下所示:
@Component
@Mapper(injectionStrategy = InjectionStrategy.CONSTRUCTOR, uses = {TransactionMapper.class, EventMapper.class})
public interface GameMapper {
@Mapping(target = "transaction",
expression = "java(transactionMapper.transactionToDto(findTransactionForPlayer(game, idPlayer)))")
GameResultDto gameToGameResultDto(Game game, Long idPlayer);
// Some more methods
}
Run Code Online (Sandbox Code Playgroud)
问题是,EventMapper被生成为private final EventMapper eventMapper;内部的属性GameMapperImpl,但TransactionMapper不是,因此构建失败,因为 MapStruct 找不到transactionMapper。
我最好的猜测是,这是由于没有显式GameMapper使用其他方法TransactionMapper,因此 Mapstruct 决定不需要它并且不会将其注入到实现中。
那么...有没有办法强制 MapStruct 在子句中包含映射器uses,即使看起来它们没有被使用,或者有任何其他方法来解决这个问题?
我可以在有字符串到枚举映射的地方找到答案,但我找不到如何将枚举映射到字符串。
public class Result {
Value enumValue;
}
public enum Value {
TEST,
NO TEST
}
public class Person {
String value;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能映射这个?
我试过 :
@Mapping(target = "value", source = "enumValue", qualifiedByName = "mapValue")
@Named("mapValue")
default Person mapValue(final Value en) {
return Person.builder().value(en.name()).build();
}
Run Code Online (Sandbox Code Playgroud) 通过双向一对多关联执行从数据传输对象 (DTO) 到Hibernate实体的MapStruct映射的最佳方法是什么?
假设我们有一个与它链接的BookDto类型的多个评论:ReviewDto
public class BookDto {
private List<ReviewDto> reviews;
// getter and setters...
}
Run Code Online (Sandbox Code Playgroud)
相应的 Hibernate 实体与以下对象Book具有一对多关联Review:
@Entity
public class Book {
@OneToMany(mappedBy = "book", orphanRemoval = true, cascade = CascadeType.ALL)
private List<Review> reviews = new ArrayList<>();
public void addReview(Review review) {
this.reviews.add(review);
review.setBook(this);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
@Entity
public class Review {
@ManyToOne(fetch = FetchType.LAZY)
private Book book;
public void setBook(Book book) {
this.book = book;
}
//... …Run Code Online (Sandbox Code Playgroud) // Driver model
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Driver {
private String driverName;
private String licenseNumber;
}
// Car model
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
private String make;
private List<Driver> drivers;
private CarType type;
}
// Car DTO
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CarDto {
private String make;
private Integer totalDrivers;
private String type;
}
@Mapper
public interface CarMapper {
@Mapping(target = "totalDrivers", expression = "java(mapDrivers(car.getDrivers()))")
CarDto mapCarDto(Car car);
default Integer mapDrivers(List<Driver> totalDrivers) {
return totalDrivers.size();
}
@InheritInverseConfiguration …Run Code Online (Sandbox Code Playgroud)