본격적인 Spring 개발에 앞서 몇가지 환경세팅이 필요하다.
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/res/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/" />
<beans:property name="suffix" value=".jsp" />
resources (정적인 html문서 같은 웹 리소스들의정보를 기술하는 태그)의 값을 res 로 바꾼다. (명령어 축약을 위해)
<beans:bean class...> (Controller가 Model를 리턴하고 DispatcherServlet이 jsp 파일을 찾을 때 쓰이는 정보를 기술하는 태그)
에 /WEB-INF/view에서 view 삭제. (경로 설정시 편의를 위해)
프로젝트를 처음 생성할 때 web.xml에 명시된 DispatcherServlet 설정 부분은 다음과 같다.
web.xml
<!-- encoding -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
메이븐은 자바 프로젝트를 관리하는 툴로, 미리 작성된 xml 파일을 이용하여 라이브러리를 자동으로 다운로드하거나 프로젝트를 빌드해준다. pom.xml은 간단히 말하면 Maven의 빌드 정보를 담고 있는 파일로, POM(Project Object Model)을 설정하는 부분으로 프로젝트 내 빌드 옵션을 설정하는 부분이다.
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!-- mybatis spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
root-context는 공통 bean을 모은 공간이다. 프로젝트의 어플리케이션 영역 설정하고 스프링 MVC 설정과 관련된 여러 처리를 담당한다. JSP와는 관련없는 객체(Bean)을 설정 하고, View와 밀접하지 않은 정보를 기술하는 xml 파일이다.
<!-- Root Context: defines shared resources visible to all other web components -->
<!--1.datasource 설정: 드라이버 관련-->
<!-- Database Connection을 생성 -->
<!-- DB 계정 이름과 패스워드 입력 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/sist?serverTimezone=Asia/Seoul"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property> <!-- sql연결 -->
</bean>
<!--2.sqlSessionFactory 설정-->
<!-- classpath: 빌드시 컴파일된 class 파일들의 위치 경로 -->
<!-- <property> 태그 -->
<!--
SqlSessionFactoryBean 클래스에서 선언된
dataSource, configLocation, mapperLoations
변수들의 값을 할당 받는다!!
-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- config 경로 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml" /> <!-- mapper 경로 -->
</bean>
<!-- <constructor-arg> -->
<!-- sqlSession bean에 sqlSessionFactory 객체 주입-->
<bean id="sqlSession"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<!-- transactionManager에 dataSource 객체 주입-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>