MapStruct

参考文章

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 = &quot;spring&quot;)
public interface UserRoleMapper {

 @Mappings({
 @Mapping(source = &quot;id&quot;, target = &quot;userId&quot;),
@Mapping(source = &quot;username&quot;, target = &quot;name&quot;),
@Mapping(source = &quot;role.roleName&quot;, target = &quot;roleName&quot;)
 })
 UserRoleDto toUserRoleDto(User user);
}

@Mapper:

修饰接口,表明该接口为映射处理类。被修饰的类会自动生成实现类。实现类自行查看编译后文件。

componentModel:

指定组件模型,只能有特定的值。具体取值查看该属性注释。SpringBoot项目一般设置为@Mapper(componentModel = "spring"),个人更喜欢使用默认方式。

默认方式:
@Mapper

public interface UserRoleMapper {

UserRoleMapper instance = Mappers.getMapper(UserRoleMapper.class);

 @Mappings({
 @Mapping(source = &quot;id&quot;, target = &quot;userId&quot;),
 @Mapping(source = &quot;username&quot;, target = &quot;name&quot;),
 @Mapping(source = &quot;role.roleName&quot;, target = &quot;roleName&quot;)
 })
 UserRoleDto toUserRoleDto(User user);
}

外部调用:UserRoleMapper userMapper = UserRoleMapper.instance;

Spring模式:
@Mapper(componentModel = &quot;spring&quot;)
public interface UserRoleMapper {

@Mappings({
 @Mapping(source = &quot;id&quot;, target = &quot;userId&quot;),
 @Mapping(source = &quot;username&quot;, target = &quot;name&quot;),
 @Mapping(source = &quot;role.roleName&quot;, target = &quot;roleName&quot;)
 })
 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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值