Spring - Spring + Mybatis 설정하기 - Mapper를 이용하여 DAO자동생성하기
목록  
제 목 Spring + Mybatis 설정하기 - Mapper를 이용하여 DAO자동생성하기
작성자 박세청 작성일 2014/01/17 16:57


 Spring + Mybatis 설정하기 - Mapper를 이용하여 DAO자동생성하기

 

이번엔 Mapper를 이용하여 dao를 자동생성해 보겠습니다. 이것을 하면 뻔한 dao를 하나씩 만들어줘야 했던 작업이 사라져 매우 편합니다.

 

전편의 ibatis와 비슷하게 설정하기 (http://blog.naver.com/platinasnow/30174311054) 와 똑같은 과정을 거치므로 참고해도 좋습니다.

 

 

1. Libary


mybatis-3.2.2.jar

 

mybatis-spring-1.2.0.jar

 

두 파일이 필요합니다.

 

dependency는 아래와 같습니다.

 

 <dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.2.2</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.2.0</version>

</dependency>

 

 

2. Setting


 

 

여기서 mapper 패키지에 UserInfoMapper는 자동 생성될 DAO로써, Interface로 생성해야 합니다.

 

query 패키지의 쿼리 문이 담긴 xml Mapper도 맵퍼라고 불리고 두개는 같은 패키지에 넣는 경우도 있지만,

 

여기서는 의미를 확실하게 해두기 위해서 이름을 저렇게 짓고, 두개를 분리해보았습니다.

 

 

2.1 MyBatis 환경설정

 

web.xml 은 마찬가지로 마이바티스의 설정파일을 추가하게끔 설정합니다.

 

  <context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>

  /WEB-INF/spring-servlet.xml

  /WEB-INF/mybatis-context.xml

  </param-value>

  </context-param>

  <listener>

  <listener-class>

  org.springframework.web.context.ContextLoaderListener

  </listener-class>

  </listener>

 

<servlet>

<servlet-name>spring</servlet-name> 

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

 

 

mybatis-context.xml 은 다음과 같습니다.

 

 <?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:p="http://www.springframework.org/schema/p

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

 

<context:component-scan base-package="com.exam.service" />

<!-- mssql-->  

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 

<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>

<property name="url" value="jdbc:sqlserver://localhost:1433;selectMethod=cursor;"/> 

<property name="username" value="userid"/> 

<property name="password" value="password"/> 

</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

   <property name="dataSource" ref="dataSource" />

   <property name="mapperLocations" value="classpath*:com/exam/query/*.xml" />

   <property name="typeAliasesPackage" value="com.exam.model"/>

   <property name="configLocation" value="/WEB-INF/config/mybatisConfig.xml"/>

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

  <property name="basePackage" value="com.exam.mapper" />

</bean>

<!-- 트랜잭션 매니저 -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

 

<!-- Annotation 기반 트랜잭션 설정 -->

<tx:annotation-driven transaction-manager="transactionManager" />

</beans>


 

기존 설정과 달라진 점은 더이상 sqlSession을 써서 수동으로 dao를 만들지 않기 때문에 sqlSession 설정을 뺐다는 점과, 그 대신 맵퍼를 

 

인식하게 하기위해서 MapperScannerConfigurer를 사용하여 mapper 패키지를 basePackage로 설정했다는 점입니다.

 

2.2. xml Mapper

 

 <?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="com.exam.mapper.UserInfoMapper">

 

<select id="getUserList" resultType="UserInfoVO">

use kb

SELECT name, userId

FROM User_Table

</select>

 

</mapper>


 

여기서 네임스페이스가 중요합니다. dao를 자동으로 생성할 dao 파일의 이름과 경로를 지정합니다.

 

이 파일은 앞에서 말했듯이 Interface 파일입니다.

 

 

2.3. Dao Mapper

 

 public interface UserInfoMapper {

public List<UserInfoVO> getUserList();

 

}


 

Interface로 설정해야된다는 점이 중요합니다. 파일 반환형, 파라미터, 이름은 xml mapper에서 쓴 걸 그대로 사용해야합니다.

 

또한 interface이기 때문에 내부 구현은 하지 않아도 되는게 아닌 안해야 합니다.

 

서비스와 컨트롤러부분은 사용하던데로 사용하면 되니 

 

 ibatis와 비슷하게 설정하기 (http://blog.naver.com/platinasnow/30174311054)를 참고하기 바랍니다. 





이전글 Spring MVC 패턴에서 ajax를 활용한 실시간 댓글달기 소스
다음글 [MyBatis] Spring + Mybatis 설정하기 - Ibatis 와 비슷하게 설정하기 - Ibatis/Mybatis

목록