Bean Copy
Bean Copy 是基于"注解+注解处理器"实现的提供和手写代码相同的性能,极大的简化代码,覆盖各种场景的功能。
快速开始
MDP 项目默认引入依赖
使用要求
- JDK8及以上。
- MDP 开发框架,版本 >= 1.5.1.RC7。
1 自动映射
1.1 同名同类型自动映射
1.1.1 基本类型自动映射
基本类型 byte、short、int、long、float 、double、 boolean、char 可实现自动映射,且自动实现拆箱装箱
@Data
public class Source {
private byte byteValue;
private short shortValue;
private int intValue;
private long longValue;
private float floatValue;
private double doubleValue;
private boolean booleanValue;
private char charValue;
private String string;
}
@Data
public class Target {
private byte byteValue;
private short shortValue;
private int intValue;
private long longValue;
private float floatValue;
private double doubleValue;
private boolean booleanValue;
private char charValue;
private String string;
}
@MdpBeanCopy
public interface SimpleBeanCopy {
Target sourceToTarget(Source source);
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationLoader.class)
public class SimpleBeanCopyTest {
@Autowired
private SimpleBeanCopy simpleBeanCopy;
@Test
public void testSourceToTarget() {
Source source = createSource();
Target target = SimpleBeanCopy.INSTANCE.sourceToTarget(source);
assertTarget(target);
}
private Source createSource() {
Source source = new Source();
source.setByteValue(Byte.MAX_VALUE);
source.setShortValue(Short.MAX_VALUE);
source.setIntValue(Integer.MAX_VALUE);
source.setLongValue(Long.MAX_VALUE);
source.setFloatValue(Float.MAX_VALUE);
source.setDoubleValue(Double.MAX_VALUE);
source.setBooleanValue(true);
source.setCharValue(Character.MAX_VALUE);
source.setString("hello world~");
return source;
}
private void assertTarget(Target target) {
assertThat(target.getByteValue()).isEqualTo(Byte.MAX_VALUE);
assertThat(target.getShortValue()).isEqualTo(Short.MAX_VALUE);
assertThat(target.getIntValue()).isEqualTo(Integer.MAX_VALUE);
assertThat(target.getLongValue()).isEqualTo(Long.MAX_VALUE);
assertThat(target.getFloatValue()).isEqualTo(Float.MAX_VALUE);
assertThat(target.getDoubleValue()).isEqualTo(Double.MAX_VALUE);
assertThat(target.isBooleanValue()).isEqualTo(true);
assertThat(target.getCharValue()).isEqualTo(Character.MAX_VALUE);
assertThat(target.getString()).isEqualTo("hello world~");
}
}
网友评论