간단하게 필요한 라이브러리에 대해서 설명 드리자면,
Mybatis 생산성/유지보수/성능/이식성을 향상 시키고, 간단하게 DB 사용을 지원 해주는 오픈소스입니다
Mybatis-spring는 하나의 모듈로써 Mybatis와 Spring를 연결 해주는 역할을 합니다.
jdbc는 자바와 데이터베이스의 연결을 해주는 역할을 합니다.
dbcp 디비의 연결을 미리 예측하고 일정 갯수의 connection을 유지하는것 입니다.(효율적인 db사용 가능)
첫 번째로 pom.xml 에 Mybatis, MyBatis-spring 을 추가 해주세요. 각 버젼 확인은 아래 url 에서 해주세요
Mybatis - https://mvnrepository.com/artifact/org.mybatis/mybatis
Mybatis-Spring - https://mvnrepository.com/artifact/org.mybatis/mybatis-spring
spring-jdbc - https://mvnrepository.com/artifact/org.springframework/spring-jdbc
spring-dbcp - https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp
저는 3.4.6(Mybatis) , 2.0.0(Mybatis-Spring), 5.0.0(spring-jdbc), 1.4(commons-dbcp) 을 사용 하겠습니다.
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 | < dependency >
< groupid >org.mybatis</ groupid >
< artifactid >mybatis-spring</ artifactid >
< version >2.0.0</ version >
</ dependency >
< dependency >
< groupid >org.mybatis</ groupid >
< artifactid >mybatis</ artifactid >
< version >3.4.6</ version >
</ dependency >
< dependency >
< groupid >org.springframework</ groupid >
< artifactid >spring-jdbc</ artifactid >
< version >5.1.0.RELEASE</ version >
</ dependency >
< dependency >
< groupid >commons-dbcp</ groupid >
< artifactid >commons-dbcp</ artifactid >
< version >1.4</ version >
</ dependency >
|
라이브러리 등록이 완료되면 root-context.xml 파일을 설정해야 합니다. 이 파일은 웹과 관련 없는 나머지 객체(bean)을 설정 해줍니다.
원래는 web.xml 최초 서버가 실행될 때 이 파일을 참조 할 수 있게 설정을 해야 하는데, 스프링 프로젝트 생성시 자동으로 입력 됩니다.
1 2 3 4 | < context-param >
< param-name >contextConfigLocation</ param-name >
< param-value >/WEB-INF/spring/root-context.xml</ param-value >
</ context-param >
|
DB 연결하는 부분은 웹이 아니기 때문에 이부분에서 설정을 해줍니다. (웹은 servlet-context)
아래 항목 중 Namespace를 누르시면 Configure Namespaces 화면을 확인 하실 수 있습니다.
aop, bean, context, mybatis-spring을 체크 해주세요. 체크를 하시면 아래 그림과 같이 자동으로 bean의 경로가 설정 됩니다.


자동 입력된 root-context.xml 파일에서 주석 밑에 DB접속 정보를 입력 해주시면 됩니다.
mariadb 는 mysql jdbc를 이용해서 접근이 가능합니다. 찜찜하신 분들은 mariadb jdbc를 사용 해주시면 됩니다.
그리고 dataSource의 destory-method 는 bean 객체의 스코프가 종료될 때 선언된 클래스를 종료하기 위해 close()메소드를 실행 합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | < bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name = "driverClassName" value = "ex(com.mysql.jdbc.Driver)" ></ property >
< property name = "url" value = "ex(jdbc:mysql://서버URL:서버포트번호/db이름?serverTimezone=UTC)" ></ property > < property name = "username" value = "ex(ID)" ></ property >
< property name = "password" value = "ex(PW)" ></ property >
</ bean >
< bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" >
< property name = "dataSource" ref = "dataSource" ></ property >
< property name = "configLocation" value = "classpath:/config/mybatis-config.xml" ></ property >
< property name = "mapperLocations" value = "classpath:/mappers/*Mapper.xml" ></ property >
</ bean >
< bean id = "sqlSession" class = "org.mybatis.spring.SqlSessionTemplate" destroy-method = "clearCache" >
< constructor-arg name = "sqlSessionFactory" ref = "sqlSessionFactory" ></ constructor-arg >
</ bean >
< context:component-scan base-package = "ex(mybatisUtil)" ></ context:component-scan >
|
root-context.xml 에 데이터 베이스 관련 정보들을 입력 하신 다음에 아래와 같이 src/main/resources 폴더에
config, mappers 폴더를 만들어 주세요.
config 폴더에는 mybatis-config.xml 파일을, mappers 폴더에는 testMapper.xml 파일을 생성 해주세요.
각 폴더 및 파일을 생성 하신 다음 아래 내용을 해당 파일에 복사해서 붙여 넣어주세요 !!!
원래 하나의 파일에 설정을 할 수 있지만 저는 다른 config 파일들이 추가 될 예정이라서 효율적인 관리를 위해 따로 등록 하였습니다.

이 두놈은 syntaxHighlight 가 도저히 먹히지 않습니다ㅠㅠ
(mybatis-config.xml)
<?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>
(testMapper.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper">
</mapper>
여기까지 스프링 프로젝트의 DB설정 및 Mybatis 연동이 끝났습니다.
다음 포스트 부터 실제 mybatis를 이용해서 데이터를 불러오고 입력하는 예제를 진행 해보겠습니다.
감사합니다 :)