1.Controller에 주소연결 및 메소드 생성
2.Service 메소드 호출작성-> 빨간줄 클릭-> create Method
-> I Service에서 앞뒤로 접근권한,예외처리
-> 컨트롤 + 인터페이스명 커서로 클래스 이동
-> add ~~로 메소드 추가
3. dao 메소드 호출 작성-> 빨간줄 클릭-> 위와 동일
4.developer에서 쿼리작성 및 확인 -> SQL.xml에 태그 추가 및 쿼리 붙이기
-> dao에서 sqlsession 메소드 호출
5.controller 작업 마무리 -> 화면 적용
※자바랑 XML 파일 수정 시 먼저 서버 중단 수정 후 서버 새로고침 필수
#{이름} -> 해당위치에 이름에 해당하는 값
이 문자열로 들어간다.ex)('값')
${이름} -> 해당위치에 이름에 해당하는 값을 그대로 넣고 쿼리
로 인식.
ex) where B_NO = ${bNo} --> where B_NO = 4
col='a,b,c'
select #{col} ==> 'a,b,c'
select ${col} ==> select a,b,c
차이점: 따옴표
#을 이용하자
실습
주소 :testMList
화면에 나올사항 : 표를 출력하시오(회원번호, 아이디,이름,생년월일)
최근가입회원부터 출력
실습결과
상세보기 실습
내코드 올리기
TestController
package com.spring.sample.web.test.controller;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.spring.sample.web.test.service.ITestService;
@Controller
public class TestController {
//객체주입 받겠다.
@Autowired
public ITestService iTestService;
@RequestMapping(value="/test1")
public ModelAndView test1(ModelAndView mav) throws Throwable {
List<HashMap<String, String>>list
= iTestService.getBList();
mav.addObject("list", list);
mav.setViewName("test/test1");
return mav;
}
@RequestMapping(value="/testMList")
public ModelAndView testMList(ModelAndView mav) throws Throwable{
List<HashMap<String, String>>list1
= iTestService.getMList();
mav.addObject("list", list1);
mav.setViewName("test/testMList");
return mav;
}
@RequestMapping(value="/test2")
public ModelAndView tes2(
@RequestParam HashMap<String, String> params,
ModelAndView mav) throws Throwable{
//단일 컬럼은 해쉬맵으로 갖고온다
HashMap<String, String> data
=iTestService.getB(params);
mav.addObject("data", data);
mav.setViewName("test/test2");
return mav;
}
}
ITestService
package com.spring.sample.web.test.service;
import java.util.HashMap;
import java.util.List;
public interface ITestService {
public List<HashMap<String, String>> getBList() throws Throwable;
public List<HashMap<String, String>> getMList() throws Throwable;
public HashMap<String, String> getB(HashMap<String, String> params) throws Throwable;
}
TestService
package com.spring.sample.web.test.service;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spring.sample.web.test.dao.ITestDao;
@Service
public class TestService implements ITestService {
//객체 주입 받겠다.
@Autowired
public ITestDao iTestDao;
@Override
public List<HashMap<String, String>> getBList() throws Throwable {
return iTestDao.getBList();
}
@Override
public List<HashMap<String, String>> getMList() throws Throwable {
return iTestDao.getMList();
}
@Override
public HashMap<String, String> getB(HashMap<String, String> params) throws Throwable {
return iTestDao.getB(params);
}
}
ITestDao
package com.spring.sample.web.test.dao;
import java.util.HashMap;
import java.util.List;
public interface ITestDao {
public List<HashMap<String, String>> getBList() throws Throwable;
public List<HashMap<String, String>> getMList() throws Throwable;
public HashMap<String, String> getB(HashMap<String, String> params) throws Throwable;
}
TestDao
package com.spring.sample.web.test.dao;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
//저장소에 접근한다.
@Repository
public class TestDao implements ITestDao {
@Autowired
public SqlSession sqlSession;
@Override
public List<HashMap<String, String>> getBList() throws Throwable {
return sqlSession.selectList("B.getBList");
}
@Override
public List<HashMap<String, String>> getMList() throws Throwable {
return sqlSession.selectList("M.getMList");
}
@Override
public HashMap<String, String> getB(HashMap<String, String> params) throws Throwable {
//단일row selectOne
return sqlSession.selectOne("B.getB",params);
}
}
M_SQL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="M"><!-- namespace: 클래스명과동일 -->
<!-- id: 메소드명과 동일 -->
<!-- resultType: row 1줄의 형태를 지정 -->
<!-- 쿼리 작성 시 ; 이 들어가면 실행 되지 않음 -->
<select id="getMList" resultType="hashmap">
SELECT M_NO,M_ID,M_NM,TO_CHAR(M_JOINDT, 'HH:MM:SS') AS TIME
FROM M
ORDER BY M_JOINDT DESC
</select>
</mapper>
B_SQL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="B"><!-- namespace: 클래스명과동일 -->
<!-- id: 메소드명과 동일 -->
<!-- resultType: row 1줄의 형태를 지정 -->
<!-- 쿼리 작성 시 ; 이 들어가면 실행 되지 않음 -->
<select id="getBList" resultType="hashmap">
SELECT B_NO, B_TITLE, B_WRITER, TO_CHAR(B_DT, 'YYYY-MM-DD')AS B_DT
FROM B
ORDER BY B_NO DESC
</select>
<!-- parameterType은 받는 값타입에 대한 것 resultType: 쿼리결과타입에 대한 것 -->
<select id="getB" parameterType="hashmap" resultType="hashmap">
SELECT B_NO, B_TITLE, B_WRITER, B_CON, TO_CHAR(B_DT, 'YYYY--MM--DD') AS B_DT
FROM B
WHERE B_NO = #{bNo}
</select>
</mapper>
test1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
thead{
background-color: orange;
}
</style>
<script type="text/javascript"
src="resources/script/jquery/jquery-1.12.4.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("tbody").on("click", "tr", function(){
$("#bNo").val($(this).attr("name")); //여기서 this는 클릭한 tr
$("#goForm").submit();
});
}); //ready end
</script>
</head>
<body>
<form action="test2" id="goForm" method="post">
<input type="hidden" id="bNo" name="bNo"/>
</form>
<table>
<thead>
<tr>
<th>번호</th>
<th>제목</th>
<th>작성자</th>
<th>작성일</th>
</tr>
</thead>
<tbody>
<c:forEach var="data" items="${list}">
<tr name="${data.B_NO}">
<td>${data.B_NO}</td>
<td>${data.B_TITLE}</td>
<td>${data.B_WRITER}</td>
<td>${data.B_DT}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
test2
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript"
src="resources/script/jquery/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
}); //ready end
</script>
</head>
<body>
번호: ${data.B_NO}<br/>
제목: ${data.B_TITLE}<br/>
작성자: ${data.B_WRITER}<br/>
작성일: ${data.B_DT}<br/>
내용<br/>
${data.B_CON}<br/>
</body>
</html>
testMList
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
thead{
background-color: red;
}
</style>
</head>
<body>
<table border="2" cellspacing="0">
<thead>
<tr>
<th>회원번호</th>
<th>아이디</th>
<th>이름</th>
<th>생년월일</th>
</tr>
</thead>
<tbody>
<%-- items는 키값을 적어야한다 벨류값이 아니고 ㅋㅋ --%>
<c:forEach var="info" items="${list}">
<tr>
<td>${info.M_NO}</td>
<td>${info.M_ID}</td>
<td>${info.M_NM}</td>
<td>${info.TIME}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
상세보기가 안된다....왜 안될까 ㅋㅋ내일 아침에 다시 해보자