在我的dto课堂上:
private String password;
在我的模型班上:
private byte[] password;
我想使用mapStruct将String转换为byte []。有人可以帮忙
提前感谢。
最好是提供一种在String
和byte[]
之间映射的默认方法。
例如:
@Mapper
public MyMapper {
Model fromDto(Dto dto);
default byte[] toBytes(String string) {
return string != null ? string.getBytes() : null;
}
}
使用此方法,MapStruct将自动为Dto
和Model
之间的所有其他字段执行操作,并将String
和byte[]
之间的映射保留为toBytes
方法。
假设您有此类。
public class Source {
private String password;
//getters and setters
}
public class Destination {
private byte[] password;
//getters and setters
}
您可以创建自定义映射器。
@Mapper
public abstract class MyMapper {
public Destination sourceToDest(Source source) {
Destination dest = new Destination();
dest.setPassword(source.getPassword().getBytes());
return dest;
}
}
然后
MyMapperImpl mapper = new MyMapperImpl();
Destination dest = mapper.sourceToDest(source);