스프링에서 MySQL 연결을 위해서 jdbc connector를 추가해야 한다.
다운로드를 위한 주소는 아래와 같다. (현재 버전은 5.1.38)
https://dev.mysql.com/downloads/connector/j/
이와 함께 mybatis와 mybatis-spring을 설치해야 한다. (pom.xml 설정)
* 2016년 2월 최신 버전은 mybatis 3.3.1과 mybatis-spring 1.2.4 이다.
* mybatis 블로그
http://blog.mybatis.org/2016/02/mybatis-331-and-mybatis-spring-124.html
pom.xml을 등록할 내용은 다음과 같다.
* pom.xml에 등록하면 maven에 의해 필요한 컴포넌트를 자동으로 다운로드 한다
<!-- .. 생략 .. --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>org.bgee.log4jdbc-log4j2</groupId> <artifactId>log4jdbc-log4j2-jdbc4</artifactId> <version>1.16</version> </dependency> </dependencies> <!-- .. 후략 .. -->
다음으로 해야할 작업은 root-context.xml에 리소스를 등록하는 일이다.
MySQL 접속 주소와 ID/PW 그리고 작업시 데이터 인코딩(UTF-8) 방식을 지정한다.
<!-- Root Context: defines shared resources visible to all other web components --> <!-- <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”></property> <property name=”username” value=”zerock“></property> <property name=”password” value=”zerock“></property> </bean> --> <bean id=“dataSource” class=“org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name=“driverClassName” value=“net.sf.log4jdbc.sql.jdbcapi.DriverSpy”></property> <property name=“url” value=“jdbc:log4jdbc:mysql://127.0.0.1:3306/book_ex?useUnicode=yes&characterEncoding=UTF-8”></property> <property name=“username” value=“test”></property> <property name=“password” value=“test”></property> </bean> <bean id=“sqlSessionFactory” class=“org.mybatis.spring.SqlSessionFactoryBean”> <property name=“dataSource” ref=“dataSource” /> <property name=“configLocation” value=“classpath:/mybatis-config.xml”></property> <property name=“mapperLocations” value=“classpath:mappers/**/*Mapper.xml”></property> </bean> <bean id=“sqlSession” class=“org.mybatis.spring.SqlSessionTemplate” destroy-method=“clearCache”> <constructor-arg name=“sqlSessionFactory” ref=“sqlSessionFactory”> </constructor-arg> </bean> <context:component-scan base-package=“org.zerock.persistence”></context:component-scan> </beans>
mybatis를 이용해 MySQL DB 연결을 위해 설정해야 할 파일을 정리하면
- <Project_Home>/src/main/webapp/WEB-INF/spring/root-context.xml
: dataSource를 정의 (JNDI로 URL/ID/PW 등을 설정)
: DAO 스캔 위치를 정의 - <Project_Home>/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
: DB 객체와 Java 객체를 연결할 mapper 파일 위치 정의
: 연결할 Controller 스캔 대상을 정의 - <Project_Home>/src/main/resources/mapper/${xxx}Mapper.xml
: DB에 적용할 preparedStatement 들을 정의 (DAO 인터페이스와 연결) - <Project_Home>/src/main/resources/mybatis-config.xml
: mybatis를 위한 설정 (대개의 경우 작성할 일이 없다)
이와 같은 작업은 다음의 순서로 진행한다.
<내용>
MyBatis를 이용해 Spring 어플리케이션과 MySQL DB의 객체를 연결
<작업순서>
- DB 객체 생성
– DB 생성, Table 생성, Dummy 데이터 입력, 쿼리 테스트 - root-context.xml 설정
– dataSource(DB 연결 정보) 설정, jUnit으로 접속 테스트 - servlet-context.xml, mybatis-config.xml 설정
– 거의 복사 수준 - domain 패키지 작성
– java에서 사용할 객체 정의 (DB 객체에 대응하는) - persistence 패키지 작성
– java에서 사용할 객체 인터페이스 정의 (CURD 명령)
– 객체 인터페이스에 대한 구현객체 정의 (인터페이스 상속) - ${xxx}Mapper.xml 작성
– java 객체 인터페이스의 메소드와 대응하는 쿼리문 작성 - control 패키지 작성
– 스프링 annotation으로 URL과 파라미터 및 결과 view를 연결 - view 작성 (jsp 페이지들)
– 파라미터 객체들을 이용해 화면에 출력 (<% .. %> 구문과 JQuery/Javascript 이용)
MySQL에 연결하는 스프링 예제는 인터넷에 많이 있고 기본적인 단계이므로,
이 과정에서 에러가 발생한다면 xml 설정에 관한 부분이 많다.
이런 경우 책이나 예제 코드를 참고해 그대로 테스트 해 볼 것을 권한다.