๊ธฐ์ด or ๊ถ๊ธํ๋ ๊ฒ์ ์ ๋ฆฌํ๋ ๊ณต๊ฐ.
- Project Object Model์ ์ค๋ง
- maven์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด XMLํ๊ทธ ํํ๋ก ์ ๊ณต์ด ๋๋๋ฐ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํ๊ทธ๋ฅผ pom.xml์ ๋ฃ์ด์ฃผ๋ฉด ์๋์ผ๋ก dependencies์ *.jar ์ถ๊ฐ
-
DispatherServlet์ ์ค์ ์ ์น ์ดํ๋ฆฌ์ผ์ด์ ์ /WEB-INF/web.xml ํ์ผ์ ์ถ๊ฐํ๋ฉฐ, ์๋ธ๋ฆฟ๊ณผ ๋งคํ ์ ๋ณด๋ฅผ ์ถ๊ฐํ๋ฉด DispatherServlet ์ค์ ์ด ์๋ฃ๋๋ค.
-
web.xml์๋ธ๋ฆฟ ํด๋์ค๋ JSP ํ์ด์ง์ ๋ฌ๋ฆฌ ์ค์น๋ฟ๋ง ์๋๋ผ ๋ฑ๋ก์ ํ๋ ๊ณผ์ ์ ํ์๋ก ํ๋ค. ์ฌ๊ธฐ์ ์๋ธ๋ฆฟ ํด๋์ค๋ฅผ ๋ฑ๋กํ๋ ๊ณณ์ ์ด๋ฆ์ Web application deployment descriptor๋ผ๊ณ ํ๋๋ฐ (์ค์ฌ์ DD-Deployment Descriptor) ์ด ์ญํ ์ ํ๋ ๊ฒ์ด ๋ฐ๋ก web.xml์ด๋ค. web.xml ํ์ผ์ ์น ์ ํ๋ฆฌ์ผ์ด์ ๋๋ ํฐ๋ฆฌ๋ง๋ค ๋ฑ ํ๋์ฉ๋ง ์กด์ฌํ ์ ์๋ค. -
DD๋ WAS ๊ตฌ๋ ์ /WEB-INF ๋๋ ํ ๋ฆฌ์ ์กด์ฌํ๋ web.xml์ ์ฝ์ด ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์ค์ ์ ๊ตฌ์ฑํ๊ธฐ ์ํด ์กด์ฌํ๋ค.
-
web.xml์ ์์ฑํ๊ธฐ ์ํด์ ์ฐ์ ๋ฃจํธ ์๋ฆฌ๋จผํธ์ธ
<web-app></web-app>์ ๋ง๋ ๋ค. -
์ด ์์ ์น ์๋ฒ๊ฐ ์น ๋ธ๋ผ์ฐ์ ๋ก๋ถํฐ URL์ ๋ฐ์์ ๋ ์๋ธ๋ฆฟ ํด๋์ค๋ฅผ ์ฐพ์์ ํธ์ถํ๊ธฐ ์ํด ํ์ํ ์ ๋ณด๋ฅผ ๊ธฐ๋กํด์ผ ํ๋ค.
-
URL๊ณผ ์๋ธ๋ฆฟ ํด๋์ค์ ์ด๋ฆ์ ๊ฐ๊ฐ
<servlet><servlet-class></servlet-class></servlet>ํ๊ทธ์<servlet-mapping><url-pattern></url-pattern></servlet-mapping>๋ก ์์ฑ
<web-app>
<servlet>
<servlet-name>
//์๋ธ๋ฆฟ์ ์ด๋ฆ
</servlet-name>
<servlet-class>
//์๋ธ๋ฆฟ ํด๋์ค์ ์ด๋ฆ
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
//์ฐธ์กฐํ ์๋ธ๋ฆฟ์ ์ด๋ฆ
</servlet-name>
<url-pattern>
//mappingํ URL
</url-pattern>
</servlet-mapping>
</web-app><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>- ContextLoaderListener ํด๋์ค๋ ์คํ๋ง ์ค์ ํ์ผ(๋ํดํธ์์ ํ์ผ๋ช applicationContext.xml)์ ๋ก๋ํ๋ฉด ServletContextListener ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ ์๊ธฐ ๋๋ฌธ์ ServletContext ์ธ์คํด์ค ์์ฑ ์(ํฐ์ผ์ผ๋ก ์ดํ๋ฆฌ์ผ์ด์ ์ด ๋ก๋๋ ๋)์ ํธ์ถ๋๋ค. ์ฆ, ContextLoaderListener ํด๋์ค๋ DispatcherServlet ํด๋์ค์ ๋ก๋๋ณด๋ค ๋จผ์ ๋์ํ์ฌ ๋น์ฆ๋์ค ๋ก์ง์ธต์ ์ ์ํ ์คํ๋ง ์ค์ ํ์ผ์ ๋ก๋ํ๋ค.
<param-name>contextConfigLocation</param-name>: ๋น ์ค์ ํ์ผ์ ํ๋ ์ด์์ ์ฌ์ฉํ๊ฑฐ๋, ํ์ผ ์ด๋ฆ๊ณผ ๊ฒฝ๋ก๋ฅผ ์ง์ ์ง์ ํด์ฃผ๊ณ ์ถ๋ค.
<mvc:annotation-driven/>: ์ด๋ ธํ ์ด์ ์ฌ์ฉ์ ๋์์ค<bean:component-scan base-package="com.example2"/>: autowiredํ ํจํค์ง ๊ฒฝ๋ก- DispatherServlet์ ์ด ๋ทฐ ์ด๋ฆ๊ณผ ๋งค์นญ๋๋ ๋ทฐ ๊ตฌํ์ฒด๋ฅผ ์ฐพ๊ธฐ ์ํด ViewResolver๋ฅผ ์ฌ์ฉํ๋ค.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>- DB ํ ์ด๋ธ ์์ฑ
- VO ํด๋์ค ์ค๊ณ (domain)
- DAO ์ธํฐํ์ด์ค ์์ฑ - ์คํํด์ผ ํ๋ ๊ธฐ๋ฅ์ ์ธํฐํ์ด์ค๋ก ์ ์
- XML Mapper์ ์์ฑ๊ณผ ํ์ํ DTD ์ถ๊ฐ ๋ฐ SQL๋ฌธ ์์ฑ
- MyBatis์์ ์์ฑ๋ XML Mapper๋ฅผ MyBatis๊ฐ ๋์ํ ๋ ์ธ์ํ๋๋ก ์ค์
- DAO์ธํฐํ์ด์ค ๊ตฌํํ๋ DAO ํด๋์ค ์์ฑ - SqlSessionTemplate ์ด์ฉ
-
๊ฐ์ฒด์งํฅ ์ธ์ด์ธ ์๋ฐ์ ๊ด๊ณํ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ํ๋ก๊ทธ๋๋ฐ์ ์ข๋ ์ฝ๊ฒ ํ ์ ์๊ฒ ๋์์ฃผ๋ ๊ฐ๋ฐ ํ๋ ์์ํฌ
-
์๋ฐ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ๋ก๊ทธ๋๋ฐ์ ํ๊ธฐ ์ํด JDBC(์๋ฐ์์ ์ ๊ณตํ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ๋ก๊ทธ๋๋ฐ API)๋ฅผ ์ ๊ณต
-
JDBC๋ ๊ด๊ณํ ๋ฐ์ดํฐ ๋ฒ ์ด์ค๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๋ค์ํ API๋ฅผ ์ ๊ณต
-
๋ค์ํ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ง์ํ๊ธฐ ์ํด JDBC๋ ์ธ๋ถ์ ์ธ ์์ ์ด ๊ฐ๋ฅํ๊ฒ ์์ ๋ณ๋ก ๊ฐ๊ฐ์ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ฒ๋๋ค. ์ด๋ฌํ ์ฌํญ๋ค์ ๋ค์์ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ ๊ด๋ จ๋ ๊ฐ์ฒด๋ฅผ ํด์ ํด์ผํ๋ ๋จ์ ์ด ์กด์ฌ
-->Mybatis๋ JDBC๋ณด๋ค ์ข๋ ํธํ๊ฒ ์ฌ์ฉํ๊ธฐ ์ํด ๊ฐ๋ฐ๋์์
-
SqlSessionFactoryFactory ํด๋์ค : SqlSession์ ๋ง๋๋ ์ญํ
-
Dao๋ Factory์ ๋ฉค๋ฒ๋ก ์ ์งํ๋ฉด์ ํ์ํ ๋ SqlSession์ openํด์ ์ฌ์ฉํ๊ณ ๋ค์ฐ๋ฉด sqlSession๋ฅผ close
-
SqlSessionํด๋์ค : sql๋ฌธ์ ์ค์ ํธ์ถํด์ฃผ๋ ์ญํ (ํ์ํ ๋ openํ๊ณ closeํด์ค์ผํจ)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="mapperLocations" value="classpath:mybatis/mapper/**/*.xml"/>
</bean>- XML Mapper๋ฅผ ์์ฑํ ๋๋ namespace๋ผ๋ ๊ฒ์ ์ค์ ์ ์กฐ์ฌํด์ผํ๋ค. ํด๋์ค์ ํจํค์ง์ ์ ์ฌํ ์ฉ๋๋ก, MyBatis ๋ด์์ ์ํ๋ SQL ๋ฌธ์ ์ฐพ์์ ์คํํ ๋ ๋์ํ๋ ๊ฒ์ด๋ค
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.repository.BoardRepository">
<select id="findAll" resultType="com.example.domain.Board">
SELECT * from Board;
</select>
</mapper>- ํ๋์ ์์ฒญ์์ HttpServletRequest ๊ฐ์ฒด๊ฐ ์๋ฉธํ๊ธฐ ๊น์ง ์ํ์ ๋ณด๋ฅผ ์ ์งํ๊ณ ์ ํ ๋, ํ๋ฒ์ ์์ฒญ์ผ๋ก ์คํ๋ ํ์ด์ง๋ผ๋ฆฌ ์ ๋ณด๋ฅผ ๊ณต์ ํ๊ณ ์ ํ ๋ ์ฌ์ฉ๋๋ฉฐ, ๋์คํจ์ฒ์ ์ํ ์์ฒญ์ฌ์ง์ ์ ํ๊ธฐ ์ HttpServletRequest ๊ฐ์ฒด์ setAttribute( ) ๋ฉ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฑ๋กํ๊ณ ์์ฒญ ์ฌ์ง์ ์ผ๋ก HttpServletRequest ๊ฐ์ฒด๊ฐ ์ ๋ฌ๋ ํ์ด์ง์์ getAttribute( ) ๋ฉ์๋๋ก ์ถ์ถํ ์ ์๋ค.