본문 바로가기
CLASS/SPRING,JSTL

별도의 config, mapper를 사용하는 방식

by hingu 2024. 7. 24.

- dbconfig.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"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
		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">
	
	<context:annotation-config />
	<bean id="dbInfo" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/cms"></property>
		<property name="username" value="hana"></property>
		<property name="password" value="hana1234"></property>
	</bean>
	
	<!-- 1 -->
	<bean id="sqlfact" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dbInfo"></property>
		<property name="configLocation" value="classpath:/META-INF/config.xml"></property>
		<property name="mapperLocations" value="classpath:/META-INF/mapper.xml"></property>
	</bean>
	
	<bean id="template" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close">
		<constructor-arg name="sqlSessionFactory" ref="sqlfact"></constructor-arg>
	</bean>
	
	<!-- 2. 기존거에서 걍 이렇게 추가하면 됨 -->
	<bean id="sqlfact2" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dbInfo"></property>
		<property name="configLocation" value="classpath:/META-INF/config2.xml"></property>
		<property name="mapperLocations" value="classpath:/META-INF/mapper2.xml"></property>
	</bean>
	
	
	<bean id="template2" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close">
		<constructor-arg name="sqlSessionFactory" ref="sqlfact2"></constructor-arg>
	</bean>
</beans>

 

🔽

mapper2.xml , config2.xml 새로 생성