-
[Spring] 게시판 만들기(12) - Mybatis 연동Spring/4.3.x - 게시판 만들기 2018. 12. 14. 10:35반응형
SQL문을 별도의 XML 파일로 관리하여 개발 및 유지 보수가 용이한 MyBatis를 설정하는 작업을 진행하도록 하겠습니다.
1. POM.xml에 의존성 추가
Spring-jdbc, Spring-test, Mybatis 의존성을 추가하세요.
12345678910111213141516171819202122232425262728<!-- 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를 추가하세요.
12345678910111213141516171819202122232425<?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.xsdhttp://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://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&useSSL=false&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
1234567<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//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
더보기123456789101112131415161718192021222324252627282930313233343536373839404142package 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());@Injectprivate DataSource dataSource;@Testpublic void dataSourceConnectionTest() throws Exception {Connection connection = null;try {connection = dataSource.getConnection();logger.debug("connection : [{}]", connection);} catch (Exception e) {e.getMessage();}}}cs MyBatisTest.java
더보기1234567891011121314151617181920212223242526272829303132333435363738394041package 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());@Injectprivate SqlSessionFactory sqlSessionFactory;@Testpublic 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반응형'Spring > 4.3.x - 게시판 만들기' 카테고리의 다른 글
[Spring] 게시판 만들기(14) - 게시글 조회(SELECT) (28) 2018.12.17 [Spring] 게시판 만들기(13) - mapper 및 json 설정 (3) 2018.12.14 [Spring] 게시판 만들기(11) - 인터셉터(Interceptor) 설정 (5) 2018.12.14 [Spring] 게시판 만들기(10) - log4j->logback으로 변경 (7) 2018.12.13 [Spring] 게시판 만들기(9) - MySQL 연동 (11) 2018.12.04