//源对象
@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);
}
//结果
使用场景
目标对象需要对源对象的字段做一些简单处理,类似金额相加等等。