重點技術-201801003-Orika物件對映
Orika:物件屬性對映工具,作用與Dozer類似。
---------------------Maven引入---------------------
<dependency> <groupId>ma.glasnost.orika</groupId> <artifactId>orika-core</artifactId> <version>1.5.2</version> </dependency>
---------------------程式碼使用---------------------
private static final MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
BookEntity bookEntity = new BookEntity("銀河系漫遊指南", "道格拉斯·亞當斯", Date.from(LocalDate.of(1952, Month.MARCH, 11).atStartOfDay(ZoneId.systemDefault()).toInstant()), "{\"ISBN\": \"9787532754687\", \n \"page\": 279\n }", 1);
BookDTO bookDTO = null; Gson gson = new Gson();
//測試1:普通物件轉換 // bookDTO = mapperFactory.getMapperFacade().map(bookEntity, BookDTO.class); // System.out.println(gson.toJson(bookDTO));
//測試2:欄位對映註冊 // mapperFactory.classMap(BookDTO.class, BookEntity.class) // .field("author.name", "authorName") // .field("author.birthday", "authorBirthday") // .byDefault() // .register(); // // bookDTO = mapperFactory.getMapperFacade().map(bookEntity, BookDTO.class); // System.out.println(gson.toJson(bookDTO));
//測試3:欄位轉換器 // mapperFactory.getConverterFactory().registerConverter("bookTypeConvert", // new BidirectionalConverter<BookType, Integer>() // { // @Override // public Integer convertTo(BookType bookType, Type<Integer> type, MappingContext mappingContext) // { // return bookType.getValue(); // } // // @Override // public BookType convertFrom(Integer value, Type<BookType> type, MappingContext mappingContext) // { // return BookType.getBookType(value); // } // } // ); // mapperFactory.classMap(BookDTO.class, BookEntity.class) // .field("author.name", "authorName") // .field("author.birthday", "authorBirthday") // .fieldMap("bookType", "type").converter("bookTypeConvert").add() // .byDefault() // .register(); // // bookDTO = mapperFactory.getMapperFacade().map(bookEntity, BookDTO.class); // // System.out.println(gson.toJson(bookDTO));
//測試4:JSON型別的屬性轉換 mapperFactory.getConverterFactory().registerConverter("bookInfoConvert", new BidirectionalConverter<BookInfo, String>() { @Override public String convertTo(BookInfo bookInfo, Type<String> type, MappingContext mappingContext) { return gson.toJson(bookInfo); }
@Override public BookInfo convertFrom(String json, Type<BookInfo> type, MappingContext mappingContext) { return gson.fromJson(json, BookInfo.class); } }); mapperFactory.getConverterFactory().registerConverter("bookTypeConvert", new BidirectionalConverter<BookType, Integer>() { @Override public Integer convertTo(BookType bookType, Type<Integer> type, MappingContext mappingContext) { return bookType.getValue(); }
@Override public BookType convertFrom(Integer value, Type<BookType> type, MappingContext mappingContext) { return BookType.getBookType(value); } } );
mapperFactory.classMap(BookDTO.class, BookEntity.class) .field("author.name", "authorName") .field("author.birthday", "authorBirthday") .fieldMap("bookType", "type").converter("bookTypeConvert").add() .fieldMap("bookInfo", "bookInformation").converter("bookInfoConvert").add() .byDefault() .register();
bookDTO = mapperFactory.getMapperFacade().map(bookEntity, BookDTO.class);
System.out.println(gson.toJson(bookDTO));