기록해! 정리해!

다중 mapper 설정하기 본문

SpringBoot

다중 mapper 설정하기

zsuling 2022. 10. 11. 16:21

[방법1. application.properties 를 이용하는 방법 ]

 
####  다중 mapper 설정하기
mybatis.mapper-locations=classpath:mapper/**/*.xml​


[ 방법 2 . 클래스를 만들어 사용하는 방법 ]

package com.majustory;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@MapperScan(basePackages="com.majustory")
@EnableTransactionManagement
public class DatabaseConfig {
 
	 @Bean
	 public  SqlSessionFactory  sqlSessionFactory(DataSource dataSource )  throws Exception {
		 
			  SqlSessionFactoryBean sqlSessionFactory = new  SqlSessionFactoryBean();
			  sqlSessionFactory.setDataSource(dataSource);
			  
			  PathMatchingResourcePatternResolver resolver = new  PathMatchingResourcePatternResolver();
			  sqlSessionFactory.setMapperLocations(resolver.getResource("classpath:mapper/Board-Mapper.xml")); 
		  
		  return sqlSessionFactory.getObject();  
	  
	 }
 

	 @Bean
	 public  SqlSessionTemplate  sqlSessionTemplate(SqlSessionFactory  sqlSessionFactory) throws Exception{  
		 
		 	final  SqlSessionTemplate  sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
		 	
		  return sqlSessionTemplate;  
		  
	 }
}​


 

Comments