Spring Boot 集成 Orika 对象映射框架完整指南

📅 2026/8/14 20:42:40
Spring Boot 集成 Orika 对象映射框架完整指南
1. 概述Orika 是一个高效、轻量级的 Java Bean 映射框架能够自动处理对象之间的属性复制和转换。本文将详细介绍如何在 Spring Boot 项目中集成和使用 Orika。2. 环境准备2.1 导入依赖在项目的 pom.xml 文件中添加 Orika 依赖dependency groupIdma.glasnost.orika/groupId artifactIdorika-core/artifactId version1.5.2/version /dependency3. Spring Boot 配置3.1 创建 MapperFactory 配置类编写容器注入的类将 Orika 的 MapperFactory 配置为 Spring Beanpackage com.kingboy.springboot.config; import ma.glasnost.orika.MapperFactory; import ma.glasnost.orika.impl.DefaultMapperFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class MapperFacotoryAutoWire { Bean public MapperFactory getFactory(){ return new DefaultMapperFactory.Builder().build(); } }import ma.glasnost.orika.MapperFactory; import ma.glasnost.orika.impl.DefaultMapperFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class OrikaConfig { Bean public MapperFactory mapperFactory() { MapperFactory factory new DefaultMapperFactory.Builder().build(); // 全局注册映射规则 factory.classMap(Person.class, Student.class) .field(dateTime, birth) // 映射不同名的字段 .byDefault() // 其他同名字段自动映射 .register(); return factory; } }4. 使用示例4.1 单元测试示例以下是一个完整的单元测试示例展示如何使用 Orika 进行对象映射import ma.glasnost.orika.MapperFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.Date; import java.util.List; import static org.junit.Assert.*; RunWith(SpringRunner.class) SpringBootTest(classes KingBoyApplication.class, webEnvironment SpringBootTest.WebEnvironment.RANDOM_PORT) public class MapperFactoryTest { Autowired private MapperFactory mapperFactory; /** * 测试单个对象映射 */ Test public void copyBeanToBean() { // 1. 准备数据 Person person new Person(king, 123, new Date()); // 2. 执行映射 (无需再次注册配置类已处理) Student student mapperFactory.getMapperFacade().map(person, Student.class); // 3. 验证结果 assertNotNull(student); assertEquals(king, student.getName()); assertEquals(Integer.valueOf(123), student.getAge()); assertEquals(person.getDateTime(), student.getBirth()); System.out.println(Single Object Mapping: student); } /** * 测试列表映射 */ Test public void copyListToList() { // 1. 准备数据 ListPerson personList getPersonList(); // 2. 执行列表映射 ListStudent students mapperFactory.getMapperFacade().mapAsList(personList, Student.class); // 3. 验证结果 assertNotNull(students); assertEquals(personList.size(), students.size()); // 简单校验第一个元素 Student firstStudent students.get(0); assertEquals(lucy1, firstStudent.getName()); assertEquals(Integer.valueOf(1), firstStudent.getAge()); // 打印结果 students.forEach(System.out::println); } private ListPerson getPersonList() { ListPerson list new ArrayList(5); Date now new Date(); for (int i 1; i 5; i) { list.add(new Person(lucy i, i, now)); } return list; } }5. 实体类定义5.1 Person 类源对象 Person 类的定义package com.kingboy.springboot.domain; import java.time.LocalDateTime; import java.util.Date; public class Person { public Person() { } public Person(String name, Integer age, Date dateTime) { this.name name; this.age age; this.dateTime dateTime; } private String name; private Integer age; private Date dateTime; public String getName() { return name; } public Person setName(String name) { this.name name; return this; } public Integer getAge() { return age; } public Person setAge(Integer age) { this.age age; return this; } public Date getDateTime() { return dateTime; } public Person setDateTime(Date dateTime) { this.dateTime dateTime; return this; } Override public String toString() { return Person{ name name , age age , dateTime dateTime }; } }5.2 Student 类目标对象 Student 类的定义package com.kingboy.springboot.domain; import java.time.LocalDateTime; import java.util.Date; public class Student { private String name; private String grade; private Integer age; private Date birth; public Date getBirth() { return birth; } public Student setBirth(Date birth) { this.birth birth; return this; } public String getName() { return name; } public Student setName(String name) { this.name name; return this; } public String getGrade() { return grade; } public Student setGrade(String grade) { this.grade grade; return this; } public Integer getAge() { return age; } public Student setAge(Integer age) { this.age age; return this; } Override public String toString() { return Student{ name name , grade grade , age age , birth birth }; } }6. 总结通过以上步骤我们成功在 Spring Boot 项目中集成了 Orika 对象映射框架。Orika 提供了灵活的字段映射配置支持单个对象和集合对象的映射能够大大简化 Java Bean 之间的属性复制工作。