<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- MyBatis 3.4.1 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- MyBatis-Spring -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- Spring-jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Spring-test -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
다음과 같이 pom.xml파일에 mysql, spring-jdbc, mybatis, spring-test 의존성을 추가해준다.
이제 Test코드를 작성해보자
먼저 MysqlTest.java를 작성해보자.
@Runwith(SpringJunit4ClassRunner.class)
@ContextConfiguration(loactions={"file:src/main/webapp/WEB-INF/spring/root-context})
public class MySqlTest{
@Inject
private DataSource ds;
@Test
public void testConnection() throws Exception{
try(Connection con = ds.getConnection()){
System.out.println("테스트 성공 >>>>>>> Connection출력:" +con+"/n");
}catch(Exception e){
e.printStackTrace();
}
}
}
MyBatis 테스트 코드도 살펴보자.
@Runwith(SpringJunit4ClassRunner.class)
@ContextConfiguration={"file:src/main/webapp/WEB-INF/spring/**/root-context.xml})
public class MyBatisTest{
@Inject
private SqlSessionFactory sqlFactory;
@Test
public void testSession() throws Exception{
try(SqlSession session = sqlFactory.openSession(){
System.out.println("테스트성공 : session 출력 : "+session+"/n");
}catch(Exception e){
e.printStactTrace();
이렇게 작성하고, Junit테스트를 해보자.
Test코드의 @어노테이션에 대해서는 1번째 Spring정리에서 했으니 생략하도록 하겠다.
참고 : 코드로 배우는 스프링 웹 프로젝트