본문 바로가기
☕Java/Spring

[20210723] Spring + MyBatis를 이용한 로그인 및 게시판 6 - spring-app.xml, spring-controller.xml, SqlMapconfig.xml

by 캔 2021. 7. 23.

db.properties는 데이터베이스 접속을 위한 정보들(드라이버, url, 사용자 이름, 비밀번호)를 담고 있는 파일이고 이전 프로젝트에서 다룬 적이 있으므로 생략한다.

두 개의 DTO를 위해 SqlMapConfig.xml에서도 alias와 mapper를 각각 등록해준다.

<!-- SqlMapConfig.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>
 	<typeAliases>
 		<typeAlias type="customer.dto.BoardDTO" alias="bbs" />
 		<typeAlias type="customer.dto.CustomerDTO" alias="customer" />
 	</typeAliases>
 	<mappers>
 		<mapper resource="customer/resource/boardMapper.xml" />
 		<mapper resource="customer/resource/customerMapper.xml" />
 	</mappers>
 </configuration>

spring-app.xml에는 css나 js파일을 저장하는 resources 폴더 사용을 위해 mvc:resources 태그를 사용했다.

<!-- spring-app.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
		
		<mvc:annotation-driven />
		<mvc:default-servlet-handler />
		<mvc:resources location="/resources/" mapping="/resources/**" />
		<mvc:view-resolvers>
			<mvc:jsp prefix="/views/" suffix=".jsp"/>
		</mvc:view-resolvers>
</beans>

spring-controller에는 DAO에서 설명했듯이 dao bean과 sqlsession bean이 DAO의 애너테이션으로 대체되었기 때문에 없어졌다.

<!-- spring-controller.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
		
		<context:component-scan base-package="customer.*" />
		<bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
			<property name="location" value="/WEB-INF/db.properties"/>
		</bean>
		<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
			<property name="driverClass" value="${driver}" />
			<property name="url" value="${url}" />
			<property name="username" value="${username}" />
			<property name="password" value="${password}" />
		</bean>
		<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="dataSource" />
			<property name="configLocation" value="/WEB-INF/SqlMapConfig.xml" />
		</bean>
		<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
			<constructor-arg ref="sqlSessionFactoryBean" />
		</bean>
</beans>