본문 바로가기
풀스택/spring 다시 복습 처음부터

5/14 eclipse react연동, json통신

by woohyun22 2019. 5. 15.

backend


springboot - mybatis



-Controller


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.douzone.smartchecker.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.douzone.dto.JSONResult;
import com.douzone.smartchecker.service.TimeService;
 
@CrossOrigin
@RequestMapping("/time")
@Controller
 
//@CrossOrigin    
//@RequestMapping("/time")
//@Controller
public class TimeController {
 
    @Autowired
    TimeService timeService;
    
    
    @ResponseBody
    @GetMapping("/{no}")//파라미터(변수)를 url에 같이 넘겨줌(null값과 공백값은 사용x)
    public JSONResult get(@PathVariable long no) {//@pathvariable와 동일하게 선언한다.
        return JSONResult.success(timeService.time(no));//model 형식도 
    }
}
 
cs
  • @CrossOrigin (origins = "http://jeong-pro.tistory.com", maxAge = 3600)

-> 기본 도메인이 http://jeong-pro.tistory.com 인 곳에서 온 ajax요청만 받아주겠다.

  • @RequestMapping 클라이언트는 URL로 요청을 전송 요청 URL을 어떤 메서드가 처리할지 여부를 결정하는 것이 @RequestMapping이다.
  • @Controller 붙이면 해당 클래스를 웹 요청을 처리하는 컨트롤러로 사용할 수 있다.

  • @Autowired service
  • @ResponseBody
  • @GetMapping

- TimeService


  • @pathvariable @RequestMapping 어노테이션 값으로 {템플릿변수} 를 사용합니다.
     @PathVariable 어노테이션을 이용해서 {템플릿 변수} 와 동일한 이름을 갖는 파라미터를 추가하면 됩니다. 파라미터넘겨줌



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.douzone.smartchecker.service;
 
import java.util.HashMap;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.douzone.dto.JSONResult;
import com.douzone.smartchecker.vo.CommuteVo;
 
@Service //꼭 붙여야된다. 서비스단 실행되게
public class TimeService {
    @Autowired
    private WorkTimeService workTimeService;
    
    @Autowired
    private CommuteService commuteService;
    
 
    
    public Map<String, Object> time(Long no){
        String time = workTimeService.getUseWorkTime().getStart();
        //time에 workTimeService.getUseWorkTime()의 start를 넣는다.
        CommuteVo commuteVo = commuteService.getUseCommuteTime(no);
        //위처럼 값 한개(start)만 가져오면 위처럼 쓰고 두개 이상을 가져와야될떄는
        //Vo를 가져와서 여러번 디비를 들락날락 안하는게 보편적
        if(commuteVo !=null) {//비어있을때 error뜨는 것을 방지해서
        String state = commuteVo.getState();
        String date = commuteVo.getDay();
        Map<String, Object> map = new HashMap<String, Object>();//hashmap 배열에 
        map.put("time", time);//put으로 각각의 값에 넣어준다. - "" 안엥는것이 통신시 값에대한 name
        map.put("state", state);
        map.put("date", date);
        return map;
        }
        return null;//비면 null값 반환
    }
    
    
    
}
 
cs


service단에서 다른곳의 값들을 사용하려면 dao보단 service자체를 불러와서 쓰자 - 그렇게되면 dao와 vo는 원래 만들어져있던 것들을 쓰기에 불필요.



src/main/resources/mybatis/mapper/~~.xml


728x90

댓글