본문 바로가기
카테고리 없음

2. 디비 연결

by Luna_lua 2022. 2. 2.
반응형

1. MyBatisConfig만들기

package com.example.mytrip.mybatis;


import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
@RequiredArgsConstructor
@MapperScan("com.example.mytrip.mappers")
public class MyBatisConfig {

    private final ApplicationContext applicationContext;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    public HikariConfig hikariConfig() {
        return new HikariConfig();
    }

    @Bean
    public DataSource dataSource() {
        return new HikariDataSource(hikariConfig());
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath*:/mapper/*.xml"));

        try {
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
            sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
            return sqlSessionFactory;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

2. applicationConfig.properties 설정

#server port // 서버에 사용할 포트! 10000이상으로 하는것을 추천
server.port=10011

#JDBC spy datasource // 디비와 spy를 사용해서 로그에서 보기 쉽도록 사용한다.
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
spring.datasource.hikari.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.datasource.hikari.jdbc-url=jdbc:log4jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.hikari.username=hr
spring.datasource.hikari.password=hr

#log level
logging.level.root=info

#404
spring.mvc.throw-exception-if-no-handler-found=true
spring.web.resources.add-mappings=false

 

3. pom.xml (필수 아님!)

/* dependencies안에 넣기 */
<dependency>
    <groupId>org.bgee.log4jdbc-log4j2</groupId>
    <artifactId>log4jdbc-log4j2-jdbc4</artifactId>
    <version>1.16</version>
</dependency>

/* build안에 넣기 */
 <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.setting</include>
                </includes>
            </resource>
        </resources>

 

4. 마이바티스 테스트 하기

package com.example.mytrip.mybatis;


import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.Connection;

@SpringBootTest
@Slf4j
public class MyBatisConfigTest {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private DataSource dataSource;

    @Test
    public void testDataSource() {
        try
                (
                        Connection conn = dataSource.getConnection();
                ) {
            log.info("--------------------------");
            log.info("datasource connection : " + conn);
            log.info("--------------------------");
        } catch (Exception e) {
            log.info(e.getMessage());

        }
    }

    @Test
    public void testSqlSession() {
        log.info("--------------------------");
        log.info("sql session factory : " + sqlSessionFactory);
        log.info("--------------------------");

        try
                (
                        SqlSession sqlSession = sqlSessionFactory.openSession(true);
                        Connection conn = sqlSession.getConnection();
                ) {
            log.info("sql session : " + sqlSession);
            log.info("sql session connection : " + conn);
            log.info("--------------------------");
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }
}
반응형