반응형


먼저 이 글을 보기 전에 알고 있으면 더 좋은 글입니다.


1번. MySQL JSON DATA TYPE

2018/11/30 - [Develop/MySQL] - [MySQL] MySQL JSON - MySQL 5.7 JSON Functions



2번. MySQL에서의 '일반' 값을 JSON형식으로 가져오기

2018/10/28 - [Develop/Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기

2018/10/24 - [Develop/Spring Boot] - Spring boot jpa map, hashmap, JSON형식


3번. JSON PARSING

2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막

2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편

2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편

2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

2018/11/06 - [Develop/Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편



나도 SPRING BOOT 에서 어떻게 가져오는지 많이 검색해봤으나. 자료가 거의 없어서 열심히 삽질했던 것 같다.

수 많은 삽질 끝에 알아내서 필요한 곳에 사용중이다.


이번 글은 MySQL에서 SELECT시 일반 DATA 형식이 아닌 JSON DATA TYPE을 SPRING BOOT 에 SELECT하는 내용이다.

우선 일반 DATA형식은 그냥 말그대로 SELECT id, name FROM test 해서 가져오는 내용들이고

JSON DATA TYPE 형식은 위의 1번 링크에서 확인할 수 있다.



1. 우선 이번에 사용할 예제 테이블 및 데이터이다. 1번링크에서 확인 가능하다.



2. 먼저 JSON DATA TYPE을 불러올 코드를 작성한다.


아래의 코드를 보면 CONCAT_WS('\\\\',  부분이 있다.


사실 JSON DATA TYPE을 가져올 때 JSON_OBJECT를 사용하면 가져올 수 있으나. 사용하는 이유는


SPRING BOOT에서 CONCAT_WS('\\\\',  를 사용하지 않으면 MySQL에서 값을 불러올때 쌍따옴표를 지워버린다. 


그래서 만약 CONCAT_WS를 사용하지않고 JSON_OBJECT만을 이용하여 SELECT하면


ex) {"ID": 1, "NAME": "TEST", "PASSWORD": "1234", "PHONE_NUM": "010-1111-1111"} 이렇게 불러와야할 값이

     {"    이렇게 나오고 뒤의 내용은 다 짤려버린다.


그래서 CONCAT_WS 함수를 이용하여 쌍따옴표가 안짤리고 SELECT하기 위해 앞에 역슬래쉬를 붙인다. 


4개 붙이는 이유는 두개 붙이니까 SPRING BOOT단에서 짤려버리더라...... 그래서 4개 붙인다.


그리고 CONCAT_WS를 사용하면 값을 Byte로 불러온다.  그래서 String 형식이나 기타 외의 데이터로는 불러오지 못하므로


항상 Object 데이터 형식을 사용해서 불러와야 한다. 그 후 Object를 Byte로 변환 그리고 Byte를  String으로 변환하는 방법 등을 사용 해야 한다.


아래의 코드에서도 우선 List로 결과를 불러오고 Object를 통해 Byte로 변환 그리고 Byte를 다시 String으로 변환한다.


@GetMapping("/test")
	public void test() {
		Query query = entityManager.createNativeQuery("SELECT " + "concat_ws('\\\\', "
				+ "json_object('id', t.id, 'password', t.password, 'name', t.name, 'phone_num', t.phone_num)) "
				+ "as TEST " + "FROM " + "test t");

		List r = query.getResultList();
		for (Object obj : r) {
			byte[] b = (byte[]) obj;
			String value = new String(b, StandardCharsets.UTF_8);
			System.out.println(value);
		}
}


위 코드를 실행하면 아래와 같은 JSON 타입의 내용을 Spring Boot에서 불러올 수 있다.




3.  이 Json String을 JsonObject로 만들어보자 


3번 링크의 Gson이나 Jackson, JsonObject 등등 을 이용하여 만들 수 있다.


Jackson을 이용하여 한번 만들어 보겠다.


먼저 TestDTO 클래스를 하나 만든다 

3번링크의 Json 1편의 DTO에서 password와 phone_num 변수를 추가한다.


만든 후 현재 데이터를 2개가 불러오기 때문에 객체 배열로 생성하였다.

그 후 각 클래스 배열마다 Jackson의 objectmapper를 사용하여 Json String을 Json Object로 변환시켜주었다.


Query query = entityManager.createNativeQuery("SELECT " + "concat_ws('\\\\', "
				+ "json_object('id', t.id, 'password', t.password, 'name', t.name, 'phone_num', t.phone_num)) "
				+ "as TEST " + "FROM " + "test t");


		List r = query.getResultList();
		
		ObjectMapper objectMapper = new ObjectMapper();
		TestDTO t[] = new TestDTO[r.size()];
		int i=0;
		
		for (Object obj : r) {
			byte[] b = (byte[]) obj;
			String value = new String(b, StandardCharsets.UTF_8);
			try {
				
				t[i]= objectMapper.readValue(value, TestDTO.class);
				i++;
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
		for(TestDTO test : t) {
			System.out.println(test.getId());
			System.out.println(test.getPassword());
			System.out.println(test.getName());
			System.out.println(test.getPhone_num());
			System.out.println();
		}

아래와 같이 결과가 나온다


이것을 많은 형식을 이용하여 불러 올 수있다 2번링크를 사용하여 애초에  Map으로 불러와서 mapping시키거나 할 수 있다.


Map으로 불러와서 mapping할 경우 문제점은 정규식을 통해 파싱해줘야 하는 단점이있다.


왜냐하면 Json값이 { "id" : 1 } 이렇다면 Map으로 불러올경우 "{ "id" : 1 }" 이렇게 중괄호에 쌍따옴표가 붙어버린다.


그래서 정규식을 통해 중괄호의 쌍따옴표만 지워야 하는 단점이 있다. 하지만 나는 이러한 단점에도 불구하고도 


JSON_TYPE과 함께 일반 데이터까지 가지의 값을 가져 와야하기 때문에 자주 사용한다.

반응형
반응형


MySQL 5.7.8 VER 부터 JSON TYPE을 지원해준다. 

그래서 MySQL에서 JSON TYPE을 위한 많은 함수를 제공해준다.


MySQL에서는  JSON Functions의 레퍼런스를 상세히 제공해준다.


MySQL 5.7 : https://dev.mysql.com/doc/refman/5.7/en/json-functions.html
MySQL 8 : https://dev.mysql.com/doc/refman/8.0/en/json.html

아주 기본적인 예만 한번 해본다.

1.TEST라는 이름을 가진 테이블안에 아래와 같은 2개의 데이터가 들어가 있다.


2.  MySQL 레퍼런스에서 제공해주는 JSON_OBJECT라는 Function을 사용하면

아래와 같은 결과를 얻을 수 있다.



{
    "ID":  1,
     "NAME":  "TEST",
     "PASSWORD":  "1234",
     "PHONE_NUM":  "010-1111-1111"
}{
    "ID":  2,
     "NAME":  "HI",
     "PASSWORD":  "5555",
     "PHONE_NUM":  "010-2222-2222"
}


반응형
반응형


마지막으로 hashmap 을 json으로 만드는 방법과, JsonObject를 생성하는 방법이다.



Spring boot Json 1편, 2편, 3편, 4편

1. 2018/11/06 - [Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편

2. 2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

3. 2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편

4. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편



번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기


Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기



HashMap을 Json으로 만드는 방법을 배우기에 앞서 JsonObject를 생성하는 방법을 먼저 본다.


1. google-json-simple을 이용한 JsonObject 만드는 방법.


xml에 아래와 같은 dependency를 추가한다.


		com.googlecode.json-simple
		json-simple
		1.1


우리가 만들어볼 Json예제는 4편에서 사용한 Json이다.


{
    "id": 1,
    "password": "1234",
    "details": [
        {
            "name": "test",
            "age": 20
        },{
            "name": "test2",
            "age": 21
        }
    ] 
}

그리고 아래와 같이 JSONObject라는 것과 JSONArray라는 것을 사용하여 JSON을 생성할 수 있다.


public void test() {
		JSONObject obj = new JSONObject();
		JSONArray arr = new JSONArray();
		
		obj.put("id", 1);
		obj.put("password", "1234");
		
		JSONObject obj2 =new JSONObject();
		obj2.put("name", "test");
		obj2.put("age", 20);
		
		arr.add(obj2);
		
		obj2 = new JSONObject();
		obj2.put("name", "test2");
		obj2.put("age", 21);
		
		arr.add(obj2);
		
		obj.put("details", arr);
		
		System.out.println(obj.toString());
		
}




현재는 Json이 Array를 포함하여 JSONArray를 사용하였지만 일반 적인 key : value 형태는 JSONObject 하나만으로도 생성할 수 있다.


2. Gson을 이용한JsonObject 생성


이 방법은 Json  2편에서 배웠으나 이번엔 Array를 사용하기 때문에 다시 한번 해본다. 사실 위의 json-simple과 별반 다를게 없다.



	public void test() {
		Gson gson = new Gson();
		JsonObject obj = new JsonObject();
		JsonArray arr = new JsonArray();
		
		obj.addProperty("id", 1);
		obj.addProperty("password", "1234");
		
		JsonObject obj2 = new JsonObject();
		obj2.addProperty("name", "test");
		obj2.addProperty("age", 20);
		arr.add(obj2);
		
		obj2= new JsonObject();
		obj2.addProperty("name", "test2");
		obj2.addProperty("age", 21);
		
		arr.add(obj2);
		
		obj.add("details", arr);
		
		String jsonString = obj.toString();
		
		System.out.println("gson을 이용한 객체생성");
		System.out.println(jsonString);
		
	}




3. HashMap을 Json으로 만드는 방법.


HashMap<String, Object>를 선언해서 이번 편에서 했던 모든 것들(json-simple의 obj와 gson의 obj)을 HashMap에 put해보고

일반 데이터를 HashMap에 put해서 json으로 변환해 보는 법을 해본다. 그리고 gson의 prettyprinting을 이용하여 예쁘게 출력해본다.



public void test() {
		JSONObject obj = new JSONObject();
		JSONArray arr = new JSONArray();

		obj.put("id", 1);
		obj.put("password", "1234");

		JSONObject obj2 = new JSONObject();
		obj2.put("name", "test");
		obj2.put("age", 20);

		arr.add(obj2);

		obj2 = new JSONObject();
		obj2.put("name", "test2");
		obj2.put("age", 21);

		arr.add(obj2);

		obj.put("details", arr);

		
		JsonObject g_obj = new JsonObject();
		JsonArray g_arr = new JsonArray();

		g_obj.addProperty("id", 1);
		g_obj.addProperty("password", "1234");

		JsonObject g_obj2 = new JsonObject();
		g_obj2.addProperty("name", "test");
		g_obj2.addProperty("age", 20);
		g_arr.add(g_obj2);

		g_obj2 = new JsonObject();
		g_obj2.addProperty("name", "test2");
		g_obj2.addProperty("age", 21);

		g_arr.add(g_obj2);

		g_obj.add("details", g_arr);
		
		HashMap map = new HashMap();
		map.put("json-simple", obj);
		map.put("gson", g_obj);
		map.put("HashMap", "맵테스트");
		map.put("숫자테스트", 1234567890);
		
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		String jsonString = gson.toJson(map, new TypeToken>() {
		}.getType());
		System.out.println("HashMap to JSON");
		System.out.println(jsonString);

	}



이로써 Spring boot 에서의 Json에 대한 학습을 마무리한다.

1편부터 마지막편까지의 내용과 번외편 내용 2개만 알아도 Spring boot에서 모든 데이터들을 Json으로 만들 수 있다.


반응형
반응형

 

Spring Boot에서 Json 파싱을 위해 Jackson을 기본적으로 제공해준다. Spring에서는 maven 또는 gradle을 xml에 추가해야한다.

사실 기본적인 기능은 앞선 gson과 다를게 없다.

 

Spring boot Json 1편, 2편, 4편, 마지막

1. 2018/11/06 - [Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편

2. 2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

3. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편

4. 2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막

 

 

 

번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기

 

Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기

 

 

1. Jackson ObjectMapper (JsonString to Object)

 

public void test() { 		ObjectMapper objectMapper = new ObjectMapper();  		String Json = 		    "{ \"id\" : 1, \"password\" : \"1234\" }";  		try { 		    TestDTO t = objectMapper.readValue(Json, TestDTO.class);	// String to Object로 변환  		    System.out.println("id = " + t.getId()); 		    System.out.println("password = " + t.getPassword()); 		     		    String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(t); //다시 jsonString으로 변환 		    System.out.println(jsonString); 		} catch (IOException e) { 		    e.printStackTrace(); 		} }

 

(위의 TestDTO은 1편에 나와있다.)

 

objectMapper를 통해 JsonString을 Object로 변환할 수 있었고 writeValueAsString을 통해 다시 JsonString으로도 변환이 가능하다

반응형

2. Object From JsonFile

 

 

ObjectMapper objectMapper = new ObjectMapper();  URL url = new URL("file:data/test.json");  TestDTO t = objectMapper.readValue(url, TestDTO.class); 

 

test.json 파일엔

{

"id" : 1,

"password" : 1234

}

들어 가있다.

 

 

기본적인 사용방법은 여기까지이다 다음편에 Jackson JsonNodeClass를 설명한다.

Jackson 가이드 - https://www.baeldung.com/jackson

반응형
반응형

 

1편에서 Spring Boot에서 데이터를 JSON형식으로 표현하는 가장 기본적인 방법인 클래스를 이용한 방법을 배웠다.

이번에는 Gson을 이용한 JsonOjbect to String, String to JsonObject를 알아아보자.

 

개인적인 경험으론 Gson이 가장 Json파싱에서 핵심이 아닐까 생각한다.

 

 

Spring boot Json 1편, 3편, 4편, 마지막

1. 2018/11/06 - [Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편

2 .2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편

3. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편

4. 2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막

 

 

번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기

 

Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기

 

 

 

1. 먼저 pom.xml에 의존성을 추가한다.

 

 		com.google.code.gson 		gson 		2.8.5  

 

2. Gson을 이용한 Json 생성

 

public String test3() { 		 		Gson gson = new Gson(); 		JsonObject obj = new JsonObject(); 		String id=null; 		obj.addProperty("id", id); 		obj.addProperty("pass", 1234); 		 		String i = obj.toString(); 		String j = gson.toJson(obj); 		System.out.println(i); 		System.out.println(j); 		return i; } 

 

Gson의 JsonObject를 Import받고 JsonObject를 이용하여 Json 형식으로 만들었다.

위의 toString()으로 호출한 결과와 gson.toJson()을 이용한 결과가 다르다는 걸 볼 수 있다.

아래의 gson.toJson()을 이용하면 null값이 들어올 경우 키값이 사라져 버린다.

 

3. String to JsonObject

 

public void test3() { 		 		Gson gson = new Gson(); 		String json = "{\"id\": 1, \"password\": \"1234\"}";  		TestDTO test = gson.fromJson(json, TestDTO.class); 		System.out.println(test.getId()); 		System.out.println(test.getPassword()); 		 } 

 

1편에서 사용한 TestDTO를 통해 Json객체를 생성할 수 있다.

반응형

4.  객체를 Json으로 만들기

 

	public void test3() { 		 		Gson gson = new Gson(); 		TestDTO test2 = new TestDTO(); 		test2.setId(2); 		test2.setPassword("2222"); 		String i=gson.toJson(test2); 		System.out.println(i); 	} 

 

5.  Gson의 JsonParser와 JsonElement를 통한 JsonParsing

 

	public void test3() { 		 		String json = "{\"id\": 1, \"password\": \"1234\"}"; 		JsonParser jp = new JsonParser(); 		JsonElement je = jp.parse(json); 		int id = je.getAsJsonObject().get("id").getAsInt(); 		String pass = je.getAsJsonObject().get("password").getAsString(); 		System.out.println(id + " / " + pass); 	} 

 

6. Gson을 이용하여 Class의 toString() 을 이용한 Json 생성

 

1편의 TestDTO클래스에 Gson을 이용한 toString() 으로 보다 빠르고 편리하게 Json을 생성할 수 있다.

 

import com.google.gson.Gson;  public class TestDTO {  	private Integer id; 	private String password; 	 	 	public TestDTO() { 		super(); 	} 	public TestDTO(Integer id, String password) { 		super(); 		this.id = id; 		this.password = password; 	} 	public Integer getId() { 		return id; 	} 	public void setId(Integer id) { 		this.id = id; 	} 	public String getPassword() { 		return password; 	} 	public void setPassword(String password) { 		this.password = password; 	} 	 	@Override 	public String toString() { 		return new Gson().toJson(this); 	} }  	public void test3() {  		Gson gson = new Gson(); 		TestDTO test2 = new TestDTO(); 		test2.setId(2); 		test2.setPassword("2222"); 		String json = test2.toString(); 		System.out.println(json); 	} 

 

7. 기타

 

이외에도 Json 예쁘게 출력하는 방법 등 여러가지 기능이 존재한다.

 

 

Gson가이드 - https://github.com/google/gson/blob/master/UserGuide.md

 

반응형
반응형

 

Spring Boot에서 데이터를 JSON형식으로 표현하는 가장 기본적인 방법으로는 클래스를 이용하는 방법이다.

이 방법을 알아야 다음에 Deserialization한 JSON을 만들 수 있다.

 

Spring boot Json 2편, 3편, 4편, 마지막

1. 2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

2. 2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편

3. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편

4. 2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막

 

번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기

 

Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기

 

 

 

먼저 DTO를 생성한다.

public class TestDTO {  	private Integer id; 	private String password; 	 	 	public TestDTO() { 		super(); 	} 	public TestDTO(Integer id, String password) { 		super(); 		this.id = id; 		this.password = password; 	} 	public Integer getId() { 		return id; 	} 	public void setId(Integer id) { 		this.id = id; 	} 	public String getPassword() { 		return password; 	} 	public void setPassword(String password) { 		this.password = password; 	} 	 } 

다음 컨트롤러를 하나 생성해준다.

@RestController @RequestMapping(value = "/jsontest") public class TestClass { 	 	@GetMapping() 	public TestDTO test() { 		TestDTO test = new TestDTO(1, "1111"); 		return test; 	} 	 	@GetMapping("/2") 	public List test2() { 		List test = new ArrayList(); 		test.add(0, new TestDTO(1, "1111")); 		test.add(1, new TestDTO(2, "2222")); 		test.add(2, new TestDTO(3, "3333")); 		return test; 	} }
반응형

그리고 실행하게 되면

 

1. test() 실행화면

 

2. test2() 실행화면

 

이렇게 Json 형태로 출력된다

참고로 Map은 Json형태로 출력되지 않고 {"id" = 1, "password"="1111"} 형식으로 출력된다. List만 Json형식으로 출력된다.

또한, 이렇게 클래스로 만든 JsonObject는 Return할 때만 Json으로 출력되지, log또는 print할 경우엔 Json형식으로 출력되지 않는다.

Json형식으로 출력되게 하려면 다음에 할 2편을 참고.

 

기초는 마무리.

 

반응형
반응형

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편

2. 2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

3. 2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편

4. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편

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편집기가 자꾸 안되서 사진으로 대체합니다)


select 문 안에 new map을 선언한다. 주의할 점으로는 as를 사용하지않으면 Map의 Key값에 숫자가 들어간다. 예를들면 만약 저 위의 코드에 as를 주지 않는다면 {0 : id, 1 : password}라는 숫자가 0부터 증가하는 형식으로 들어가게 되며 as를 주게되면 as 로 선언한 이름이 Key값으로 들어간다. 

 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) 를 생성해야 하는 번거로움이 존재.


반응형

+ Recent posts