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 버전 확인 방법
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&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 객체 출력
'개발 > Spring' 카테고리의 다른 글
[Spring/게시판] 4. 로그인 / 로그아웃 (0) | 2020.05.05 |
---|---|
[Spring/게시판] 3.1 회원가입 추가기능 (0) | 2020.04.25 |
[Spring/게시판] 3. 회원가입하기 (0) | 2020.04.23 |
[Spring/게시판] 2. Mybatis 연동하기 (0) | 2020.04.23 |
최근댓글