美文网首页
java表达式的嵌入使用

java表达式的嵌入使用

作者: 波波维 | 来源:发表于2018-03-14 10:54 被阅读0次
//源对象
@Data
public class EntityH implements Serializable{
    private Integer valH;
}
//目标对象
@Data
public class EntityI implements Serializable{
    private Integer valH;
}
//转换方法
@Mapper
public interface ExpressionMapper {
    ExpressionMapper INSTANCE = Mappers.getMapper(ExpressionMapper.class);
    @Mappings({
            @Mapping(target = "valH", expression = "java(entityH.getValH() + 1 + (int)(Math.random()*5))"),
    })
    EntityI entityGtoI(EntityH entityH);
}
//调用方法
@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) throws ParseException {
      entityH.setValH(5);
      entityI = ExpressionMapper.INSTANCE.entityGtoI(entityH);
      System.out.println(entityI.toString());
      SpringApplication.run(DemoApplication.class, args);
  }

//结果


image.png

使用场景
目标对象需要对源对象的字段做一些简单处理,类似金额相加等等。

相关文章

网友评论

      本文标题:java表达式的嵌入使用

      本文链接:https://www.haomeiwen.com/subject/ocoxqftx.html