Orika 对象复制 克隆
引入依赖
<dependency> <groupId>ma.glasnost.orika</groupId> <artifactId>orika-core</artifactId> <version>1.5.4</version> </dependency>
构造一个MapperFactory
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
注册字段映射
mapperFactory.classMap(SourceClass.class, TargetClass.class) .field("firstName", "givenName") .field("lastName", "sirName") .byDefault() .register();
当字段名在两个实体不一致时可以通过 .field()
方法进行映射,如果字段名都一样则可省略,byDefault()
方法用于注册名称相同的属性,如果不希望某个字段参与映射,可以使用 exclude
方法。
进行映射
MapperFacade mapper = mapperFactory.getMapperFacade(); SourceClass source = new SourceClass(); // set some field values ... // map the fields of 'source' onto a new instance of PersonDest TargetClass target = mapper.map(source, TargetClass.class);
示例:
实体映射
// 实体转换 Student student = new Student("1", "name1", "email1"); // 构造一个MapperFactory MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); // 注册字段映射 // 当字段名在两个实体不一致时可以通过 .field()方法进行映射,如果字段名都一样则可省略,byDefault()方法用于注册名称相同的属性,如果不希望某个字段参与映射,可以使用 exclude方法。 mapperFactory.classMap(Student.class, Teacher.class) .field("email", "emailAddress") .byDefault() .register(); // 进行映射 MapperFacade mapper = mapperFactory.getMapperFacade(); Teacher teacher = mapper.map(student, Teacher.class); log.info("teacher:" + teacher);//teacher:Teacher(id=1, name=name1, emailAddress=email1)
集合转换
// 集合转换 Student student1 = new Student("1", "name1", "email1"); Student student2 = new Student("2", "name2", "email2"); List<Student> studentList = Lists.newArrayList(student1, student2); // 构造一个MapperFactory MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); // 注册字段映射 // 当字段名在两个实体不一致时可以通过 .field()方法进行映射,如果字段名都一样则可省略,byDefault()方法用于注册名称相同的属性,如果不希望某个字段参与映射,可以使用 exclude方法。 mapperFactory.classMap(Student.class, Teacher.class) .field("email", "emailAddress") .byDefault() .register(); // 进行映射 MapperFacade mapper = mapperFactory.getMapperFacade(); List<Teacher> teacherList = mapper.mapAsList(studentList, Teacher.class); log.info("teacherList:" + teacherList);// teacherList:[Teacher(id=1, name=name1, emailAddress=email1), Teacher(id=2, name=name2, emailAddress=email2)]
集合映射实体
// 集合映射实体 Person person = new Person(); person.setNameParts(Lists.newArrayList("1", "name1", "email1")); // 构造一个MapperFactory MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); // 注册字段映射 // 当字段名在两个实体不一致时可以通过 .field()方法进行映射,如果字段名都一样则可省略,byDefault()方法用于注册名称相同的属性,如果不希望某个字段参与映射,可以使用 exclude方法。 mapperFactory.classMap(Person.class, Student.class) .field("nameParts[0]", "id") .field("nameParts[1]", "name") .field("nameParts[2]", "email") .byDefault() .register(); // 进行映射 MapperFacade mapper = mapperFactory.getMapperFacade(); Student student = mapper.map(person, Student.class); log.info("student:" + student);// student:Student(id=1, name=name1, email=email1)
类类型映射
// 类类型映射 BasicPerson basicPerson = new BasicPerson(); Student student = new Student("1", "name1", "email1"); basicPerson.setStudent(student); // 构造一个MapperFactory MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); // 注册字段映射 // 当字段名在两个实体不一致时可以通过 .field()方法进行映射,如果字段名都一样则可省略,byDefault()方法用于注册名称相同的属性,如果不希望某个字段参与映射,可以使用 exclude方法。 mapperFactory.classMap(BasicPerson.class, Teacher.class) .field("student.id", "id") .field("student.name", "name") .field("student.email", "emailAddress") .byDefault() .register(); // 进行映射 MapperFacade mapper = mapperFactory.getMapperFacade(); Teacher teacher = mapper.map(basicPerson, Teacher.class); log.info("teacher:" + teacher);// teacher:Teacher(id=1, name=name1, emailAddress=email1)
多重映射
// 多重映射 Student student1 = new Student("1", "name1", "email1"); Student student2 = new Student("2", "name2", "email2"); List<Student> studentList = Lists.newArrayList(student1, student2); StudentGrade studentGrade = new StudentGrade(); studentGrade.setStudentGradeName("gradename1"); studentGrade.setStudentList(studentList); // 构造一个MapperFactory MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); // 注册字段映射 // 当字段名在两个实体不一致时可以通过 .field()方法进行映射,如果字段名都一样则可省略,byDefault()方法用于注册名称相同的属性,如果不希望某个字段参与映射,可以使用 exclude方法。 mapperFactory.classMap(StudentGrade.class, TeacherGrade.class) .field("studentGradeName", "teacherGradeName") .field("studentList", "teacherList") .byDefault() .register(); mapperFactory.classMap(Student.class, Teacher.class) .field("email", "emailAddress") .byDefault() .register(); // 进行映射 MapperFacade mapper = mapperFactory.getMapperFacade(); TeacherGrade teacherGrade = mapper.map(studentGrade, TeacherGrade.class); log.info("teacherGrade:" + teacherGrade);// teacherGrade:TeacherGrade(teacherGradeName=gradename1, teacherList=[Teacher(id=1, name=name1, emailAddress=email1), Teacher(id=2, name=name2, emailAddress=email2)])
附:
@Data @AllArgsConstructor @NoArgsConstructor public class Student { private String id; private String name; private String email; } @Data @AllArgsConstructor @NoArgsConstructor public class Teacher { private String id; private String name; private String emailAddress; } @Data public class Person { private List<String> nameParts; } @Data public class BasicPerson { private Student student; } @Data public class StudentGrade { private String studentGradeName; private List<Student> studentList; } @Data public class TeacherGrade { private String teacherGradeName; private List<Teacher> teacherList; }
参考文章:http://javadaily.cn/articles/2021/09/15/1631687008282.html