Orika映射工具MapperFactory使用方法,及LocalDateTime转换异常

一、创建MapperFactory

手动创建

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

spring创建

@Bean
    public MapperFactory getFactory(){
        return new DefaultMapperFactory.Builder().build();
    }

二、使用方法

如果两个类中的属性名一样,类型不一样,会自动转换。如果无法转换会报错。

1. 单个类映射单个类(字段名不完全一样)


//注意User中的name和UserVO中的username属性名不同,age的类型也不同

class User {
    private String name;
    private String age;
    private String  sex;
    //Setter,Getter
}

class UserVO {
    private String username;
    private Integer age;
    private String  sex;
    //Setter,Getter
}
public void UserToUserVO(User user) {
    //先注册以下映射方式
    mapperFactory.classMap(User.class, UserVO.class)
                .field("name","username")//不一样的字段映射
                .byDefault()//剩余的字段映射
                .register();

    UserVO uservo = mapperFactory.getMapperFacade().map(user, UserVO.class);
}

2. 单个类映射单个类(字段名完全一样)


//注意age的类型不同

class User {
    private String username;
    private String age;
    private String  sex;
    //Setter,Getter
}

class UserVO {
    private String username;
    private Integer age;
    private String  sex;
    //Setter,Getter
}
public void UserToUserVO(User user) {
    UserVO uservo = mapperFactory.getMapperFacade().map(user, UserVO.class);
}

3. 集合映射呈另一个泛型的集合


//注意User中的name和UserVO中的username属性名不同,age的类型也不同

class User {
    private String name;
    private String age;
    private String  sex;
    //Setter,Getter
}

class UserVO {
    private String username;
    private Integer age;
    private String  sex;
    //Setter,Getter
}


    public void copyUserListToUserVOList(List userList){
        //手动配置不一样的属性转换
        mapperFactory.classMap(User.class, UserVO.class)
                .field("name","username")//不一样的字段映射
                .byDefault()//剩余的字段映射
                .register();
        //转换List
        List userVOList = mapperFactory.getMapperFacade().mapAsList(userList, UserVO.class);
        //打印验证一下(重写UserVO的tostring)
        userVOList.forEach(user -> {
            System.out.println(user);
        });

三、用了java8中的LocalDateTime结果发现转换异常,解决方法为:在容器中增加一个bean,如下:

@Component
public class OrikaMapperFactoryExtend {
    @Autowired
    private MapperFactory mapperFactory;
    @PostConstruct
    public void init() {
        mapperFactory.getConverterFactory().registerConverter(new LocalDateTimeConverter());
        mapperFactory.getConverterFactory().registerConverter(new LocalDateConverter());
        mapperFactory.getConverterFactory().registerConverter(new LocalTimeConverter());
    }
    private class LocalDateTimeConverter extends BidirectionalConverter<LocalDateTime, LocalDateTime> {
        @Override
        public LocalDateTime convertTo(LocalDateTime source, Type destinationType) {
            return LocalDateTime.from(source);
        }
        @Override
        public LocalDateTime convertFrom(LocalDateTime source, Type destinationType) {
            return LocalDateTime.from(source);
        }
    }
    private class LocalDateConverter extends BidirectionalConverter<LocalDate, LocalDate> {
        @Override
        public LocalDate convertTo(LocalDate source, Type destinationType) {
            return LocalDate.from(source);
        }
        @Override
        public LocalDate convertFrom(LocalDate source, Type destinationType) {
            return LocalDate.from(source);
        }
    }
    private class LocalTimeConverter extends BidirectionalConverter<LocalTime, LocalTime> {
        @Override
        public LocalTime convertTo(LocalTime source, Type destinationType) {
            return LocalTime.from(source);
        }
        @Override
        public LocalTime convertFrom(LocalTime source, Type destinationType) {
            return LocalTime.from(source);
        }
    }
}

你可能感兴趣的:(☀Java,-------【三方类库】,spring,orika,mapper)