반응형
JPA ORM을 이용한 @Query의 결과값을 Map, JSON형식으로 바로 만들어주는 방법은 2가지가 있다
1. Map을 이용한다.
2. DTO(POJO, VO)를 생성하여 이용한다.
JPA EntityManager를 이용한 Map형식으로 Mapping하는 방법은 아래 링크에 있다.
1. 2018/10/28 - [Develop/Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기
Spring Boot Json익히기
Spring boot Json 1편, 2편, 3편, 4편, 마지막
1. 2018/11/06 - [Develop/Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편
3. 2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편
5. 2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막
Spring boot에서 MySQL JSON 타입 SELECT하는 방법
1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기
1. Map을 이용.
(html편집기가 자꾸 안되서 사진으로 대체합니다)
2. DTO(POJO, VO)를 이용하는 방법.
먼저 패키지를 생성하고 DTO(POJO, VO)를 생성한다
ex) net.test.com // 패키지안에
TestDTO.java 생성
public class TestDTO{
private String id;
private String password;
public TestDTO(){}
public Test(String id, String password){
this.id = id;
this.password=password;
}
public String getId(){
return id;
}
public void setId(String id){
this.id =id;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password =password;
}
}
주의할점은 디폴트 생성자를 필수적으로 생성해줘야 한다.
이후
@Query("select new net.test.com.TestDTO(t.test_id, t.test_password) from Test t")
List test();
//위의 값을 가져오는 방법.
List t = testDAO.test();
t.getId()
1번의 Map과 다르게 as를 설정하지 않아도 {id : id} 값으로 잘 받을 수 있다. 하지만 일일이 DTO(POJO, VO) 를 생성해야 하는 번거로움이 존재.반응형
'Develop > Spring Boot' 카테고리의 다른 글
| Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기 (0) | 2018.10.28 |
|---|---|
| Spring Boot AWS Elastic BeanStalk을 이용한 배포 (0) | 2018.10.26 |
| application.properties에 작성안하고 Run Configuration에 설정하기 (0) | 2018.10.26 |
| Spring boot 커맨드라인으로 profile 또는 외부 properties시 주의사항. (0) | 2018.10.23 |
| Spring boot Log 개발용 로그, 배포용 로그 profile 설정 (1) | 2018.10.23 |