1. package com.itheima.restkeeper.utils;
    2. import lombok.extern.slf4j.Slf4j;
    3. import ma.glasnost.orika.MapperFacade;
    4. import ma.glasnost.orika.MapperFactory;
    5. import ma.glasnost.orika.MappingContext;
    6. import ma.glasnost.orika.converter.BidirectionalConverter;
    7. import ma.glasnost.orika.converter.ConverterFactory;
    8. import ma.glasnost.orika.impl.DefaultMapperFactory;
    9. import ma.glasnost.orika.metadata.Type;
    10. import java.time.LocalDate;
    11. import java.time.LocalDateTime;
    12. import java.time.LocalTime;
    13. import java.util.List;
    14. /**
    15. * @Description 对象转换工具
    16. */
    17. @Slf4j
    18. public class BeanConv {
    19. private static MapperFacade mapper;
    20. private static MapperFacade notNullMapper;
    21. static {
    22. MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
    23. ConverterFactory converterFactory = mapperFactory.getConverterFactory();
    24. converterFactory.registerConverter(new LocalDateTimeConverter());
    25. converterFactory.registerConverter(new LocalDateConverter());
    26. converterFactory.registerConverter(new LocalTimeConverter());
    27. mapper = mapperFactory.getMapperFacade();
    28. MapperFactory notNullMapperFactory =
    29. new DefaultMapperFactory.Builder().mapNulls(false).build();
    30. notNullMapper = notNullMapperFactory.getMapperFacade();
    31. }
    32. private static class LocalDateTimeConverter
    33. extends BidirectionalConverter<LocalDateTime, LocalDateTime> {
    34. @Override
    35. public LocalDateTime convertTo(
    36. LocalDateTime localDateTime,
    37. Type<LocalDateTime> type,
    38. MappingContext mappingContext) {
    39. return LocalDateTime.from(localDateTime);
    40. }
    41. @Override
    42. public LocalDateTime convertFrom(
    43. LocalDateTime localDateTime,
    44. Type<LocalDateTime> type,
    45. MappingContext mappingContext) {
    46. return LocalDateTime.from(localDateTime);
    47. }
    48. }
    49. private static class LocalDateConverter
    50. extends BidirectionalConverter<LocalDate, LocalDate> {
    51. @Override
    52. public LocalDate convertTo(
    53. LocalDate localDate,
    54. Type<LocalDate> type,
    55. MappingContext mappingContext) {
    56. return LocalDate.from(localDate);
    57. }
    58. @Override
    59. public LocalDate convertFrom(
    60. LocalDate localDate,
    61. Type<LocalDate> type,
    62. MappingContext mappingContext) {
    63. return LocalDate.from(localDate);
    64. }
    65. }
    66. private static class LocalTimeConverter
    67. extends BidirectionalConverter<LocalTime, LocalTime> {
    68. @Override
    69. public LocalTime convertTo(
    70. LocalTime localTime,
    71. Type<LocalTime> type,
    72. MappingContext mappingContext) {
    73. return LocalTime.from(localTime);
    74. }
    75. @Override
    76. public LocalTime convertFrom(
    77. LocalTime localTime,
    78. Type<LocalTime> type,
    79. MappingContext mappingContext) {
    80. return LocalTime.from(localTime);
    81. }
    82. }
    83. /**
    84. * 复制对象所有属性
    85. * @param source 源对象
    86. * @param destination 目标对象
    87. */
    88. public static void toBean(Object source, Object destination) {
    89. mapper.map(source, destination);
    90. }
    91. /**
    92. * 复制对象非null属性
    93. *
    94. * @param source 源对象
    95. * @param destination 目标对象
    96. */
    97. public static void toBeanNotNull(Object source, Object destination) {
    98. notNullMapper.map(source, destination);
    99. }
    100. /**
    101. * 深度复制对象
    102. * @param source 源对象
    103. * @param destinationClass 目标类型
    104. * @return 复制出的目标对象
    105. */
    106. public static <T> T toBean(Object source, Class<T> destinationClass) {
    107. if (EmptyUtil.isNullOrEmpty(source)){
    108. return null;
    109. }
    110. return mapper.map(source, destinationClass);
    111. }
    112. /**
    113. * 复制List
    114. * @param sourceList 源List
    115. * @param destinationClass 目标List的元素类型
    116. * @return 复制出的目标List
    117. */
    118. public static <T> List<T> toBeanList(List<?> sourceList, Class<T> destinationClass) {
    119. if (EmptyUtil.isNullOrEmpty(sourceList)){
    120. return null;
    121. }
    122. return mapper.mapAsList(sourceList,destinationClass);
    123. }
    124. }

    使用
    image.png
    image.png