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

[4.스프링+MyBatis_MySQL의 설정]4.DataSource 설정

by jaekk 2018. 8. 10.

DataSource


spring과 MyBatis를 같이 사용하는 경우 주로 스프링의 설정으로 JDBC연결을 처리

->Spring-jdbc 모듈의 클래스를 이용해서 root-context.xml에 DataSource 추가 


DataSource: JDBC의 커넥션 처리 기능

(MySQL과의 연동을 담당)

1
2
3
4
5
6
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/book_ex?useSSL=false"></property>
        <property name="username" value="zerock"></property>
        <property name="password" value="zerock"></property>
    </bean>    
cs

spring-jdbc모듈의 DriverManagerDataSource클래스를 이용

bean id

객체 빈을 찾기 위해서 사용하는 가명

id를 이용해서 다른 객체와 연결

             

spring-text모듈을 이용, WAS상에서 동작시키지 않고도 동작 확인 가능


DataSource 테스트 진행

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
japackage org.zerock.web;
 
import java.sql.Connection;
 
import javax.inject.Inject;
import javax.sql.DataSource;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"file:src/mian/webapp/WEB-INF/spring/**/root-context.xml"})
public class DataSourceTest {
    
    @Inject
    private DataSource ds;
    
    @Test
    public void testConntion() throws Exception{
        try (Connection con=ds.getConnection()){
            System.out.println(con);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}
 
cs


@RunWith,

@ContextConfiguration

테스트 코드를 실행할 때 스프링이 로딩되도록 하는 부분

@ContextConfiguration의 locations속성 경로의 xml파일을 이용해서 스프링 로딩

@Inject

@Inject 애노테이션 처리된 DataSource는 스프링이 생성해서 주입해주므로 객체 생성하지 않아도 됨




테스트 코드 진행 결과


댓글