敏捷工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.5 KiB

2 years ago
  1. package com.whn.hellospring.utils;
  2. import org.springframework.beans.BeanUtils;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. /**
  6. * DO,DOT模型转换
  7. */
  8. public class DO2DTOUtil {
  9. private static DO2DTOUtil instance = new DO2DTOUtil();
  10. private DO2DTOUtil(){}
  11. public static DO2DTOUtil getInstance(){
  12. return instance;
  13. }
  14. /**
  15. * DTO集合转换成DO集合
  16. *
  17. * @param dtoList DTO 对象集合
  18. * @param doClass DO 类型
  19. * @return doClass类型的集合
  20. */
  21. public Object dtoListToDoList(Object dtoList, Class doClass) {
  22. if (dtoList == null) {
  23. return null;
  24. }
  25. List<Object> doList = new ArrayList<>();
  26. for (Object dto : (List) dtoList) {
  27. Object i = dtoToDo(dto, doClass);
  28. if (i != null) {
  29. doList.add(i);
  30. }
  31. }
  32. return doList;
  33. }
  34. /**
  35. * DTO模型转换成DO
  36. *
  37. * @param objectDto DTO 对象
  38. * @param doClass DO 类型
  39. * @return doClass类型的对象
  40. */
  41. public Object dtoToDo(Object objectDto, Class doClass) {
  42. if (objectDto == null) {
  43. return null;
  44. }
  45. Object objectDo = null;
  46. try {
  47. objectDo = doClass.newInstance();
  48. BeanUtils.copyProperties(objectDto, objectDo);
  49. } catch (Exception ex) {
  50. ex.printStackTrace();
  51. }
  52. return objectDo;
  53. }
  54. /**
  55. * DO集合转换成DTO集合
  56. *
  57. * @param doList DO 对象集合
  58. * @param dtoClass DTO 类型
  59. * @return dtoClass类型的集合
  60. */
  61. public Object doListToDtoList(Object doList, Class dtoClass) {
  62. if (doList == null) {
  63. return null;
  64. }
  65. List<Object> dtoList = new ArrayList<>();
  66. for (Object i : (List) doList) {
  67. Object dto = doToDto(i, dtoClass);
  68. if (dto != null) {
  69. dtoList.add(dto);
  70. }
  71. }
  72. return dtoList;
  73. }
  74. /**
  75. * DO转换成DTO
  76. *
  77. * @param objectDo DO 对象
  78. * @param dtoClass DTO 类型
  79. * @return dtoClass类型的对象
  80. */
  81. public Object doToDto(Object objectDo, Class dtoClass) {
  82. if (objectDo == null) {
  83. return null;
  84. }
  85. Object objectDto = null;
  86. try {
  87. objectDto = dtoClass.newInstance();
  88. BeanUtils.copyProperties(objectDo, objectDto);
  89. } catch (Exception ex) {
  90. ex.printStackTrace();
  91. }
  92. return objectDto;
  93. }
  94. }