WonYong-Jang / ToyProject

0 stars 0 forks source link

[오라클] 커넥션 풀 / HikariCp / mybatis #7

Open WonYong-Jang opened 4 years ago

WonYong-Jang commented 4 years ago

커넥션 풀 설정

compile group: 'com.zaxxer', name: 'HikariCP', version: '2.7.4'

WonYong-Jang commented 4 years ago

Mybatis 와 스프링 연동

라이브러리

    compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.2'
    compile group: 'org.mybatis', name: 'mybatis', version: '3.4.6'
    compile group: 'org.springframework', name: 'spring-jdbc', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-tx', version: '5.0.7.RELEASE'

SQLSessionFactory

Mapper

public interface TimeMapper {
    @Select("SELECT sysdate FROM dual")
    public String getTime();
}

log4jdbc-log4j2

WonYong-Jang commented 4 years ago
@Configuration
@ComponentScan(basePackages = {"org.zerock.sample"})
@MapperScan(basePackages = {"org.zerock.mapper"})
public class RootConfig {
    @Bean
    public DataSource dataSource() {
        HikariConfig hikariConfig = new HikariConfig();
        //hikariConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver"); 
        //hikariConfig.setJdbcUrl("jdbc:oracle:thin:@localhost:49161:xe");
        hikariConfig.setDriverClassName("net.sf.log4jdbc.sql.jdbcapi.DriverSpy"); // log4jdbc.log42j 설정후 
        hikariConfig.setJdbcUrl("jdbc:log4jdbc:oracle:thin:@localhost:49161:xe"); // log4jdbc.log42j 설정후

        hikariConfig.setUsername("system");
        hikariConfig.setPassword("oracle");

        HikariDataSource dataSource = new HikariDataSource(hikariConfig);

        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {

        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource());
        return (SqlSessionFactory) sqlSessionFactory.getObject();
    }
}