일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- SI
- 슈샤이어 떠돌이상인
- HWP
- PIP모드
- 토토이크 떠돌이상인
- 페이튼 떠돌이상인
- SE
- zoom
- 베른 떠돌이상인
- 이름바꾸기
- https
- 가로세로세팅
- 애니츠 떠돌이상인
- IT용어
- 욘 떠돌이상인
- 루테란 떠돌이상인
- 작은화면
- 한글
- SSL
- 유튜브
- 홈페이지제작견적
- 아르데타인 떠돌이상인
- 파푸니카 떠돌이상인
- 로헨델 떠돌이상인
- sm
- 3
- 특정페이지가로로
- Today
- Total
도담도담
Spring에서 xml 파일들의 역할 본문
web.xml : 요청이랑 상관없이 미리 등록
root-context.xml : 미리등록 해놓아야 할 객체들
mybatis-config.xml : 매핑 객체 및 SQL 매퍼파일
Mapper.xml : SQL 쿼리
servlet-context.xml : 요청이 들어왔을 때 등록해 놓아야 할 객체들
pom.xml : Maven 설정
web.xml
1. 사전 등록
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
root-context.xml
db.properties 가 있어야 밑에 애들이 미리 등록된다.
<bean id="dataSource"
<bean id="transactionManager"
<bean id="sqlSessionFactory"
<bean id="sqlSession"
2. 요청 시 등록
<servlet>
(3) <servlet-name>appServlet</servlet-name>
(4) <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <== 생성등록 시작
<init-param>
<param-name>contextConfigLocation</param-name>
(5) <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> <==완성
</init-param>
<load-on-startup>1</load-on-startup>
(4) </servlet> <== 생성등록 끝
웹 브라우저로부터 요청사항이 들어오면 여기 밑에가 먼저 실행
<servlet-mapping>
(2) <servlet-name>appServlet</servlet-name>
(1) <url-pattern>/</url-pattern> <==이 중에서도 제일 먼저 실행
</servlet-mapping>
servlet-context.xml
<annotation-driven /> 로 @ 붙은 클래스를 다 찾는다
@Controller : "com.lsj.biz.controller"
@Repository : "com.lsj.biz.repository" <==객체가 먼저 등록
@들의 실제 컨트롤러 위치는 다르지만,
<context:component-scan base-package="com.lsj.biz" />
덕분에 하위 패키지들까지 한꺼번에 검색이 가능하다.
@Repository
public class TodoRepository {
@Autowired
SqlSession sqlSession;
~~
}
@Controller
public class TodoController {
@Autowired
TodoRepository repo;
~~
@RequestMapping(~~~)
}
mybatis-config.xml
1. SQL 쿼리를 선언한 Mapper 에서 데이터를 자동 매핑할 수 있도록 VO(DTO) 객체 설정
아래와 같이 여러개의 VO를 설정할 수 있다.
<typeAliases>
<typeAlias type="VO클래스에 대한 풀네임" alias="Mapper에서 사용할 이름"></typeAlias>
<typeAlias type="VO클래스에 대한 풀네임" alias="Mapper에서 사용할 이름"></typeAlias>
</typeAliases>
2. DataSource 에 관한 설정 (DI 방법에 따라 선택. 즉 생략 가능)
<environments default="development">
<!-- 아래의 <environment~~></environment> 는 여러개의 DB접속 내용을 추가로 설정할 수 있다. -->
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
3. Mapper 파일의 위치 설정
여러개의 Mapper 파일을 설정 할 수 있는데, 단, 각 Mapper 파일 내부의 namespace는 전체 프로젝트에서 유일한 값.
<mappers>
<mapper resource="Mapper 파일의 위치/파일" />
<mapper resource="Mapper 파일의 위치/파일" />
</mappers>
Mapper.xml
<mapper namespace="프로젝트 내에서 유일한 이름으로 설정">
<select id="해당 Mapper에서 유일한 이름" resultType="config.xml에서 설정한 이름(alias)">
SELECT * FROM todo <!-- 여기서 쿼리작성 시 주의사항 : 절대 ;를 사용하면 안된다. -->
</select>
<select id="해당 Mapper에서 유일한 이름" parameterType="객체, 기본데이터형" resultType="config.xml에서 설정한 이름(alias)">
SELECT * FROM todo <!-- 여기서 쿼리작성 시 주의사항 : 절대 ;를 사용하면 안된다. -->
</select>
<insert id="필수">
</insert>
<update id="필수">
</update>
<delete id="필수">
</delete>
</mapper>
각 태그 (엘리먼트) 내에서는 조건을 이용한 분기 작업할 수 있는 <if ~~></if> 를 사용할 수 있다..
<if test="조건"> 쿼리문 </if>
예) 데이터 수정 시, 조건에 따른 수정값이 변경 되어야 할 경우,
<update id="updateTodo" parameterType="Todo">
update todolist set
<if test= type=="'TODO'">
type='DOING'
</if>
<if test= type=="'DOING'">
type='DONE'
</if>
where
id=#{id}
</update>
DispatcherServlet
xml문서들의 내용들을 전달받고 전달해주는 Servlet
http://localhost:9000/biz/
톰캣 서버주소 ==> http://localhost:9000
톰캣 서버 내부의 프로젝트(즉, 컨테이너) ==> /biz
실제 요청사항 문자열 ==> "/" ==> DispatcherServlet 에게 전달
DispatcherServlet
1. "/"와 동일한 값을 갖고 있는 메서드 검색.. (없으면 => 404)
2. HomeController =>
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "main";
}
3. "main" 이라는 문자열을 DispatcherServlet 에게 전달
4. DispatcherServlet은 ViewResolver 객체를 찾고
ViewResolver에게 "main" 에 대한 구체적인 내용을 문의
5. ViewResolver 는
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
을 이용하여
"/WEB-INF/views/main.jsp" 라고 DispatcherServlet에게 응답
6. DispatcherServlet는 웹브라우저에게 "/WEB-INF/views/main.jsp" 와 같이 응답
7. 웹 브라우저는 "/WEB-INF/views/main.jsp" 내용을 달라고 다시 요청
8. DispatcherServlet은 /WEB-INF/views/ 에 있는 main.jsp 파일을 컴파일 하여 html 형태로 웹 브라우저에게 응답 처리!!
9. 웹브라우저는 응답받은 html 형태의 문서를 HTML 엔진을 통해 컴파일 하여 사용자에게 출력
'IT 공부 > KH 정보교육원' 카테고리의 다른 글
Lombok 라이브러리의 기본 어노테이션 요약 (0) | 2021.07.02 |
---|---|
Mybatis에서 SQL 쿼리를 실행하는 방법 (0) | 2021.07.01 |
Spring에서 패키지별 역할 (0) | 2021.06.30 |
Spring + myBatis 연동하기 (0) | 2021.06.29 |
myBatis 콘솔 출력 결과에 대한 설명 (0) | 2021.06.29 |