Published 2020. 11. 19. 20:52
728x90
반응형

스프링에서 MyBatis 연동하기

 

1. 모듈추가

<!-- MyBatis -->

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>${org.springframework-version}</version>
</dependency>

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.6</version>
</dependency>

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.2</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>${org.springframework-version}</version>
</dependency>

 

 

2.SqlSessionFactoryBean과 트랜잭션 관리자 설정

SqlSessionFactoryBean으로 mybatis의 SqlSessionFactory를 생성

<?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.xsd">

  <!-- mybatis 빈 등록 : mybatis 관련 설정파일은 target/classes에서 참조한다.-->
  <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
  
    <!-- ref속성으로 dataSource빈을 참조한다. -->
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
    <property name="mapperLocations" value="classpath*:mapper/**/*-mapper.xml" />
  </bean>

  <!-- sqlSessionTemplate 빈 등록 -->
  <bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSessionTemplate">
  
    <!-- SqlSessionTemplate에 기본생성자가 없기때문에 기본생성자를 호출해줘야함. -->
    <constructor-arg index="0" ref="sqlSessionFactoryBean" />
  </bean>
  
</beans>

 

 

 

 

 

 

반응형

'프로그래밍 > Spring' 카테고리의 다른 글

11.24(스프링 MVC 처리 흐름)  (0) 2020.11.24
11.23(MVC1 패턴과 MVC2 패턴)  (0) 2020.11.23
11.18(DispatcherServlet과 web.xml 설정)  (0) 2020.11.18
11.17(MVC 뷰 구현)  (0) 2020.11.17
11.09(Spring Security - 사용자 인증)  (0) 2020.11.09
복사했습니다!