TIL 2021.10.15(금) RestController와 Controller 자료 정리, Mybatis에서 forEach문 사용하기

Jelly·2021년 10월 15일
0

Spring

목록 보기
11/11

####RestController

  • @Controller에 @ResponseBody가 추가된 것으로 Json 형태로 객체 데이터를 반환할 때 사용한다.
  • @Controller는 Model을 만들어 데이터를 담아 view를 찾는 것이지만 @RestController는 객체만을 반환하고 객체 데이터는 JSON, XML 형식으로 HTTP 응답에 전송한다.
  • RestController 어노테이션을 사용하면 모든 메서드가 View 대신 객체로 생성된다.


/출처 : https://www.facebook.com/photo/?fbid=1873079892846068&set=gm.3389524044492534

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="functions" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
function ajaxProc(url,type,dataType){
	$.ajax({
		url:url,
		type:type,
		dataType:dataType,
		success:function(v){
			console.log(v);
		}
	
	});
	
}
	$(function(){
		$("button#ajax1").click(function(){
			ajaxProc('/web/ajaxMessage.do', 'GET', 'text'); 
		});
		$("button#ajax2").click(function(){
			ajaxProc('/web/ajaxAlert.do', 'GET', 'script'); 
		});
		
		$("button#ajax3").click(function(){
			ajaxProc('/web/ajaxJson.do', 'GET', 'json');
		});
		
	});
</script>
</head>
<body>
	<button id="ajax1">Ajax1</button>
	<button id="ajax2">Ajax2</button>
	<button id="ajax3">Ajax3</button>
</body>
</html>
  1. 버튼을 누르면 script를 통해 해당 id 값의 기능이 실행된다.
  2. ajaxProc가 실행되며 파라미터에 전달 인자를 넣어 주게 된다.
  3. 성공하면 Success 펑션이 실행된다.
    해당 경로를 지정해 두었기 때문에 dispather servlet에서 해당 url과 매칭되는 controller를 찾는다. Handler Mapping에서 매칭을 해 준다.

####Mybatis_forEach

  • collection : 전달 받은 인자 List나 Array 형태만 가능하다.
  • item : 전달 받은 인자 값을 alias명으로 대체해 준다.
  • open : 구문 앞에 붙일 문자열.
  • close : 구문의 가장 끝에 붙일 문자열.
  • separator : 반복되는 사이에 출력할 문자열. (, 혹은 # 등 여러 개의 값을 받아 올 때 구분해 주는 역할)
  • index : 반복되는 구문의 번호로 0부터 증가한다.
  <select id="selectMyPage" parameterType="java.util.HashMap" resultType="board">
  <include refid="sql"></include>
  	WHERE NO IN
  	<foreach collection="nos" index="i" item="item" open="(" close=")" separator="," >
  		#{item}
  	</foreach>
  </select>

출처 : https://wook-dragon.tistory.com/8

profile
공부한 것 정리하는 개발 입문자

0개의 댓글