[21.06.16 MySql] 프레임워크 MVC
▶ frontController : 모든 요청을 받아 처리하는 servlet 파일
- URLMapping : 파일 경로와 이름을 직접 적어서 접근하는 방법이 아닌 별칭을 이용하여 접근할때 사용하는 별칭0
▶ frontController와 controller
- frontController(Servlet) : 클라이언트에 요청을 확인하고 요청에 따라 작업을 분배
- controller(Class) : frontController에서 분배 받은 작업을 처리
package kr.smhrd.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("*.do")
public class BoardFrontController extends HttpServlet {
// servlet이라고 명칭
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// =============================== < 1. 클라이언트 요청 확인하기> ===============================
// 현재 접속 경로 가져오기
String requst_url = request.getRequestURI();
//System.out.println(requst_url);
// 현재 context 경로 가져오기
String context_url = request.getContextPath();
//System.out.println(context_url);
// 접속_path - context_path = 접속한 URL
String command = requst_url.substring(context_url.length() + 1, requst_url.length() -3);
System.out.println(command);
// =============================== < 2. 요청에 따라 작업 분배> ===============================
// frontController(servlet)
if (command.equals("list")) {
// Controller(class)
// pojo (Plain Old Java Object)
// list 작업에 필요한 Class
}else if (command.equals("delete")) {
// delete 작업에 필요한 Class
}else {
}
▷ request.getRequestURL();
-> 현재 접속한 ULR을 반환함
ex ) /MVC/list.do -----> /context_path/접속URL
▷ request.getContextPath();
-> 현재 context_path를 반환함
ex ) /MVC -----> /context_path
▷ 현재 접속한 경로를 가져오는법
request.getRequestURL() - request.getContextPath() = 현재 접속한 경로(ULR)
ex ) /MVC/list.do - /MVC = list.do
▶ Mybatis 프레임워크 API 사용
-> sql명령어를 Mapper을 xml로 만들어야 한다.
-> MyBatis 사이트에 참고해서 어떻게 Mapper파일을 생성하는지 참고해야함
!! MyBatis XML, properties, configuration 총 3가지 파일을 생성해야함
https://mybatis.org/mybatis-3/ko/getting-started.html
MyBatis – 마이바티스 3 | 시작하기
Copyright © 2009–2021MyBatis.org. .
mybatis.org
1. MyBatis XML 파일 생성
- 패키지를 만들어서 내부에 mybastic.xml 이름으로 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를 기준으로 select id를 조회하기 때문에 이름이 같더라도 namespace를 다르게 설정하면 된다 -->
<mapper namespace="org.mybatis.example.BlogMapper">
<!-- id를 호출하여 사용하기 때문에 id가 중요하다
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id} -->
<!-- ================= 예제 ======================= -->
<select id="BoardList">
select * from tbl_board
</select>
<!-- ============================================ -->
</mapper>
2. properties 파일 생성
- 패키지에 file을 생성하면서 db.properties 이름으로 생성
# 총 4가지 값이 있어야지 db에 연결할 수 있다.
# !!!!!!!!!!!<<<<<<<< 주의 >>>>>>>>!!!!!!!!!!!!
# 공백 넣으면 안됨
# <<< driver >>>
# db접속에 필요한 드라이버 이름
# <<< url >>>
# url 에서 "jdbc:mysql://"는 '프로토콜' 이라고 하며 뒤쪽 주소는 '서브프로토콜'이라고 한다
# db에서 한글이 깨질때는 서브프로토콜 뒤에 ?~~ 이라는 인코딩 설정을 해줘야함
# <<< username >>>
# db 접속 아이디
# <<< password >>>
# db접속 비밀번호
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/iot
username=root
password=12345
3. configuration 파일 생성
- config.xml 파일 생성
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="kr/smhrd/mybatis/db.properties" />
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 여기가 xml 파일 경로 설정 -->
<mapper resource="kr/smhrd/mybatis/BoardMapper.xml" />
</mappers>
</configuration>