<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_tuto" />
<property name="username" value="root" />
<property name="password" value="beyondme" />
</bean>
</beans>
maven 추가
<!--@Inject-->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
테스트 코드
1. 위치 : src/test/java/org.wiems.web/DataSourceTest.java
package org.wimes.web;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import javax.sql.DataSource;
import java.sql.Connection;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/web/WEB-INF/applicationContext.xml"})
public class DataSourceTest {
@Inject
private DataSource ds;
@Test
public void testConnection() throws Exception{
try {
Connection con = ds.getConnection();
System.out.println(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
오류
여기까지 진행했는데, test 코드에서 junit 이 import 되지 않는 오류가 발생했다.
그래서 maven repository에서 Spring TestContext Framework를 검색해 해당되는 버전으로 들어가 다른 라이브러리들과의 버전이 맞는지 체크하며 변경했다.
이래도 오류가 잡히지 않았고, 결국 프로젝트 우클릭 - Build Path - configure Build Path - Libraries - add Library - JUnit 으로 들어가 JUnit library version을 Junit4로 선택해 생성했다.
이렇게 하니 오류가 사라졌다.
결과
여기까지 작성하고 실행하니 console창에 오류가 없이, "jdbc:mysql:~~~"이라는 메세지가 뜨는 걸로 봐서는 DB 연결이 잘 되는것같다.
SqlSessionFactory 객체 설정
<bean id="sqlSeesionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
package org.wimes.web;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/web/WEB-INF/applicationContext.xml"})
public class MyBatisTest {
@Inject
private SqlSessionFactory sqlSessionFactory;
@Test
public void testFactory(){
System.out.println(sqlSessionFactory);
}
@Test
public void testSession() throws Exception{
try{
SqlSession sqlSession = sqlSessionFactory.openSession();
System.out.println(sqlSession);
}catch (Exception e){
e.printStackTrace();
}
}
}
결과
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@20b5f2ac org.apache.ibatis.session.defaults.DefaultSqlSession@2364305a
console창에 이와 비슷한 형태로 출력이 된다면 문제 없이 정상 동작한다는 뜻입니다.
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations">
<array>
<value>
classpath*:/src/main/resources/mappers/*.xml
</value>
</array>
</property>
</bean>
참고
1. 개발환경설정 - Spring + MyBatis + MySQL의 설정
2. [spring] MyBatis와 스프링 연동하기 1편
2. [06.스프링+MyBatis]3.XML Mapper의 작성