2. Spring MVC 에 MyBatis 적용해 보기.
Spring + Mybatis 연동은 sts 와 mybatis 문서를 기반으로 작성 하였습니다.
[소스코드]
- Eclipse import 하시면 프로젝트 확인 가능 합니다.
※ import/export 방법은 아래 글 참고하세요.
http://jjeong.tistory.com/564
1. Spring MVC 구성해 보기.
2. Spring MVC 에 MyBatis 적용해 보기.
3. Spring MVC 에 Spring Security 적용해 보기.
4. Spring MVC 에 Hibernate 적용해 보기.
5. 2+3번 적용해 보기.
6. 3+4번 적용해 보기.
- 소스코드를 첨부 하였으므로 요약 정보만 기술 합니다.
[pom.xml]
<!-- spring framework jdbc 설정 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.0.6</version>
</dependency>
<!-- mybatis spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.0.2</version>
</dependency>
<!-- apache commons dbcp-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
[web.xml]
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
classpath:context/**/applicationContext*.xml
</param-value>
</context-param>
[applicationContext-mybatis.xml]
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:configuration/mybatis/config.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- http://groups.google.com/group/ksug/browse_thread/thread/766cd1fd8ba39c96 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:sql/mybatis/mapper/**/*.xml" />
</bean>
.... 중략
<bean id="boardContentViewDAO" class="proto.board.dao.BoardContentViewDAOImpl">
<property name="sqlSession" ref="sqlSession" />
</bean>[board.xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="proto.board.mapper">
<select id="getBoardContentView" resultType="proto.board.domain.BoardContentVO">
SELECT
document_srl,
title,
content,
user_id,
nick_name,
email_address,
regdate
FROM
xedemodb.xe_documents
WHERE
document_srl=66
</select>
</mapper>
- 기본 CRUD 에 대한 내용은 mybatis 문서를 참고 하시면 아주 쉽습니다.
[config.properties]
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xedemodb?autoReconnect=true
jdbc.username=root
jdbc.password=1234
※ jdbc url structure 참고
jdbc:odbc://host.domain.com:port/databasefile
[mysql]
http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
[oracle]
http://docs.oracle.com/cd/E12095_01/doc.10303/e12089/admdb.htm
[Class Flow - 초간단버전임]
http://localhost:8080/board/boardContentView
[BoardContentViewController.java]
@Autowired
private BoardContentViewService boardContentViewService;
...
boardContentViewService.getBoardContentVO()
[BoardContentViewServiceImpl.java]
- BoardContentViewService.java 는 interface 이므로 구현체인 BoardContentViewServiceImpl.java 확인
@Autowired
private BoardContentViewDAO boardContentViewDAO;
public BoardContentVO getBoardContentVO() {
return (BoardContentVO)boardContentViewDAO.getBoardContentVO();
}
[BoardContentViewDAOImpl.java]
- BoardContentViewDAO.java 는 interface 이므로 구현체인 BoardContentViewDAOImpl.java 확인
private SqlSession sqlSession;
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
public BoardContentVO getBoardContentVO() {
//boardContentVO.setContent("CONTENT");
return (BoardContentVO) sqlSession.selectOne("proto.board.mapper.getBoardContentView");
}
[board.xml]
<mapper namespace="proto.board.mapper">
<select id="getBoardContentView" resultType="proto.board.domain.BoardContentVO">
SELECT
document_srl,
title,
content,
user_id,
nick_name,
email_address,
regdate
FROM
xedemodb.xe_documents
WHERE
document_srl=66
</select>
</mapper>
[BoardContentViewVO.java]
public class BoardContentVO {
private int documentSrl;
private String title;
private String content;
private String nickname;
private String email;
private String regdate;
public int getDocumentSrl() {
return documentSrl;
}
.... 중략
public void setRegdate(String regdate) {
this.regdate = regdate;
}
}
자, 그럼 여기까지 아주 초간단 spring framework 과 mybatis 를 이용해서 SELECT 하는 것 까지 테스트 해봤습니다.
완전 기초 이니, 걍 참고만 하세요..
출처 - http://jjeong.tistory.com/606
'Framework & Platform > Spring' 카테고리의 다른 글
이클립스 + 톰캣 + 스프링 MVC + maven 개발환경 구축 1 (0) | 2012.03.21 |
---|---|
spring MVC 예제 3 (9) | 2012.03.21 |
Spring MVC 요청처리 생명주기 및 예제 (0) | 2012.03.21 |
Spring Framework 소개 (0) | 2012.01.31 |
Spring AOP(Aspect Oriented Programming) 개념 (0) | 2010.12.18 |