当前位置: 首页 > 工具软件 > Orika > 使用案例 >

Orika快速入门

储阳曦
2023-12-01

Orika作用:进行对象之间的转换

基本使用

  • 引入依赖

java <dependency> <groupId>ma.glasnost.orika</groupId> <artifactId>orika-core</artifactId> <version>1.4.6</version> </dependency>

  • 定义对象

```java public class User{ private String name; private Integer age; }

public class CopyUserBase { private String name; private Integer age; }

public class CopyUser { private String name; private Integer copyAge; } ```

对象拷贝 属性名相同

```java User user = new User("beyondLi", 18);

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

CopyUserBase copyUserBase = mapperFactory.getMapperFacade().map(user,CopyUserBase.class); ```

对象拷贝 属性名不同

```java User user = new User("beyondLi", 18);

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

mapperFactory.classMap(User.class,CopyUser.class).field("age","copyAge").byDefault() .register(); CopyUser copyUser = mapperFactory.getMapperFacade().map(user, CopyUser.class); ```

集合拷贝 属性名不同 List<User> ==> List<CopyUser>

java //进行属性名匹配 mapperFactory.classMap(User.class, CopyUser.class).field("age", "copyAge").byDefault().register(); //进行集合复制 List<CopyUser> copyUsers = mapperFactory.getMapperFacade().mapAsList(userList, CopyUser.class);

对象中包含集合,但集合中对象的属性名称不同

```java public class Customer { private List userList; }

public class CustomerCopy { private List userList; } ```

```java mapperFactory.classMap(User.class, CopyUser.class).field("age","copyAge").byDefault().register();

CustomerCopy customerCopy = mapperFactory.getMapperFacade().map(customer, CustomerCopy.class); ```

嵌套映射

java public class A { private String name; private Integer age; private User user; // User(id,name,myCollege) } public class B { private Info info; // Info(name,age) private UserVo userVo; // UserVo(id,name,school) }

```java A a = new A("ccb", 23, new User("1", "ccb", "tust"));

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); mapperFactory.classMap(A.class,B.class).field("user","userVo") .field("user.myCollege","userVo.school") .field("name","info.name").field("age","info.age") .byDefault().register();

B b = mapperFactory.getMapperFacade().map(a, B.class); ```

排除属性

```java public class A { private String myName; private Integer age; }

public class B { private String name; private Integer age; } ```

```java MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

// 要排除的"属性名"在两个类中不能一样,否则也会映射 mapperFactory.classMap(A.class,B.class).exclude("myName").byDefault().register();

A a = new A("ccb", 23); B map = mapperFactory.getMapperFacade().map(a, B.class);

System.out.println(map); // B(name=null, age=23) ```

空值映射

默认情况下,源对象字段值为null,映射后目标对象对应字段值将被覆盖为null

局部配置,会影响后续字段

```java MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); mapperFactory.classMap(A.class,B.class) .field("myName","name") .mapNulls(false).byDefault().register();

A a = new A("ccb", null,null); B b = new B("easyccb", 22,"tust");

mapperFactory.getMapperFacade().map(a, b);

System.out.println(a); // A(name=ccb, age=null, school=null) System.out.println(b); // B(name=ccb, age=22, school=tust) ```

字段级别控制

```java MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); mapperFactory.classMap(A.class,B.class) .field("myName","name") .fieldMap("age","age").mapNulls(false).add() // 只影响age字段映射, .byDefault().register();

A a = new A("ccb", null,null); B b = new B("easyccb", 22,"tust"); // ccb 22 null

mapperFactory.getMapperFacade().map(a, b);

System.out.println(a); // A(myName=ccb, age=null, school=null) System.out.println(b); // B(name=ccb, age=22, school=null) ```

自定义转换器

  • 定义转换器

```java public class MyConverter extends BidirectionalConverter {

public MyDate convertTo(Date source, Type destinationType) { // convert in one direction }

public Date convertFrom(MyDate source, Type destinationType) { // convert in the other direction } } ```

  • 注册字段级别转换器

```java MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

ConverterFactory converterFactory = mapperFactory.getConverterFactory(); converterFactory.registerConverter("myConverterIdValue", new MyConverter()); ```

  • 使用

java mapperFactory.classMap( Source.class, Destination.class ) .fieldMap("sourceField1", "sourceField2").converter("myConverterIdValue").add() ... .register();

相关文章

https://juejin.cn/post/7036965055061032996

https://juconcurrent.com/2017/05/19/orika

https://blog.csdn.net/neweastsun/article/details/80559868

 类似资料: