作者:威哥028_438 | 来源:互联网 | 2024-11-30 10:24
使用Orika进行Java Bean属性复制
Orika是一个高效的Java Bean属性复制工具,它利用javassist
在运行时动态生成字段属性映射的字节码,虽然在性能上略逊于cglib的BeanCopier
,但在灵活性和易用性方面表现出色。
GitHub地址:https://github.com/orika-mapper/orika
Maven依赖
要在项目中使用Orika,需要在pom.xml文件中添加以下依赖:
ma.glasnost.orika
orika-core
1.5.4
Java使用示例
为了更好地演示Orika的功能,我们创建了一些测试用例,这些用例展示了Orika的基本用法以及如何处理不同字段名和类型转换的情况。
org.projectlombok
lombok
1.18.22
org.junit.jupiter
junit-jupiter
5.8.1
com.google.guava
guava
29.0-jre
以下是具体的Java代码示例:
package bean;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.FieldNameConstants;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.converter.builtin.DateToStringConverter;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.junit.jupiter.api.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* Orika演示测试类
*/
class OrikaDemoTest {
@Test
void testMap() {
DefaultMapperFactory mapperFactory = getMapperFactory();
MapperFacade facade = mapperFactory.getMapperFacade();
SourceClass source = getSourceClass();
// 基本使用
TargetClass target = facade.map(source, TargetClass.class);
compare(source, target);
assertNotEquals(source.getEmail(), target.getMyEmail());
}
@Test
void testMapAsList() {
DefaultMapperFactory mapperFactory = getMapperFactory();
MapperFacade facade = mapperFactory.getMapperFacade();
SourceClass source = getSourceClass();
// 集合映射
List target = facade.mapAsList(Lists.newArrayList(source), TargetClass.class);
compare(source, target.get(0));
assertNotEquals(source.getEmail(), target.get(0).getMyEmail());
}
@Test
void testDiffFieldName() {
DefaultMapperFactory mapperFactory = getMapperFactory();
// 设置不同字段名双向映射
mapperFactory.classMap(SourceClass.class, TargetClass.class)
.field(SourceClass.Fields.email, TargetClass.Fields.myEmail)
.byDefault()
.register();
MapperFacade facade = mapperFactory.getMapperFacade();
SourceClass source = getSourceClass();
TargetClass target = facade.map(source, TargetClass.class);
compare(source, target);
assertEquals(source.getEmail(), target.getMyEmail());
}
@Test
void testConverter() throws ParseException {
String format = "yyyy-MM-dd";
DefaultMapperFactory mapperFactory = getMapperFactory();
// 设置类型转换
ConverterFactory cOnverterFactory= mapperFactory.getConverterFactory();
converterFactory.registerConverter(new DateToStringConverter(format));
String birth = "2021-11-21";
Date date = new SimpleDateFormat(format).parse(birth);
SourceClass source = getSourceClass()
.setBirth(date);
MapperFacade facade = mapperFactory.getMapperFacade();
TargetClass target = facade.map(source, TargetClass.class);
compare(source, target);
assertEquals(birth, target.getBirth());
}
private DefaultMapperFactory getMapperFactory() {
return new DefaultMapperFactory.Builder().build();
}
private void compare(SourceClass source, TargetClass target) {
assertEquals(source.getId(), target.getId());
assertEquals(source.getName(), target.getName());
assertEquals(source.getAge(), target.getAge());
}
private SourceClass getSourceClass() {
return SourceClass.builder()
.id(1L)
.name("test")
.age(18)
.email("email@xxx")
.build();
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
@Accessors(chain = true)
static class SourceClass {
private Long id;
private String name;
private Integer age;
private String email;
private Date birth;
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
static class TargetClass {
private Long id;
private String name;
private int age;
private String myEmail;
private String birth;
}
}