개발공작소
article thumbnail
728x90

 

이번에 댓글기능 구현하려고 하니, 불가피하게 DB를 연동 및 마이바티스를 써야 할 것 같아 알아보았다.

아직 스프링부트가 안익숙하기도 하고, Gradle도 처음 써보는거라 한번 정리한다.

 

혹시 Oracle이 설치 안되어 있다면 ( 참조글 )을 참조 하도록 하자.

 

 

1. 커넥션 풀 설정

============================================================================

 

우선 스프링부트로 프로젝트를 생성하였다면 application.properties파일이 있을 것이다. 거기에

커넥션 풀을 설정 할 수 있다. 나는 다음과 같이 설정해주었다.

 

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@[호스트]:[포트번호]/[DB명]
spring.datasource.username=[사용자명]
spring.datasource.password=[암호]

 

application.properties

 

이렇게 커넥션 풀을 설정해준 뒤, build.gradle에 아래와 같이 dependency를 추가해주어, Oracle.Driver을 설정해준다.

 

runtimeOnly 'com.oracle.database.jdbc:ojdbc8'

 

이렇게 하면 오라클 설정은 끝...

 

 

2. 마이바티스 설정

============================================================================

 

dependency에 아래를 추가 해주도록 하자.

 

implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'

 

간단히 설명하면, 스프링부트에서 마이바티스를 사용하고자 할 때 이 녀석만 넣어주면, 의존성이 있는

다른 마이바티스 관련 설정들도 같이 가져온다. 하나만 추가해줘도 마이바티스가 된다는 사실...

 

그리고 application.properties에 마이바티스 관련 설정을 해주도록 해보자..

 

# MyBatis
# mapper.xml 위치 지정
mybatis.mapper-locations: mybatis-mapper/**/*.xml

# model 프로퍼티 camel case 설정
mybatis.configuration.map-underscore-to-camel-case=true

# 패키지 명을 생략할 수 있도록 alias 설정
mybatis.type-aliases-package=com.example.demo.mapper.domain

# mapper 로그레벨 설정
logging.level.com.example.demo.mapper.mapper=TRACE

 

이제 설정은 끝났으니 실제로 Controller -> Service -> Mapper -> DB에서 데이터 조회를 해보도록 하자.

 

 

 

3. 데이터 가져오기

============================================================================

 

MVC 구조

 

현재 구조는 이렇게 되어있다.

main.java.com.example.demo 안에 각 controller, service, mapper가 있는 상태이고,

resources에 mybatis-mapper라는 폴더를 만들어 testMapper.xml을 생성했다.

 

여기서 xml을 찾는 위치는 위의 application.properties에서 설정을 해주었다...

Controller는 넘기고, Service와 Mapper, XML만 보도록 하겠다.

 

IndexService.java

package com.example.demo.service;

import com.example.demo.mapper.IndexMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IndexService {

    @Autowired
    IndexMapper indexMapper;

    public void testXML() {
        System.out.print("--------------server started--------------");
        String testTXT = indexMapper.testXML();
        System.out.println("TIMESTAMP: " + testTXT);
    }

}

 

IndexMapper.java

package com.example.demo.mapper;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface IndexMapper {

    public String testXML();

}

 

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="com.example.demo.mapper.IndexMapper">
    <select id="testXML" resultType="string">
        select "TIMESTAMP" from APPQOSSYS.WLM_CLASSIFIER_PLAN
    </select>
</mapper>

 

 

이렇게 하고 서버를 구동하면 제대로 데이터를 가져오는 것을 확인 할 수 있다..

 

테스트용 데이터

 

IndexService 로그

 

 

이렇게 오라클 및 마이바티스 연동에 대한 설명을 끝내겠다. 아래는 application.properties, build,gradle 전체코드

 

 

application.properties

server.port = 8082

spring.mvc.view.prefix:/WEB-INF/html/
spring.mvc.view.suffix:.html

#oracle 연동
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
spring.datasource.username=system
spring.datasource.password=6154

# MyBatis
# mapper.xml 위치 지정
mybatis.mapper-locations: mybatis-mapper/**/*.xml

# model 프로퍼티 camel case 설정
mybatis.configuration.map-underscore-to-camel-case=true

# 패키지 명을 생략할 수 있도록 alias 설정
mybatis.type-aliases-package=com.example.demo.mapper.domain

# mapper 로그레벨 설정
logging.level.com.example.demo.mapper.mapper=TRACE

 

 

build.gradle

plugins {
	id 'org.springframework.boot' version '2.6.4'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
	implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
	runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
}

tasks.named('test') {
	useJUnitPlatform()
}
728x90
profile

개발공작소

@모찌바라기

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!