본문 바로가기

카테고리 없음

Spring + MyBatis + MySQL CRUD 구현 16 prots

servlet-context.xml, root-context.xml, web.xml 차이점

1.servlet-context.xml

  • url과 관련된 controlloer, 어노테이션 등의 설정을 할 수 있다.
  • Servlet 단위로 생성되는 context
  • Spring에서 servlet-context.xml 파일은 DispatcherServlet 생성 시에 필요한 설정 정보를 담은 파일 (Interceptor, Bean생성, ViewResolver등..)
  • URL설정이 있는 Bean을 생성 (@Controller, Interceptor)
  • Application Context를 자신의 부모 Context로 사용한다.
  • Application Context와 Servlet Context에 같은 id로 된 Bean이 등록 되는 경우, Servlet Context에 선언된 Bean을 사용한다.
  • Bean 찾는 순서
    • Servlet Context에서 먼저 찾는다.
    • 만약 Servlet Context에서 bean을 못찾는 경우 Application Context에 정의된 bean을 찾는다.

2.root-context.xml

  • view와 관련되지 않은 객체를 정의한다.
  • BEAN 설정
  • DAO, DB 등 비즈니스로직과 관련있다.

3.web.xml

  • 설정을 위한 설정 파일
  • servlet class 등록
  • WAS초기 구동 설정
  • xml 경로 설정

https://thiago6.tistory.com/70(링크참고)

 

servlet-context.xml, root-context.xml, web.xml 차이점 알기!

servlet-context.xml, root-context.xml, web.xml에 대한 차이점에 대해 정리해봅니다. 우선, xml 파일은 모두 객체(Bean)를 정의합니다. 1. servlet-contex.xml servlet에서 보듯이 요청과 관련된 객체를 정의합..

thiago6.tistory.com

단계별 정리

spring 프로젝트 생성

1. 툴 : Spring Tool Suite 3.9.5 (이클립스)

       -> 이클립스 상단매뉴 Help -> MarketPlace -> STS 설치.

2. WAS : 아파치 톰캣 8

3. JDK 

4. DB - MYSQL 

5. DB 툴 : MYSQL db4free

  • 프로젝트 생성 Spring Legacy Project / 템플릿 - Spring MVC Project
  • 패키지 구조는 3단 구조

3단구조(com.camp.myapp)

pom.xml

Maven Project의 핵심이다. import 되는 jar 파일 정보 및 버전정보를 담고 있다.

필수구문과 추가 설정 구문으로 나눠진다.

 

pom.xml

dependency 부분에 위의 그림과 같이 ID, version등의 필수 요소를 구문에 맞게 설정해서 넣으면 자동으로 설치가되고 적용이 된다. 언제나 필요한 것을 아주 간편하고 빠르게 설치할 수 있다.

 

mysql table 생성

mysql table은 다양한 방식으로 생성할 수 있다.

필자는 db4free를 이용해서 table을 만들어 보았다.

db4free를 이용해서 myadmin에 로그인한다.
코드로 만들거나 GUI를 이용해서 쉽게 Table을 제작할 수 있다.

root-context.xml에 DB 연결

root-context.xml에 해당하는 코드를 넣어서 DB에 연결한다.

sql db를 root-context에 등록했다.

<mybatis----> 부분은 Mybatis를 사용하기 위해서 sqlsessionfactory 커넥션을 추가한 것이다.

 

name space 설정을 아래와 같이 해준다.

db와 연결하기 위해 필수적인 namespace들을 설정한다.

db연결을 위한 설정파일 생성하기

src/main/resources -> mybatis-config.xml 생성

mybatis 설정파일 DTD 선언

src/main/resources -> 폴더 생성 -> board-Mapper.xml 생성

 

VO(DTO) 파일 작성

 

VO(Value Object): DB정보를 객체화 시킴.

 DTO(Data Transfer Object) : 외부시스템과 데이터를 주고 받음

 

패키지를 하나 더 만들고 VO.java 클래스를 만들고

아래에 해당하는 변수들을 만들고 getter & setter를 만들어서 VO 역할을 하게 한다.

DB를 자체적으로 만드는 것이라고 생각하면 이해가 쉽다.

변수, getter and setter, to string 까지 만들어 주면 된다. 

mapper.xml 작성

위에서 만든 board-Mapper.xml 파일에 다음과 같은 코드를 추가한다.

Mapper안에 있으면서 게시물작성, 게시물 읽기, 수정, 삭제, 전체 글 목록 등의 기능을 수행한다.

<?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="com.myp.mapper.BoardMapper">

 

  <insert id="create">

 insert into tbl_board (title, content, writer) 

 values(#{title},#{content}, #{writer})

 </insert>

 

 <select id="read" resultType="com.myp.domain.BoardVO">

 select 

   bno, title, content, writer, regdate, viewcnt 

 from 

   tbl_board 

 where bno = #{bno}

 </select>

 

 <update id="update">

 update tbl_board set title =#{title}, content =#{content} 

 where bno = #{bno}

 </update>

 

 <delete id="delete">

 delete from tbl_board where bno = #{bno}

 </delete>

 

 <select id="listAll" resultType="com.myp.domain.BoardVO">

 <![CDATA[

 select 

   bno, title, content, writer, regdate, viewcnt 

 from 

   tbl_board 

 where bno > 0 

 order by bno desc, regdate desc

 ]]>  

 </select>

 </mapper>

DAO 파일 작성

DAO(Data Access Object)

Service<-> DAO <-> Mapper : 데이터 관계

 

Service 파일 작성

 

jsp 파일, controller 작성