[Spring]Spring - MyBatis 연동하기.

http://blog.naver.com/sorlove0?Redirect=Log&logNo=20190380263



    필요한 라이브러리.


-pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
          <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>
 
        <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.3</version>
        </dependency>
        
        <!-- MyBatis-Spring 연동 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.1</version>
        </dependency>
        
        <!-- Spring Tranjection -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        
        <!-- commons dbcp -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        
        <!-- Spring jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>




- 스프링 빈 관리 설정파일에 다음 코드를 추가한다.

- root-context.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    <!-- Scan all annotations exclude controller -->
    <!-- 컨트롤러 어노테이션을 제외한 모든 컴포넌트 어노테이션을 스캔. -->
    <context:component-scan base-package="com.exam.*">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- Transaction used annotation.-->
   <!-- 트랙잭션 처리를 어노테이션으로 사용.-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!-- 데이터소스 -->
   <!-- db에 접속하기 위한 커넥션 설정.-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/project" />
        <property name="username" value="root" />
        <property name="password" value="java1234" />
    </bean>
    
    <!-- 트랜잭션 관리자 -->
   <!-- 위의 트랜잭션 어노테이션 처리할때 이름을 transactionManager로 설정하면 다음 코드를 
          쓸 필요 없다.-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 마이바티스 설정 -->
   <!-- 커넥션 설정으로 db에 직접 접속하는 sqlSession을 만드는 팩토리를 생성.-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="mybatis.test" />
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/mybatis-config.xml"/>
        <property name="mapperLocations">
            <array>
                <value>classpath:/*.xml</value>
            </array>
        </property>        
    </bean>
    
   <!-- 팩토리로 sqlSession을 생성.-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    


마이바티스 설정파일에 매퍼 파일을 등록할 필요 없이 여기서 바로 등록 가능하다.

댓글()