ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] 게시판 만들기(12) - Mybatis 연동
    Spring/4.3.x - 게시판 만들기 2018. 12. 14. 10:35
    반응형

     SQL문을 별도의 XML 파일로 관리하여 개발 및 유지 보수가 용이한 MyBatis를 설정하는 작업을 진행하도록 하겠습니다.

     

    1. POM.xml에 의존성 추가

    Spring-jdbc, Spring-test, Mybatis 의존성을 추가하세요.

    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
    <!-- Spring -->
    <org.springframework-version>4.3.6.RELEASE</org.springframework-version>
     
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
     
    <!-- MyBatis -->
    <mybatis.version>3.4.6</mybatis.version>
    <mybatis.spring.version>1.3.2</mybatis.spring.version>    
     
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>        
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis.spring.version}</version>
    </dependency>
    cs

     

    2. root-context.xml 파일 수정

    MySQL과의 연결을 담당하는 DataSource를 추가하세요.

    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
    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        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-4.3.xsd
            http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.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-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
        
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/board?allowPublicKeyRetrieval=true&amp;useSSL=false&amp;serverTimezone=UTC" />
            <property name="username" value="board" />
            <property name="password" value="password1!" />
        </bean>
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation" value="classpath:mybatis-config.xml" />
        </bean>
    </beans>
     
    cs

     

    3.​ src/main/resources 패키지에 mybatis-config.xml 생성

    mybatis-config.xml

    1
    2
    3
    4
    5
    6
    7
    <?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>
     
    </configuration>
    cs
     

     

    4. 로그 확인

    4_1. DataSourceTest.java, MyBatisTest.java 파일을 추가한 후에 JUnit으로 실행하세요. 로그가 확인되면 정상적으로 Mybatis가 연동된 것입니다.

    DataSourceTest.java

    더보기
    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
    package com.spring.board;
     
     
    import java.sql.Connection;
     
     
    import javax.inject.Inject;
    import javax.sql.DataSource;
     
     
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/*.xml" })
    public class DataSourceTest {
     
     
        protected final Logger logger = LoggerFactory.getLogger(this.getClass());
        
        @Inject
        private DataSource dataSource;
     
     
        @Test
        public void dataSourceConnectionTest() throws Exception {
            
            Connection connection = null;
            
            try {
                connection = dataSource.getConnection();
                logger.debug("connection : [{}]", connection);
            } catch (Exception e) {
                e.getMessage();
            }
        }
    }
    cs

     

    MyBatisTest.java

    더보기
    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
    package com.spring.board;
     
    import javax.inject.Inject;
     
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/*.xml" })
    public class MyBatisTest {
     
        protected final Logger logger = LoggerFactory.getLogger(this.getClass());
     
        @Inject
        private SqlSessionFactory sqlSessionFactory;
     
        @Test
        public void MyBatisSessionTest() throws Exception {
     
            SqlSession sessios = null;
     
            try {
     
                logger.debug("sqlSessionFactory : [{}]", sqlSessionFactory);
     
                sessios = sqlSessionFactory.openSession();
     
                logger.debug("sessios : [{}]", sessios);
     
            } catch (Exception e) {
     
                e.printStackTrace();
            }
        }
    }
    cs

     

    소스 코드는 Github Repository - https://github.com/tychejin1218/board_v1 (branch : section12) 를 참조하세요.
    Github에서 프로젝트 가져오기 - https://tychejin.tistory.com/33

     

     

    반응형

    댓글

Designed by Tistory.