pom依赖:
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.1.Final</version>
</dependency>
Mapper接口:
@Mapper(componentModel = "spring")
public interface UserRoleMapper {
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
UserRoleDto toUserRoleDto(User user);
}
@Mapper:
修饰接口,表明该接口为映射处理类。被修饰的类会自动生成实现类。实现类自行查看编译后文件。
componentModel:
指定组件模型,只能有特定的值。具体取值查看该属性注释。SpringBoot项目一般设置为@Mapper(componentModel = "spring"),个人更喜欢使用默认方式。
默认方式:
@Mapper
public interface UserRoleMapper {
UserRoleMapper instance = Mappers.getMapper(UserRoleMapper.class);
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
UserRoleDto toUserRoleDto(User user);
}
外部调用:UserRoleMapper userMapper = UserRoleMapper.instance;
Spring模式:
@Mapper(componentModel = "spring")
public interface UserRoleMapper {
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
UserRoleDto toUserRoleDto(User user);
}
外部调用:
@Autowire
Private UserRolemapper userRoleMapper;
@Mappings:
修饰方法,包含多个@Mapping注解,指定参数映射规则。
@Mapping:
Source:
源对象属性。
defaultValue :
使用默认值设置目标对象属性。
@Mapping(defaultValue = "CNY", target = "currencyCode")
Target:
目标对象属性。
ignore :
忽略目标属性映射。
@Mapping(target = "email", ignore = true)
dateFormat :
如果目标对象为日期,可以通过该属性指定日期转换格式。
@Mapping(source = "customer.birthday", target = "birthDateFormat", dateFormat = "yyyy-MM-dd HH:mm:ss")
单个对象-》单个对象:
@Mapper(componentModel = "spring")
public interface UserRoleMapper {
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
UserRoleDto toUserRoleDto(User user);
}
多个对象-》单个对象:
@Mappings({
// 把user中的id绑定到目标对象的userId属性中
@Mapping(source = "user.id", target = "userId"),
// 把user中的username绑定到目标对象的name属性中
@Mapping(source = "user.username", target = "name"),
// 把role对象的roleName属性值绑定到目标对象的roleName中
@Mapping(source = "role.roleName", target = "roleName")
})
UserRoleDto toUserRoleDto(User user, Role role);
参数-》实体:
@Mappings({
// 把user中的id绑定到目标对象的userId属性中
@Mapping(source = "user.id", target = "userId"),
// 把user中的username绑定到目标对象的name属性中
@Mapping(source = "user.username", target = "name"),
// 把role对象的roleName属性值绑定到目标对象的roleName中
@Mapping(source = "myRoleName", target = "roleName")
})
UserRoleDto useParameter(User user, String myRoleName);
更新:
@Mappings({
@Mapping(source = "userId", target = "id"),
@Mapping(source = "name", target = "username"),
@Mapping(source = "roleName", target = "role.roleName")
})
void updateDto(UserRoleDto userRoleDto, @MappingTarget User user);
自定义转换器:
@Component
public class BooleanStrFormat {
public String toStr(Boolean isDisable) {
if (isDisable) {
return "Y";
} else {
return "N";
}
}
public Boolean toBoolean(String str) {
if (str.equals("Y")) {
return true;
} else {
return false;
}
}
}
@Mapper(componentModel = "spring", uses = { BooleanStrFormat.class})
public interface CustomerListMapper {
@Mappings({
@Mapping(source = "name", target = "customerName"),
@Mapping(source = "isDisable", target = "disable")
})
CustomerDto customersToCustomerDto(Customer customer);
}
List映射:
@Mapper
public interface CustomerListMapper {
@Mappings({
@Mapping(source = "name", target = "customerName"),
@Mapping(source = "isDisable", target = "disable")
})
CustomerDto customersToCustomerDto(Customer customer);
}