myBatis란 DB에 접근을 용이하게 하기 위해 제작된 일종의 Framework이다.
Spring에서의 myBatis 설정은 root context xml에서 설정하며, SQL파일들은 별도의 xml로 관리한다.
설정 내용으로는 Database 접속정보의 sqlSession설정이 있다.
myBatis는 java로 되어 있으며, XML에서 설정 및 쿼리 들을 피싱하여 구동하도록 되어있다.
myBatis설정파일은 myBatis config xml을 활용하며, 접속개수,요청개수들의 설정을 할수있다.
접속인원이 100-200명 넘어가야 효과적임, 규모가 그 아래면은 굳이 할필요없음
스프링은 웹, myBatis는 DB접속관리, 쿼리관리를 쉽게 하기 위해 만들어진 애
root-context.xml 설명
흐름!
MyBatis설정은
여기서 확인가능
여기서 접속개수,요청개수 설정을 할수있음
강사님은 안해두심, 별칭만 해놓음
패키지 만들기
test패키지 안에 또 3개의 패키지 만들기, 거의 기본 구성
Controller나 Service나 만들면은 어노테이션 달기!
dao에는 Repository 저장소란 의미의 어노페이션 달기
component어노테이션 통해서 객체가 만들어지는거임
연결 시키기
1.
2
3
지금 1-> 2-> 3 순서대로 연결해준거임
샘플데이터(기존에 있던 데이터) Blank_SQL.xml을 복붙에서 이름 바꿔줌
이렇게 해야 DB붙을 준비를 함
메소드 만들기
오라클에서 썼던 데이터 가져올 메소드, 밑에 데이터 가져올거임
TestController.java
Create 눌러 -> 이동
ITestService로 이동됨, public이랑 예외처리 해주고, 저장하면
ITestService에서
Implementation 클릭시 TestService로 이동
하면은 ITestDao로 넘어가고
Implementation 클릭시 TestDao로 이동
TestDao.java
Test_SQL.xml에 아래같이 써줌
controller에서
jsp파일 만들기
데이터의 헤더부분이 key값
데이터의 키값대로 적어주기
하면 이렇게!
sellList.jsp 파일
<%@ 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">
table{
border-collapse: collapse;
}
th, td {
border: 1px solid #000;
padding: 5px;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>판매번호</th>
<th>품목</th>
<th>수량</th>
<th>일자</th>
</tr>
</thead>
<tbody>
<c:forEach var="data" items="${list}">
<tr>
<td>${data.SELL_NO}</td>
<td>${data.ITEM_NAME}</td>
<td>${data.COUNT}</td>
<td>${data.SELL_DT}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
이렇게 하면 날짜 보이는 형식 바꿀수있음,
나는 날짜할때 타입 VARCHAR2로 해서 못씀
sellList.jsp
<%@ 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">
table{
border-collapse: collapse;
}
th, td {
border: 1px solid #000;
padding: 5px;
}
</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 () {
console.log($(this).attr("no"));
$("#no").val($(this).attr("no"));
$("#actionForm").submit();
})
})
</script>
</head>
<body>
<form action="sellDetail" id="actionForm" method="post">
<input type="hidden" id="no" name="no" />
</form>
<table>
<thead>
<tr>
<th>판매번호</th>
<th>품목</th>
<th>수량</th>
<th>일자</th>
</tr>
</thead>
<tbody>
<c:forEach var="data" items="${list}">
<tr no="${data.SELL_NO}">
<td>${data.SELL_NO}</td>
<td>${data.ITEM_NAME}</td>
<td>${data.COUNT}</td>
<td>${data.SELL_DT}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
위에 데이터테이블에서 행 한줄 클릭하면 상세보기 화면으로 넘어가는 메소드 만들기(getSell메소드)
메소드 만드는거니까, TestController.java에서 시작해서 Service, Dao 쭉쭉쭉으로 가기
ITestService.java
package com.spring.sample.web.test.service;
import java.util.HashMap;
import java.util.List;
public interface ITestService {
public List<HashMap<String, String>> getSellList() throws Throwable;
public HashMap<String, String> getSell(HashMap<String, String> params) throws Throwable;
}
TestService.java
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 {
// 2. DAO를 가져다 쓰는걸 만들어야함
@Autowired
public ITestDao iTestDao;
@Override
public List<HashMap<String, String>> getSellList() throws Throwable {
return iTestDao.getSellList();
}
@Override
// getSell매소드의 인자로 HashMap<String, String> param 받는거
public HashMap<String, String> getSell(HashMap<String, String> params) throws Throwable {
return iTestDao.getSell(params);
}
}
ITestDao.java
package com.spring.sample.web.test.dao;
import java.util.HashMap;
import java.util.List;
public interface ITestDao {
public List<HashMap<String, String>> getSellList() throws Throwable;
public HashMap<String, String> getSell(HashMap<String, String> params) throws Throwable;
}
TestDao.java
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 {
// 1.
@Autowired
public SqlSession sqlSession;
@Override
public List<HashMap<String, String>> getSellList() throws Throwable {
// selectList("namespace.id"): 해당 namespace에 있는 id를 찾아서 조회 실행 목록을 받음
// 목록으로 받을때 selectList
return sqlSession.selectList("test.getSellList");
}
@Override
public HashMap<String, String> getSell(HashMap<String, String> params) throws Throwable {
// selectOne(쿼리, 데이터) : 해당 쿼리에 데이터를 전달하고 단건 결과를 돌려받음
return sqlSession.selectOne("test.getSell", params);
}
}
Test_SQL.xml에
Test_SQL.xml
<?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">
<!-- namespace : 클래스와 동일, 이 파일의 대표명 -->
<mapper namespace="test">
<!-- select : 조회
id: 구분자, 메소드명과 동일
resultType : 조회 결과 중 한줄에 대한 자료 형태를 지정
hashmap: mybatis-config.xml에서 별칭 지정해놔서 줄여쓸수있는거임
쿼리 삽입시 주의사항: ;이 있는경우 문제가 발생한다. 이유는 해당 쿼리 실행시 자동으로 추가되기 때문
-->
<select id="getSellList" resultType="hashmap">
SELECT SELL_NO, ITEM_NAME, COUNT, SELL_DT
FROM SELL
ORDER BY SELL_DT DESC, SELL_NO DESC
</select>
<!--
parameterType : 실행시 받는 값
#{키} : 헤당위치에 문자열로 키에 해당하는 값을 넣어준다
ex) no에 3이 들어있는 경우
WHERE SELL_NO = #{no}
=> WHERE SELL_NO = '3' // 문자열이기 때문에 ' '이 들어감
-->
<select id="getSell" resultType="hashmap" parameterType="hashmap">
SELECT SELL_NO, ITEM_NAME, COUNT, SELL_DT
FROM SELL
WHERE SELL_NO = #{no}
</select>
</mapper>
다시 컨트롤러와서 mav~~~ 적어주기
params.toString();
{no=20}으로 찍힘
SELECT SELL_NO, ITEM_NAME, COUNT, SELL_DT FROM SELL WHERE SELL_NO = '20' 이부분이 Test_SQL.xml에 적어준 내용
TestController.java
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 {
// 3.
@Autowired
public ITestService iTestService;
/*
* Database나 파일 같이 외부에 접근하는 경우 외적 요인으로 문제가 발샐할 수 있음으로 예외처리가 반드시 필요하다.
*/
@RequestMapping(value = "/sellList")
public ModelAndView sellList(ModelAndView mav) throws Throwable {
// 데이터 취득
List<HashMap<String, String>> list = iTestService.getSellList();
mav.addObject("list", list);
mav.setViewName("test/sellList");
return mav;
}
@RequestMapping(value = "/sellDetail")
// 페이지 이동할때 값이 하나가 넘어가는 경우가 거의 없음,
public ModelAndView sellDetail(@RequestParam HashMap<String, String> params, ModelAndView mav) throws Throwable {
System.out.println(params.toString());
HashMap<String, String> data = iTestService.getSell(params);
mav.addObject("data", data);
mav.setViewName("test/sellDetail");
return mav;
}
}
sellDetail.jsp
<%@ 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>
</head>
<body>
판매번호: ${data.SELL_NO}<br/>
품목: ${data.ITEM_NAME}<br/>
수량: ${data.COUNT}<br/>
일자: ${data.SELL_DT}<br/>
</body>
</html>
상세보기 만들기 순서흐름
매개변수 때문에 헷갈리면 a로 바꿔도 상관없음
// params를 다른이름 줘도됨, a이든 뭐든 상관없음, 여기서만, 나는 HashMap만 받으면 된다
2
우리는 아직값 no만 넘겼지만, 여러개 넘길수있으니 HashMap으로 하심
sellList에서 input을 하나 더 많들어서 값을 넘기면
8
5
TestController.java
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 {
// 3.
@Autowired
public ITestService iTestService;
/*
* Database나 파일 같이 외부에 접근하는 경우 외적 요인으로 문제가 발샐할 수 있음으로 예외처리가 반드시 필요하다.
*/
@RequestMapping(value = "/sellList")
public ModelAndView sellList(ModelAndView mav) throws Throwable {
// 데이터 취득
List<HashMap<String, String>> list = iTestService.getSellList();
mav.addObject("list", list);
mav.setViewName("test/sellList");
return mav;
}
@RequestMapping(value = "/sellDetail")
// 페이지 이동할때 값이 하나가 넘어가는 경우가 거의 없음,
// params를 다른이름 줘도됨, a이든 뭐든 상관없음, 여기서만, 나는 HashMap만 받으면 된다
public ModelAndView sellDetail(@RequestParam HashMap<String, String> params, ModelAndView mav) throws Throwable {
System.out.println(params.toString());
HashMap<String, String> data = iTestService.getSell(params);
mav.addObject("data", data);
mav.setViewName("test/sellDetail");
return mav;
}
@RequestMapping(value = "/managerList")
public ModelAndView managerList(ModelAndView mav) throws Throwable {
// 데이터 취득
List<HashMap<String, String>> list = iTestService.getManagerList();
mav.addObject("list", list);
mav.setViewName("test/managerList");
return mav;
}
@RequestMapping(value = "/managerDetail")
// 페이지 이동할때 값이 하나가 넘어가는 경우가 거의 없음,
// params를 다른이름 줘도됨, a이든 뭐든 상관없음, 여기서만, 나는 HashMap만 받으면 된다
public ModelAndView managerDetail(@RequestParam HashMap<String, String> params, ModelAndView mav) throws Throwable {
System.out.println(params.toString());
HashMap<String, String> data = iTestService.getManager(params);
mav.addObject("data", data);
mav.setViewName("test/managerDetail");
return mav;
}
}
5
ITestService.java
package com.spring.sample.web.test.service;
import java.util.HashMap;
import java.util.List;
public interface ITestService {
public List<HashMap<String, String>> getSellList() throws Throwable;
public HashMap<String, String> getSell(HashMap<String, String> params) throws Throwable;
public List<HashMap<String, String>> getManagerList() throws Throwable;
public HashMap<String, String> getManager(HashMap<String, String> params) throws Throwable;
}
6oiT
TestService.java
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 {
// 2. DAO를 가져다 쓰는걸 만들어야함
@Autowired
public ITestDao iTestDao;
@Override
public List<HashMap<String, String>> getSellList() throws Throwable {
return iTestDao.getSellList();
}
@Override
// getSell매소드의 인자로 HashMap<String, String> param 받는거
public HashMap<String, String> getSell(HashMap<String, String> params) throws Throwable {
return iTestDao.getSell(params);
}
@Override
public List<HashMap<String, String>> getManagerList() throws Throwable {
// TODO Auto-generated method stub
return iTestDao.getManagerList();
}
@Override
public HashMap<String, String> getManager(HashMap<String, String> params) throws Throwable {
// TODO Auto-generated method stub
return iTestDao.getManager(params);
}
}
ITestDao.java
package com.spring.sample.web.test.dao;
import java.util.HashMap;
import java.util.List;
public interface ITestDao {
public List<HashMap<String, String>> getSellList() throws Throwable;
public HashMap<String, String> getSell(HashMap<String, String> params) throws Throwable;
public List<HashMap<String, String>> getManagerList() throws Throwable;
public HashMap<String, String> getManager(HashMap<String, String> params) throws Throwable;
}
TestDao.java
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 {
// 1.
@Autowired
public SqlSession sqlSession;
@Override
public List<HashMap<String, String>> getSellList() throws Throwable {
// selectList("namespace.id"): 해당 namespace에 있는 id를 찾아서 조회 실행 목록을 받음
// 목록으로 받을때 selectList
return sqlSession.selectList("test.getSellList");
}
@Override
public HashMap<String, String> getSell(HashMap<String, String> params) throws Throwable {
// selectOne(쿼리, 데이터) : 해당 쿼리에 데이터를 전달하고 단건 결과를 돌려받음
return sqlSession.selectOne("test.getSell", params);
}
@Override
public List<HashMap<String, String>> getManagerList() throws Throwable {
// selectList("namespace.id"): 해당 namespace에 있는 id를 찾아서 조회 실행 목록을 받음
// 목록으로 받을때 selectList
return sqlSession.selectList("test.getManagerList");
}
@Override
public HashMap<String, String> getManager(HashMap<String, String> params) throws Throwable {
// selectOne(쿼리, 데이터) : 해당 쿼리에 데이터를 전달하고 단건 결과를 돌려받음
return sqlSession.selectOne("test.getManager", params);
}
}
7
Test_SQL.xml
1
<?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">
<!-- namespace : 클래스와 동일, 이 파일의 대표명 -->
<mapper namespace="test">
<!-- select : 조회
id: 구분자, 메소드명과 동일
resultType : 조회 결과 중 한줄에 대한 자료 형태를 지정
hashmap: mybatis-config.xml에서 별칭 지정해놔서 줄여쓸수있는거임
쿼리 삽입시 주의사항: ;이 있는경우 문제가 발생한다. 이유는 해당 쿼리 실행시 자동으로 추가되기 때문
-->
<select id="getSellList" resultType="hashmap">
SELECT SELL_NO, ITEM_NAME, COUNT, SELL_DT
FROM SELL
ORDER BY SELL_DT DESC, SELL_NO DESC
</select>
<!--
parameterType : 실행시 받는 값
#{키} : 헤당위치에 문자열로 키에 해당하는 값을 넣어준다
ex) no에 3이 들어있는 경우
WHERE SELL_NO = #{no}
=> WHERE SELL_NO = '3' // 문자열이기 때문에 ' '이 들어감
parameterType dao받는값, resultType값이 db실행결과
-->
<select id="getSell" resultType="hashmap" parameterType="hashmap">
SELECT SELL_NO, ITEM_NAME, COUNT, SELL_DT
FROM SELL
WHERE SELL_NO = #{no}
</select>
<select id="getManagerList" resultType="hashmap" parameterType="hashmap">
SELECT EMP_NO, NAME, DEPT
FROM MANAGER
</select>
<select id="getManager" resultType="hashmap" parameterType="hashmap">
SELECT EMP_NO, NAME, DEPT
FROM MANAGER
WHERE EMP_NO = #{no}
</select>
</mapper>
managerList.jsp
<%@ 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">
table{
border-collapse: collapse;
}
th, td {
border: 1px solid #000;
padding: 5px;
}
</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 () {
console.log($(this).attr("no"));
$("#no").val($(this).attr("no"));
$("#actionForm").submit();
})
})
</script>
</head>
<body>
<form action="managerDetail" id="actionForm" method="post">
<input type="hidden" id="no" name="no" />
</form>
<table>
<thead>
<tr>
<th>사원번호</th>
<th>이름</th>
<th>부서</th>
</tr>
</thead>
<tbody>
<c:forEach var="data" items="${list}">
<tr no="${data.EMP_NO}">
<td>${data.EMP_NO}</td>
<td>${data.NAME}</td>
<td>${data.DEPT}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
9
managerDetail.jsp
<%@ 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 () {
$("#listBtn").on("click", function() {
history.back();
});
});
</script>
</head>
<body>
판매번호: ${data.EMP_NO}<br/>
이름: ${data.NAME}<br/>
부서: ${data.DEPT}<br/>
<input type="button" value="목록" id="listBtn" />
</body>
</html>
1
실습하고 나서 위에 전체코드, 실습은 내일 풀어주실예정
오늘의 궁금증
1 .Test_SQL.xml에서 parameterType과 resultType를 반대 개념으로 생각함
parameterType는 선택적이라는데 그러면 어떻게 받아오는가
2 .service는 무슨 역활인가
MVC패턴에서 service는 없고, 그리고 service 코드를 보면 그냥 Dao의 메소드를 리턴해주는거니까,
Dao에서 바로 controller로 가면 안되나
6-> 강사님 왈: service 없이 하는 회사도 있데, service에 기능이 들어갈수도 있고, 추가적으로 더 넣을수도 있기 때문에 지금은 풀 사이클로 돌려보는중
https://aibees.github.io/Why-Use-Service/
Spring MVC에서 Service의 역할
의문점 제기 흔히 얘기되는, MVC 패턴에서 비즈니스 로직 구성은 Controller, Service, Dao로 역할을 분산시켜 개발한다는 이야기는 일반적이다.
aibees.github.io
3
나랑 똑같이 생각했네
'TIL > academy' 카테고리의 다른 글
국비 TIL(Today I Learned) 20220805 오라클에서 divs 데이터 가져오기 실습 (0) | 2022.08.05 |
---|---|
국비 TIL(Today I Learned) 20220804 스프링 게시판 글추가, 수정, 삭제 (0) | 2022.08.04 |
국비 TIL(Today I Learned) 20220802 스프링.... 뭘 배웠는지 몰라서 제목을 뭐라 써야할지도 모르겠다. (0) | 2022.08.02 |
국비 TIL(Today I Learned) 20220729 core태그, el 태그, spring설치 (0) | 2022.07.29 |
국비 TIL(Today I Learned) 20220728 시험, 팀플 (0) | 2022.07.29 |