1. pom.xml에 dependency 추가 & Junit 버전(4.7->4.12로 변경)

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
	<version>8.0.18</version>
</dependency>

<!-- Test -->
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>        

※ Mysql 버전 확인 방법

cmd에 "mysql --version" 입력

2. root-context.xml에 'dataSource' bean 추가

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/스키마이름?useSSL=false&amp;serverTimezone=UTC">
        </property>
        <property name="username" value="사용자이름"></property>
        <property name="password" value="비밀번호"></property>
</bean>

한글 적힌 부분은 본인의 것으로 변경

3. DataSoureTest 해보기

package com.tnqls.spring;


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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =  {"file:src/main/webapp/WEB-INF/spring/**/root-context.xml" })
public class DataSourceTest {
	
	@Autowired
	private DataSource ds;

	@Test
	public void test() {
		try(Connection con = ds.getConnection()){
			System.out.println("\n >>>>> Connection 출력: " + con + "\n");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

}

성공하면 콘솔창에 connection 객체 출력

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기