'MyBatis'에 해당되는 글 2건

  1. 2013.01.04 SpringMVC + Mybatis + MSSQL 연동 맛보기
  2. 2012.03.12 2. Spring MVC 에 MyBatis 적용해 보기. 2

SpringMVC + Mybatis + MSSQL 연동 맛보기

ITWeb/개발일반 2013. 1. 4. 10:37

Reference

MSSQL JDBC Driver Maven Dependency 설정
  - MSSQL JDBC Driver Download 받기
    http://www.microsoft.com/ko-kr/download/details.aspx?id=11774
 
  - JDK 1.6.x 이상에서는 sqljdbc4.jar 사용 권장
  - JDK 1.6.0 29 버전에서는 excuteQuery 수행 시 hang 걸리는 이슈가 있어 버전 업그레이드 필요 (reference url 참고)
 
  - exe 파일 실행 후 적절한 위치에 압축 해제
  - spring mvc maven project 생성
  - 해당 project 의 java build path 에서 libraries 내 add external jars 로 sqljdbc4.jar 추가
  - maven local repository 에 sqljdbc4.jar 설치
    . command 창에서 실행
    . mvn install:install-file -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0 -Dpackaging=jar -Dfile=D:\Application\sqljdbc_4.0\sqljdbc_4.0\kor\sqljdbc4.jar
  - pom.xml 에서 local repository 와 dependency 설정 추가
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
    </dependency>
 
    <repositories>
        <repository>
        <id>local</id>
        <name>local</name>
        <url>file://C:/Users/nhn/.m2/repository</url>
        </repository>
    </repositories>
  - spring mvc project 내 jdbc 관련 설정
    jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
    jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=nnote;
    jdbc.username=nnote
    jdbc.password=1111
SpringMVC + Mybatis + MSSQL 템플릿
    - spring-mvc4mybatis-mssql-template (이건 SpringMVC + Mybatis 글에 등록된 예제 참고 하시면 됩니다.)
템플릿 용 Schema DDL
create table xe_documents (
 document_srl bigint IDENTITY(1,1) not null,
 title varchar(256),
 content varchar(256),
 user_id varchar(256),
 nick_name varchar(256),
 email_address varchar(256),
 regdate datetime,
CONSTRAINT pk_xe_documents_srl PRIMARY KEY("document_srl")
);
 
insert into xe_documents(title,content,user_id,nick_name,email_address, regdate)
values('제목', '내용', '사용자아이디', 'henry', 'henry@naver.com', SYSDATETIME() );
 
select *
from xe_documents

:

2. Spring MVC 에 MyBatis 적용해 보기.

ITWeb/개발일반 2012. 3. 12. 10:36
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> 

- pom.xml 에서는 필요한 package depedency 를 작성해 줍니다.


[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>

- spring framework 이 처음 로딩 될때 읽어 들여야 하는 설정파일들에 대한 path 를 잡아 줍니다.


[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> 

- dbcp 설정과 dao 에서 사용할 sqlSession 설정을 합니다.


[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>

- 사용한 테이블은 xpressengine 이 설치 되면 기본 생성되는 xe_documents 의 테이블 입니다.
- 기본 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 참고

- 각자 접속 정보에 맞게 설정 하시면 됩니다.


[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 하는 것 까지 테스트 해봤습니다.
완전 기초 이니, 걍 참고만 하세요..
: