본문 바로가기

개발/Spring

스프링 마이바티스 연동

728x90
반응형
스프링 마이바티스 연동 XML

 

스프링 기반 웹 애플리케이션을 개발할 때 마이바티스는 데이터베이스 연동 기능을 담당하기 때문에 스프링에서는 간단한 설정만으로 쉽게 마이바티스를 사용할 수 있습니다.

 

[ web.xml ]

 

...
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			<!-- 마이바티스 설정파일 읽기 -->
			/WEB-INF/config/mybatis.xml
			/WEB-INF/config/service.xml
		</param-value>
	</context-param>
...

 

 

[ /config/mybatis.xml ]

 

<?xml version="1.0" encoding="UTF-8"?>
<beans
	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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans">

	<!-- 데이터베이스 설정 관련 정보를 jdbc.properties 파일에서 읽기 -->
	<bean id="propertyPlaceholderConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>/WEB-INF/config/jdbc.properties</value>
		</property>
	</bean>

	<!-- dataSource 빈 생성 -->
	<bean id="dataSource"
		class="org.apache.ibatis.datasource.pooled.PooledDataSource">
		<property name="driver" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- dataSource 속성에 dataSource 빈 설정 -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- configLocation 속성에 Config.xml 설정 -->
		<property name="configLocation"
			value="classpath:mybatis/model/modelConfig.xml" />
		<!-- mapperLocations 속성에 mybatis/mappers 패키지의 모든 매퍼 파일들을 읽어옴 -->
		<property name="mapperLocations"
			value="classpath:mybatis/mappers/*.xml" />
	</bean>

	<!-- sqlSessionTemplate 클래스를 이용해 sqlSession 빈 생성 -->
	<bean id="sqlSession"
		class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
	</bean>


	<bean id="memberDAO" class="com.spring.member.dao.MemberDAOImpl">
		<!-- sqlSession 빈을 DAO 빈에 주입 -->
		<property name="sqlSession" ref="sqlSession"></property>
	</bean>

</beans>

 

 

[ /config/service.xml ]

 

...
    <bean id="memberService"
		class="com.spring.member.service.MemberServiceImpl">
		<!-- DAO 빈을 service 빈 속성에 주입 -->
		<property name="memberDAO" ref="memberDAO" />
	</bean>
...

 

 

[ /mybatis/model/Config.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> 태그로 마이바티스에서 데이터 전달에 사용할 VO 빈 설정 -->
	<typeAliases>
		<typeAlias type="com.spring.member.vo.MemberVO" alias="memberVO" />
	</typeAliases>
</configuration>

 

 

여기까지 임미다.

728x90