본문 바로가기
Spring/Legacy

Spring <-> DB Connection

by pms93 2022. 10. 25.

< java에서의 DB connection >

package com.ms.blog.hikariconfig;

import java.io.IOException;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration 
// <mybatis-spring:scan base-package="com.ms.db.basic.repository"/>
@MapperScan(basePackages = {"com.ms.blog.repository"})
// @Coufiguarion
// - annotation으로 설정파일임을 명시한다.
// @MapperScan
// - annotation으로 basePackge를 설정한다.
public class DBConfig {
	
	@Bean
	public HikariDataSource dataSource() {
//		<bean class="com.zaxxer.hikari.HikariConfig" id="hikariConfig">
//			<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
//			<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"/>
//			<property name="username" value="oracle"/>
//			<property name="password" value="oracle"/>
//		</bean>
// 		<bean class="com.zaxxer.hikari.HikariDataSource" id="dataSource">
//			<constructor-arg ref="hikariConfig"/>
// 		</bean>
		
		// xml에서 dataSource 객체에 ref속성을 통해 위에서 설정한 hikariConfig객체의 주소값을 주입하고 있다. java에서도 이와 동일하게 진행한다.
		HikariConfig hikariConfig = new HikariConfig();
		hikariConfig.setDriverClassName("oracle.jdbc.OracleDriver");
		hikariConfig.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:xe");
		hikariConfig.setUsername("oracle");
		hikariConfig.setPassword("oracle");
		
		HikariDataSource dataSource = new HikariDataSource(hikariConfig);
		return dataSource;
	}

	@Bean
	public SqlSessionFactoryBean sessionFactory() throws IOException {
		
// 		<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
//			<property name="dataSource" ref="dataSource"/>
//			<property name="mapperLocations" value="classpath:/mappers/**/*Mapper.xml" />
// 		</bean>
		// 위에서 생성한 dataSource 객체를 sessionFacotry객체에 주입 후 classpath를 지정한다.
		// (*classpath : src/main/resources와 같은 위치)
		//  - *Mapper.xml에는 Query가 작성된다.
		SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource());
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		sessionFactory.setMapperLocations(resolver.getResources("classpath:/mappers/**/*Mapper.xml"));
		
		return sessionFactory;
	}
}

 

< xml(root-context.xml)에서의 DB Connection >

<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- 
		mybatis에 필요한 library들이 준비가 되었다면 하단의 Namespaces 설정에서 mybatis-spring을 체크해줘야 한다. 
	 -->
	<bean class="com.zaxxer.hikari.HikariConfig" id="hikariConfig">
		<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
		<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"/>
		<property name="username" value="oracle"/>
		<property name="password" value="oracle"/>
	</bean>	
	
	<bean class="com.zaxxer.hikari.HikariDataSource" id="dataSource">
		<constructor-arg ref="hikariConfig"/>
	</bean>
	
	<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
		<property name="dataSource" ref="dataSource"/>
		<property name="mapperLocations" value="classpath:/mappers/**/*Mapper.xml" />
	</bean>
	<mybatis-spring:scan base-package="com.ms.blog.repository"/>
</beans>

 

'Spring > Legacy' 카테고리의 다른 글

AJAX & JSON  (0) 2022.10.26
DAO(Repository) & Mapper.xml  (0) 2022.10.25
Controller in Legacy Package  (0) 2022.10.18