진이의 Developer Story

(2) Spring+MyBatis 게시판 만들기 - DB 셋팅 본문

Java/Spring

(2) Spring+MyBatis 게시판 만들기 - DB 셋팅

JIN3260 2016. 1. 20. 13:29

(1) 데이터베이스 및 테이블 생성
여러가지 DBMS(MS-SQL, Oracle, MySQL)가 있지만 저는 MySQL를 사용해보겠습니다.
MySQL을 설치해주시고, Database와 Table을 생성해봅시다.

board테이블의 필드는 다음과 같습니다.

 Field

Type

Comment 

 idx

int(11) 

게시글 번호 

 title

varchar(100) 

게시글 제목 

 content

varchar(5000) 

게시글 내용 

 id

varchar(50) 

게시글 작성자 

 hit

int(11) 

조회수 

 date

datetime 

작성날짜 

// sample 데이터베이스를 생성한다.
create database sample;

// 사용할 DB를 sample로 지정한다.
use sample;

// sample DB에 board Table을 생성한다.
create table `sample`.board (
  `idx` int(11) AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `content` varchar(5000) NOT NULL,
  `id` varchar(50) NOT NULL,
  `hit` int(11) NOT NULL DEFAULT '0',
  `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`idx`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

(2) web.xml 셋팅
먼저 web.xml 파일을 수정해줍니다.
저는 context를 여러 개로 분리해서 쓰기 위해서 컨텍스트 파라미터를 다음과 같이 설정하였습니다.
/WEB-INF/spring 폴더내에 존재하는 "-context.xml" 로 끝나는 모든 파일을 읽겠다는 것입니다.
지금 작성하는 프로젝트 기준으로는 datasource-context.xml(MySQL Datasource)와 mapper-context.xml(MyBatis) 파일이 있습니다.

// web.xml
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/spring/*-context.xml</param-value>
</context-param>

(3) datasource-context.xml 셋팅
/WEB-INF/spring 폴더 밑에 datasource-context.xml 파일을 생성해줍니다.

<!--?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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">

    //MySQL DataSource
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver">
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/sample">
        <property name="username" value="root">
        <property name="password" value="password">
    </property></property></property></property></bean>

    //Oracle DataSource
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
        <property name="URL" value="jdbc:oracle:thin:@127.0.0.1:1521:sample">
        <property name="username" value="root">
        <property name="password" value="password">
    </property></property></property></property></bean>
</beans>

위와 같이 datasource 빈을 정의해줍니다.
아래의 오라클 DataSource는 예시로 적어놓은 것입니다. (MySQL을 사용시 오라클 DataSource는 제거해줍시다.
두 개의 같은 이름의 빈을 정의할 경우, 의존성 주입시 에러가 나기 때문에 주의합시다.

(3-1) BasicDataSource 프로퍼티
driverClassName : 사용할 DB의 Driver Class 이름
url : DB 접속URL (sample은 DB이름)
username : 사용자 이름 (DB에 등록된 사용자 명)
password : 사용자 비밀번호

다음 글에서는 MyBatis 셋팅에 대해 알아보겠습니다.

Comments