踩坑建议
Configures the mapping between two bean types. Either resultType(), qualifiedBy() or nullValueMappingStrategy() must be specified. Example: Determining the result type // When result types have an inheritance relation, selecting either mapping method Mapping or factory method // BeanMapping can be become ambiguous. Parameter resultType() can be used. public class FruitFactory { public Apple createApple() { return new Apple(); } public Orange createOrange() { return new Orange(); } } @Mapper(uses = FruitFactory.class) public interface FruitMapper { @BeanMapping(resultType = Apple.class) Fruit toFruit(FruitDto fruitDto); }
// generates public class FruitMapperImpl implements FruitMapper { @Override public Fruit toFruit(FruitDto fruitDto) { Apple fruit = fruitFactory.createApple(); // … } }
<a name="TXcLN"></a>### @AfterMapping```javaMarks a method to be invoked at the end of a generated mapping method, right before the last return statement of the mapping method. The method can be implemented in an abstract mapper class, be declared in a type (class or interface) referenced in Mapper.uses(), or in a type used as @Context parameter in order to be used in a mapping method.The method invocation is only generated if the return type of the method (if non-void) is assignable to the return type of the mapping method and all parameters can be assigned by the available source, target or context parameters of the mapping method:A parameter annotated with @MappingTarget is populated with the target instance of the mapping.A parameter annotated with @TargetType is populated with the target type of the mapping.Parameters annotated with @Context are populated with the context parameters of the mapping method.Any other parameter is populated with a source parameter of the mapping.For non-void methods, the return value of the method invocation is returned as the result of the mapping method if it is not null.All after-mapping methods that can be applied to a mapping method will be used. @Qualifier / @Named can be used to filter the methods to use.The order of the method invocation is determined by their location of definition:Methods declared on @Context parameters, ordered by the parameter order.Methods implemented in the mapper itself.Methods from types referenced in Mapper.uses(), in the order of the type declaration in the annotation.Methods declared in one type are used after methods declared in their super-typeImportant: the order of methods declared within one type can not be guaranteed, as it depends on the compiler and the processing environment implementation.Example:@AfterMappingpublic void calledWithoutArgs() {// ...}@AfterMappingpublic void calledWithSourceAndTargetType(SourceEntity anySource, @TargetType Class<?> targetType) {// ...}@AfterMappingpublic void calledWithSourceAndTarget(Object anySource, @MappingTarget TargetDto target) {// ...}public abstract TargetDto toTargetDto(SourceEntity source);// generates:public TargetDto toTargetDto(SourceEntity source) {if ( source == null ) {return null;}TargetDto targetDto = new TargetDto();// actual mapping codecalledWithoutArgs();calledWithSourceAndTargetType( source, TargetDto.class );calledWithSourceAndTarget( source, targetDto );return targetDto;}See Also:BeforeMapping, ContextAuthor:Andreas Gudian
@Context // 数据共享
@Mapper(componentModel="spring")public interface MyMapper {@Mapping(target="x",ignore = true)// other mappingsTarget map( Source source, @Context MyService service);@AfterMappingdefault void map( @MappingTarget Target.X target, Source.ID source, @Context MyService service) {target.set( service.findById( source.getId() ) );}}
参考
概述
- DAO 与Service 层都是 返回 DO类型,与数据库表对应
- controller 层 使用 mapstruct 对 转DO 类型到 VO 类型,分两种
- DO VO 一一对应,
- 多个 DO 聚合 转成一个VO
- 例子
```java
//controller
public CommonResult
getPermissionInfo() { // 获得用户信息 SysUserDO user = userCoreService.getUser(getLoginUserId()); if (user == null) {
} // 获得角色列表 Listreturn null;
roleList = roleService.getRolesFromCache(getLoginUserRoleIds()); // 获得菜单列表 List menuList = permissionService.getRoleMenusFromCache(
// 拼接结果返回 return success(SysAuthConvert.INSTANCE.convert(user, roleList, menuList)); }getLoginUserRoleIds(), // 注意,基于登录的角色,因为后续的权限判断也是基于它SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType(), MenuTypeEnum.BUTTON.getType()),SetUtils.asSet(CommonStatusEnum.ENABLE.getStatus()));
// convert
//…
default SysAuthPermissionInfoRespVO convert(SysUserDO user, List
<a name="WIJtn"></a>### 使用@aftermapping进行MapStruct批量转换```javapublic abstract CatUI convert(Cat cat);public abstract List<CatUI> convert(List<Cat> cats);@AfterMappingpublic void populateCatName(Cat cat, @MappingTarget CatUI catUI) {String name = _someRemoteService.getCatName(catUI.getId());catUI.setName(name);}@AfterMappingpublic void populateCatNames(List<Cat> cats, @MappingTarget List<CatUI> catUIs) {Map<Integer,String> idToNameMap = _someRemoteService.getCatNames(catUIs.stream().map((c) -> c.getId() ).collect(Collectors.toList());catUIs.forEach((c) -> c.setName(idToNameMap(c.getId())));}
