实体类之间的转换

        为了实现代码的规范,降低代码中的get set方法的重复出现,未来便于代码维护,引入实体转换接口为Convert,假设是一个用户中存在的各种实体类型转化,设置其接口为UserConvert。

@Mapper
public interface UserConvert {
    UserConvert MAPPER = Mappers.getMapper(UserConvert.class);

    UserModel createReqToModel(CreateUserReq req);

    UserModel editReqToModel(EditUserReq req);

}

        当导入了一下代码之后,框架会自动将两个实体中对应的属性一一赋值过去其中假设UserModel:

@Data
@TableName("sys_user")
public class UserModel implements Serializable {
    @TableId
    private Long userId;
    private String userName;
    private Integer userGender;
    private String userCard;
    private String userPhone;
}

        CreateUserReq为:


@Data
@ApiModel("用户创建入参")
public class CreateUserReq implements Serializable {
    @ApiModelProperty("员工姓名")
    @NotNull(message = "缺少员工姓名")
    private String userName;

    @ApiModelProperty("员工性别")
    @NotNull(message = "缺少员工性别")
    private Integer userGender;

    @ApiModelProperty("身份证号")
    @NotNull(message = "缺少身份证号")
    private String userCard;

    @ApiModelProperty("手机号")
    @NotNull(message = "缺少手机号")
    private String userPhone;

}

        其中调用为:

//其中req的类型为CreateUserReq
UserModel userModel = UserConvert.MAPPER.createReqToModel(req);

        以上所述的是当两边的属性的类型和名称都是一致的时候可以直接这样转化。减少了书写get set自己赋值的重复步骤。

        假设其中有对应的属性不一致那么就会导致两边映射不上去。无论是名字还是属性不一致都不会自动映射上去。

        

        假设,存在这样一个样例。我再外部导入数据的时候,导入商品价格表。表中的价格属性是Double元为单位的两位小数,但是我在后端数据库中需要存储的是分为单位的Integer属性。在转换过程中就存在。

        ShopGoodsPriceModel:

@TableName("t_shop_goods_price")
@Data
public class ShopGoodsPriceModel implements Serializable {
    @TableId
    private Long goodsPriceId;
    private Long shopId;
    private Integer code;
    private String goodsName;
    private String goodsCode;
    private Integer goodsPrice;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    private String createUser;
    private String updateUser;
    private Integer isDeleted;
}

        ShopGoodsPriceImportDTO:

@Data
@ApiModel("门店商品价格批量导入")
public class ShopGoodsPriceImportDTO {
    @ApiModelProperty("商品价格ID")
    private Long goodsPriceId;

    @ExcelProperty("门店ID")
    @ApiModelProperty("门店ID")
    private Long shopId;

    @ExcelProperty("门店codes")
    @ApiModelProperty("门店codes")
    private Integer codes;

    @ExcelProperty("商品名称")
    @ApiModelProperty("商品名称")
    private String goodsName;

    @ExcelProperty("商品条码")
    @ApiModelProperty("商品条码")
    private String goodsCode;

    @ExcelProperty("商品价格")
    @ApiModelProperty("商品价格")
    private Double goodsPrice;

    //是否为错误数据 1:是 0:否
    private Integer isFail = 0;

    @ExcelProperty("失败原因")
    @ApiModelProperty("失败原因")
    private String failReason;
}

        如果直接使用转化就会实现精度丢失。如果传入0.9,存入后端的就会是0;

@Mapper
public interface ShopConvert {
    ShopConvert MAPPER = Mappers.getMapper(ShopConvert.class);

    @Mapping(source = "code",target = "codes")
    @Mapping(target = "goodsPrice", expression = "java(shopGoodsPriceImportDTO.getGoodsPrice() != null ? (int)(shopGoodsPriceImportDTO.getGoodsPrice() * 100D) : null)")
    ShopGoodsPriceModel shopGoodsPriceImportDTOToModel(ShopGoodsPriceImportDTO shopGoodsPriceImportDTO);

}

        由于在转换过程中需要将元转化为分,因此就需要使用、

expression = "java(shopGoodsPriceImportDTO.getGoodsPrice() != null ? (int)(shopGoodsPriceImportDTO.getGoodsPrice() * 100D) : null)"

将他转换为分之后,然后再将double类型转换为int类型即可。他会在编译过程中执行转化。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用Elasticsearch的Mapper映射实体类时,时间问题涉及到对日期和时间数据类型的处理。在Elasticsearch中,日期和时间可以使用多种格式进行存储和检索。 首先,我们需要在实体类的属性上使用合适的注解来标识该属性是一个日期或时间。对于日期类型,可以使用`@Field`注解,并指定字段类型为`FieldType.Date`,这样可以将日期通过指定的格式进行存储和检索。对于时间类型,可以使用`@Field`注解,并指定字段类型为`FieldType.Text`,同时也可以使用`format`属性来指定时间的格式。 在映射的过程中,我们可以选择使用Elasticsearch提供的默认日期时间格式,如`"yyyy-MM-dd HH:mm:ss"`,也可以根据自己的需求定义对应的格式。需要注意的是,由于日期和时间的校验和转换可能会带来性能上的影响,所以在选择日期时间格式时需要权衡性能和需求。 当对日期和时间进行检索时,可以使用相应的格式将输入的日期时间字符串转换为Elasticsearch所支持的格式,并进行检索。一般情况下,Elasticsearch会将日期和时间类型进行忽略,只比较其毫秒级的数值,所以在检索时需要将输入的日期和时间精确到毫秒级。 总之,ES Mapper映射实体类时间问题涉及到对日期和时间的存储、检索和格式转换。通过合适的注解和格式设置,我们可以方便地处理实体类中的日期和时间属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值