본문으로 바로가기

1. maven에 My Batise 라이브러리 추가

1
2
3
4
5
6
7
8
9
10
11
<!-- MyBatis 라이브러리 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.8</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.0</version>
</dependency>
cs

 

 

2. Eclipse의 Market Place에서 MyBatise 플러그인을 두가지 다 다운 받는다. 

 

 

3. configuration.xml 문서를 만든다

 

 

4. MyBatis 문서 DOCTYPE 을 복사 붙여 넣기 한다.

DOCTYPE 을 정의 하면 MyBatis 문법을 사용할 수 있다.

 

 

5. 같은 패키지에 MemberMapper.xml 문서를 만든다.

이 문서에는 sql 문이 적힐 예정이다.

 

 

6. configuration.xml 문서에 mapper를 설정해준다.

1
2
3
4
5
6
7
8
9
10
11
12
<?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>
    <!-- mapper 설정 -->
    <mappers>
        <mapper resource="com/gura/spring05/mybatis/MemberMapper.xml"/>
    </mappers>
</configuration>
 
cs

 

 

7. servlet-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
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
 
    <!-- 
        아래의 MyBatis 관련 설정은 Servers/context.xml 문서에
        
        <Resource maxWaitMillis="-1" maxIdle="10" maxTotal="20" 
        password="tiger" username="scott" 
        url="jdbc:oracle:thin:@127.0.0.1:1521:xe" 
        driverClassName="oracle.jdbc.OracleDriver" 
        type="javax.sql.DataSource" auth="Container" 
        name="jdbc/myoracle"/>
        
       위와 같은 DB 접속 정보가 있어야지 아래의 코드 동작한다.
     -->
    
    <beans:bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <beans:property name="jndiName" value="java:comp/env/jdbc/myoracle"/>
    </beans:bean>
    <beans:bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="configLocation" value="classpath:com/gura/spring08/mybatis/Configuration.xml"/>
    </beans:bean>
    <beans:bean class="org.mybatis.spring.SqlSessionTemplate">
        <beans:constructor-arg name="sqlSessionFactory" ref="sessionFactory"/>
    </beans:bean>
 
</beans:beans>
cs

 

위의 코드를 해석해보자

 

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
    <beans:bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <beans:property name="jndiName" value="java:comp/env/jdbc/myoracle"/>
    </beans:bean>
    <!-- 
        위의 설정을 JAVA code로 환산하면
        
        JndiObjectFactoryBean dataSource=new JndiObjectFactoryBean();
        dataSource.setJndiName("java:comp/env/jdbc/myoracle");
        
    -->
    
    <beans:bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="configLocation" value="classpath:com/gura/spring08/mybatis/Configuration.xml"/>
    </beans:bean>
    <!-- 
        위의 설정을 JAVA code로 환산하면
        
        JndiObjectFactoryBean dataSource=new JndiObjectFactoryBean();
        dataSource.setJndiName("java:comp/env/jdbc/myoracle");
        
        SqlSessionFactoryBean sessionFactory=new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource 의 참조 값을 전달);
        sessionFactory.setConfigLocation("classpath:com/gura/spring08/mybatis/Configuration.xml")
    -->
    
    <beans:bean class="org.mybatis.spring.SqlSessionTemplate">
        <beans:constructor-arg name="sqlSessionFactory" ref="sessionFactory"/>
    </beans:bean>
    <!-- 
        위의 설정을 JAVA code로 환산하면
        
        new SqlSessionTemplate(sessionFactory);
        **<beans:constructor-arg name="sqlSessionFactory" ref="sessionFactory"/>
        **생성자의 인자로 sessionFactory 참조값을 전달한다.
        
        new SqlSessionTemplate(sessionFactory);객체는 DB를 연동하기 위한 핵심 객체이다.
        **SqlSessionTemplate는 SqlSession 인터페이스를 implements되었다.
    -->
cs