项目作者: lbovolini

项目描述 :
Maps an object to another via getters and setters methods
高级语言: Java
项目地址: git://github.com/lbovolini/object-mapper.git
创建时间: 2020-08-15T00:55:02Z
项目社区:https://github.com/lbovolini/object-mapper

开源协议:MIT License

下载


ObjectMapper

CI Coverage Status Maven Central License: MIT

Maps an object to another via getters and setters methods

Features

  • Maps DTO to Model/Entity object
  • Maps Model/Entity to DTO object
  • Maps nested objects
  • Maps nested Lists, Sets and Maps
  • Can map any object to any object if it follows the restrictions
  • Caches objects getters and setters methods for improve performance

Restrictions

  • Maps only corresponding origin object field if it has getter method and destination object have corresponding setter method
  • Returned class should have accessible constructor without parameters
  • Getter and setter methods name should have more than 3 characters

Requirements

  • Java 1.7+

Examples

  1. @Entity
  2. public class Student {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. private int id;
  6. private String name;
  7. private String email;
  8. private String password;
  9. private LocalDate birthday;
  10. @Column(name = "Curso_id")
  11. private int courseId;
  12. @ManyToOne
  13. @JoinColumn(name = "Curso_id", insertable = false, updatable = false)
  14. private Course course;
  15. // getters, setters, equals, hashCode
  16. }
  1. public class StudentDTO {
  2. private int id;
  3. private String name;
  4. private String email;
  5. private String password;
  6. private LocalDate birthday;
  7. private int courseId;
  8. // getters, setters, equals, hashCode
  9. }

1. Maps DTO to Model

  1. StudentDTO studentDTO = new StudentDTO();
  2. studentDTO.setId(id);
  3. studentDTO.setName(name);
  4. studentDTO.setEmail(email);
  5. studentDTO.setPassword(password);
  6. studentDTO.setBirthday(birthday);
  7. studentDTO.setCourseId(courseId);
  8. Student student = ObjectMapper.map(studentDTO, Student.class);

2. Maps Model to DTO

  1. Student student = new Student();
  2. student.setId(id);
  3. student.setName(name);
  4. student.setEmail(email);
  5. student.setPassword(password);
  6. student.setBirthday(birthday);
  7. student.setCourseId(courseId);
  8. StudentDTO studentDTO = ObjectMapper.map(student, StudentDTO.class);

3. Maps List, Set and Map

  1. List<Discipline> disciplineList = execute(query, parameters);
  2. List<DisciplineDTO> disciplineDTOList = ObjectMapper.map(disciplineList, DisciplineDTO.class)