hanker

JAVA - Entity to Map, Map to Entity 본문

JAVA

JAVA - Entity to Map, Map to Entity

hanker 2024. 9. 1. 21:53
반응형

○ Entity to Map

 

사용 예를 들기 위해서 User 클래스 생성

public class User {

    private String name;
    private String email;

	// Getter, Setter
}

 

 

1. Reflection 사용

    
    public static void main(String[] args) throws IllegalAccessException {
        User user = new User();

        user.setEmail("test@gmail.com");
        user.setName("테스터");

        Map<String, Object> map = entityToMapA(user);
        System.out.println(map.toString());
    }
    
    
    
    public static Map<String, Object> entityToMapA(Object entity) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        Field[] fields = entity.getClass().getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            map.put(field.getName(), field.get(entity));
        }

        return map;
    }

결과 : 

 

 

2. Jackson 라이브러리 사용

// pom.xml 추가
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.3</version>
</dependency>


// Gradle 추가
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.3'


// 버전은 각자 Spring, Java Version에 맞게 사용
    public static void main(String[] args) throws Exception {
        User user = new User();

        user.setEmail("test@gmail.com");
        user.setName("테스터");

        Map<String, Object> map = entityToMapB(user);
        System.out.println(map.toString());
    }

    public static Map<String, Object> entityToMapB(Object entity) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.convertValue(entity, Map.class);
    }

결과 : 

 

 

○ Map to Entity

 

1. Reflection 사용

    public static void main(String[] args) throws Exception {

        Map<String, Object> map = new HashMap<>();

        map.put("name", "테스터");
        map.put("email", "test@gmail.com");

        User user = new User();

        user = mapToEntity(map, User.class);

        System.out.println("email = " + user.getEmail());
        System.out.println("name = " + user.getName());
    }

    public static <T> T mapToEntityA(Map<String, Object> map, Class<T> clazz) throws Exception {
        T entity = clazz.getDeclaredConstructor().newInstance();

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Field field = clazz.getDeclaredField(entry.getKey());
            field.setAccessible(true);
            field.set(entity, entry.getValue());
        }

        return entity;
    }

결과 : 

 

2. Jackson 라이브러리 사용

    public static void main(String[] args) throws Exception {

        Map<String, Object> map = new HashMap<>();

        map.put("name", "테스터");
        map.put("email", "test@gmail.com");

        User user = new User();

        user = mapToEntityB(map, User.class);

        System.out.println("email = " + user.getEmail());
        System.out.println("name = " + user.getName());
    }

    public static <T> T mapToEntityB(Map<String, Object> map, Class<T> clazz) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.convertValue(map, clazz);
    }

결과 : 

 

반응형