IBATIS/myBatis
2017.04.28 / 10:25

MyBatis-Spring

MultiPlayer
Ãßõ ¼ö 349

MyBatis-Spring




********************************************************************************
Getting Started
********************************************************************************


- ¼³Ä¡

MyBatis-Spring À» ÀÌ¿ëÇϱâ À§Çؼ­´Â mybatis-spring-x.x.x.jar °ú ±× dependencyµéÀ» Ŭ·¡½º Æнº¿¡ Ãß°¡ÇØ¾ß ÇÑ´Ù. MavenÀ» »ç¿ëÁßÀ̶ó¸é pom.xml ¿¡ ´ÙÀ½ dependency ¸¦ Ãß°¡ÇÑ´Ù.

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>x.x.x</version>
</dependency>

- Quick Setup

½ºÇÁ¸µ ¾îÇø®ÄÉ¿¡¼Ç ÄÁÅؽºÆ®¿¡´Â ±âº»ÀûÀ¸·Î ´ÙÀ½ 2°¡Áö°¡ ÇÊ¿äÇÏ´Ù.

1. SqlSessionFactory
2. ÃÖ¼Ò ÇϳªÀÌ»óÀÇ mapper interface

SqlSessionFactory¸¦ »ý¼ºÇϱâ À§ÇØ SqlSessionFactoryBean ÀÌ »ç¿ëµÈ´Ù. Áï ´ÙÀ½Ã³·³ ½ºÇÁ¸µ ¼³Á¤ÆÄÀÏ¿¡ ¼±¾ðÇØ¾ß ÇÑ´Ù.

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
</bean>

µ¥ÀÌÅÍ ¼Ò½º°¡ ÇÊ¿äÇÑ°ÍÀ» ÀØÁö ¸»°Í. ±× ´ÙÀ½ Mapper Interface °¡ ÇÊ¿äÇÏ´Ù.

public interface UserMapper {
  // mapper XML ÆÄÀÏ ´ë½Å annotation »ç¿ë°¡´É!
  @Select("SELECT * FROM users WHERE id = #{userId}")
  User getUser(@Param("userId") String userId);
}


ÀÌ ÀÎÅÍÆäÀ̽º´Â ´ÙÀ½Ã³·³ MapperFactoryBean ¸¦ »ç¿ëÇؼ­ ½ºÇÁ¸µ¿¡ Ãß°¡µÈ´Ù.

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

¸ÊÆÛ Å¬·¡½º´Â ¹Ýµå½Ã ÀÎÅÍÆäÀ̽º¿©¾ß ÇÑ´Ù. ½ÇÁ¦ ±¸Çö ÆÄÀÏÀÌ¸é ¾ÈµÈ´Ù.
À§Ã³·³ annotation À» ÀÌ¿ëÇؼ­ SQLÀ» ÁöÁ¤ÇÒ¼öµµ ÀÖÁö¸¸, MyBatis mapper XML ÆÄÀ϶ÇÇÑ »ç¿ë°¡´ÉÇÏ´Ù.

ÀÏ´Ü ¼³Á¤ÀÌ ¿Ï·áµÇ¸é, ±âÁ¸ ½ºÇÁ¸µ ºó°ú µ¿ÀÏÇÏ°Ô ºñÁî´Ï½º/¼­ºñ½º °´Ã¼µé¿¡°Ô mapperµéÀ» injectÇÒ¼ö ÀÖ´Ù.
MapperFactoryBean Àº SqlSessionÀ» »ý¼ºÇÏ°í, ´Ý´Â ¿ªÈ°À» ¼öÇàÇÑ´Ù. ¶ÇÇÑ ½ºÇÁ¸µ Æ®·£Àè¼ÇÀÌ ÁøÇàÁßÀ̶ó¸é ±×°ÍÀÌ ¿Ï·áµÉ¶§, ¼¼¼Ç ¶ÇÇÑ Ä¿¹Ô°ú ·Ñ¹é 󸮵ȴÙ. ±×¸®°í ¸ðµç ¿¹¿ÜµéÀ» ½ºÇÁ¸µÀÇ DataAccessException ·Î Àüȯ½ÃÅ°´Â ¿ªÈ°µµ ¼öÇàÇÑ´Ù.

ÀÌÁ¦ MyBatis µ¥ÀÌÅÍ ¸Þ¼­µåµéÀº ÄÚµå ÇÑÁÙ ÀÌ¸é µÈ´Ù.

public class FooServiceImpl implements FooService
{
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper)
   {
       this.userMapper = userMapper;
    }

    public User doSomeBusinessStuff(String userId)
    {
   return this.userMapper.getUser(userId);
    }
}


´ÙÀ½ºÎÅÍ´Â ¾Õ¼­ÀÇ ³»¿ë¿¡ ´ëÇÑ Á»´õ »ó¼¼ÇÑ ¼³¸íµéÀÌ´Ù. 


********************************************************************************
SqlSessionFactoryBean
********************************************************************************

±âº»ÀûÀ¸·Î MyBatis¿¡¼­ SqlSessionFactory´Â SqlSessionFactoryBuilder¸¦ ÀÌ¿ëÇؼ­ ¸¸µé¾îÁø´Ù. MyBatis-Spring ¿¡¼­´Â ´ë½Å, SqlSessionFactoryBean °¡ »ç¿ëµÈ´Ù.

- ¼³Á¤

ÆÑÅ丮ºóÀ» »ý¼ºÇϱâ À§ÇÑ ½ºÇÁ¸µ ¼³Á¤ ÆÄÀÏ ³»¿ëÀº ´ÙÀ½°ú °°´Ù.

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
</bean>

SqlSessionFactoryBean Àº ½ºÇÁ¸µÀÇ FactoryBean interface ¸¦ ±¸ÇöÇÏ°í Àִٴ°ÍÀ» ¾Ë ÇÊ¿ä°¡ ÀÖ´Ù (section 3.8 of the Spring documentation ÂüÁ¶). ÀÌ ¸»Àº ½ºÇÁ¸µÀÌ »ý¼ºÇÏ´Â ºóÀº SqlSessionFactoryBean ±×ÀÚü°¡ ¾Æ´Ï¸ç, ÆÑÅ丮°¡ ¸®ÅÏÇØÁÖ´Â °´Ã¼´Â ±× ÆÑÅ丮ÀÇ getObject()¸¦ È£ÃâÇÑ °á°ú¶ó´Â ¶æÀÌ´Ù. ÀÌ °æ¿ì ½ºÇÁ¸µÀº ¾îÇø®ÄÉÀÌ¼Ç ½ÃÀ۽ÿ¡ SqlSessionFactory ¸¦ »ý¼ºÇÏ°í ±×À̸§Àº sqlSessionFactory À¸·Î ÀúÀåÇÏ°Ô µÈ´Ù. ÀÚ¹Ù ÄÚµå·Î ³ªÅ¸³»¸é ´ÙÀ½°ú °°À»°ÍÀÌ´Ù.

SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
SqlSessionFactory sessionFactory = factoryBean.getObject();
//ÆÑÅ丮ÀÇ getObject()¸¦ È£ÃâÇÑ °á°ú°¡ ºóÀ¸·Î »ý¼ºµÈ´Ù.

ÀϹÝÀûÀÎ MyBatis-Spring »ç¿ë½Ã¿¡´Â SqlSessionFactoryBean ȤÀº »óÀÀÇÏ´Â SqlSessionFactory ¸¦ Á÷Á¢ »ç¿ëÇÒÀÏÀº ¾øÀ»°ÍÀÌ´Ù. ´ë½Å ¼¼¼Ç ÆÑÅ丮´Â MapperFactoryBeans³ª ´Ù¸¥ DAOµé(SqlSessionDaoSupportÀ» »ó¼Ó¹ÞÀº)¿¡°Ô ÀÎÁ§Æ® µÉ°ÍÀÌ´Ù.

- ¼Ó¼ºµé

SqlSessionFactory ´Â ÇÊ¿ä·Î ÇÏ´Â ÇϳªÀÇ ¼Ó¼ºÀ» °¡Áö´Âµ¥, ¹Ù·Î JDBC DataSource ÀÌ´Ù. ÀÌ°ÍÀº ´Ù¸¥ ½ºÇÁ¸µ µ¥ÀÌÅÍ ¿¬°á°ú ¸¶Âù°¡Áö·Î ¾î¶²°ÍÀÌµç ¼³Á¤µÉ ¼ö ÀÖ´Ù. ¶ÇÇϳª ÀϹÝÀûÀÎ ¼Ó¼ºÀÌ Àִµ¥ MyBatis XML ¼³Á¤ ÆÄÀÏÀÇ À§Ä¡¸¦ ¼³Á¤ÇÏ´Â configLocation ÀÌ´Ù. ÀÌ°ÍÀÌ ÇÊ¿äÇÑ ÀÌÀ¯´Â MyBatis ¼³Á¤ÀÌ º¯°æµÉ ÇÊ¿ä°¡ ÀÖ´Â °æ¿ìÀÌ´Ù. ¶ÇÇÑ ¸¸¾à µ¿ÀÏÇÑ Å¬·¡½º Æнº»ó¿¡ ¸ÊÆÛ XMLÆÄÀÏ°ú ¸ÊÆÛ Å¬·¡½º°¡ Á¸ÀçÇÏÁö ¾Ê´Â °æ¿ì¿¡µµ ÀÌ ¼³Á¤ÆÄÀÏÀÌ ÇÊ¿äÇÏ´Ù. ÀÌ·± °æ¿ì¶ó¸é 2°¡ÁöÀÇ ¿É¼ÇÀÌ Á¸ÀçÇϴµ¥ ù°´Â <mapper> ¼½¼ÇÁöÁ¤À¸·Î ¼öµ¿À¸·Î MyBatis XML ¸ÊÆÛ ÆÄÀϵéÀ» ÁöÁ¤Çϴ°ÍÀÌ°í, µÎ¹ø° ¿É¼ÇÀº ÆÑÅ丮 ºóÀÇ ¼Ó¼ºÀÎ mapperLocations À» »ç¿ëÇϴ°ÍÀÌ´Ù.

mapperLocations ´Â ¸®¼Ò½º À§Ä¡µéÀÇ ¸®½ºÆ®¸¦ ÇÊ¿ä·Î ÇÑ´Ù. ÀÌ ¼Ó¼ºÀº MyBatis XML ¸ÊÆÛ ÆÄÀÏÀÇ À§Ä¡¸¦ ÁöÁ¤Çϱâ À§ÇØ »ç¿ëµÉ¼ö ÀÖ´Ù. °ªÀº µð·ºÅ丮ÀÇ ¸ðµç ÆÄÀÏÀ» ·ÎµåÇϰųª, ±âº» À§Ä¡·ÎºÎÅÍ Àç±ÍÀûÀ¸·Î ¸ðµç °æ·Î¸¦ ã°Ô²û Ant½ºÅ¸ÀÏÀÇ ÆÐÅÏÀ» Æ÷ÇÔÇÒ¼ö Àִµ¥, ¿¹¸¦ µé¸é ´ÙÀ½°ú °°´Ù.

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
</bean>

ÀÌ°ÍÀº Ŭ·¡½º Æнº·ÎºÎÅÍ sample.config.mappers ÆÐÅ°Áö¿Í ±× ¼­ºê ÆÐÅ°Áöµé¿¡ ÀÖ´Â ¸ðµç MyBatis ¸ÊÆÛ XML ÆÄÀϵéÀ»  ·Îµå ÇÒ °ÍÀÌ´Ù.

********************************************************************************
Æ®·£Àè¼Ç
********************************************************************************
MyBatis-Spring À» »ç¿ëÇÏ´Â ÁÖµÈ ÀÌÀ¯ÁßÀÇ Çϳª´Â MyBatis¸¦ ½ºÇÁ¸µ Æ®·£Àè¼Ç¿¡ Âü¿©ÇÏ°Ô ÇØÁֱ⠶§¹®ÀÌ´Ù. MyBatis¸¦ À§ÇÑ Æ¯º°ÇÑ Æ®·£Àè¼Ç ¸Å´ÏÁ®¸¦ ¸¸µé±â º¸´Ù´Â, MyBatis-Spring´Â ±âÁ¸ ½ºÇÁ¸µÀÇ DataSourceTransactionManager ¸¦ È°¿ëÇÏ°í ÀÖ´Ù. ½ºÇÁ¸µ Æ®·£Àè¼Ç ¸Å´ÏÁ®°¡ ¼³Á¤ÀÌ µÇ¸é, ÀϹÝÀûÀ¸·Î ÇÏ´ø°Íó·³ ½ºÇÁ¸µ¿¡¼­ÀÇ Æ®·£Àè¼ÇÀ» ¼³Á¤ÇÒ¼ö ÀÖ´Ù. @Transactional ¾î³ëÅ×À̼ǵé°ú AOP ½ºÅ¸ÀÏÀÇ ¼³Á¤µé ¸ðµÎ Áö¿øµÈ´Ù. Æ®·£Àè¼Ç ±â°£µ¿¾È ÇϳªÀÇ SqlSession °´Ã¼°¡ »ý¼ºµÇ¾î »ç¿ëµÈ´Ù. ÀÌ ¼¼¼ÇÀº Æ®·£Àè¼ÇÀÌ ¿Ï·áµÉ¶§ ÀûÀýÇÏ°Ô, Ä¿¹Ô ȤÀº ·Ñ¹éó¸® µÉ°ÍÀÌ´Ù. MyBatis-Spring Àº ÀÏ´Ü ¼³Á¤ÀÌ µÇ¸é Æ®·£Àè¼ÇµéÀ» Åõ¸íÇÏ°Ô °ü¸®ÇÒ°ÍÀÌ´Ù. »ç¿ëÀÚÀÇ DAO Ŭ·¡½ºµé¿¡°Ô Ãß°¡ÀûÀÎ ÄÚµå´Â ºÒÇÊ¿äÇÏ´Ù.

- Standard Configuration

½ºÇÁ¸µ Æ®·£Àè¼Ç 󸮸¦ È°¼ºÈ­ ½ÃÅ°±â À§Çؼ­´Â, °£´ÜÇÏ°Ô DataSourceTransactionManager ¸¦ ½ºÇÁ¸µ ¼³Á¤ ÆÄÀÏ¿¡ »ý¼ºÇÏ¸é µÈ´Ù.

<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
</bean>

Æ®·£Àè¼Ç ¸Å´ÏÁ®¿¡ ¼³Á¤µÈ DataSource´Â SqlSessionFactoryBean »ý¼º½Ã¿¡ ¼³Á¤Çß´ø °Í°ú µ¿ÀÏÇؾ߸¸ ÇÑ´Ù. ¸¸¾à Ʋ¸®´Ù¸é Æ®·£Àè¼Ç °ü¸®´Â ÀÛµ¿ÇÏÁö ¾ÊÀ»°ÍÀÌ´Ù.

- ÄÁÅ×ÀÌ³Ê °ü¸® Æ®·£Àè¼Çµé

¸¸¾à JEE container ¸¦ »ç¿ëÁßÀÌ°í ½ºÇÁ¸µÀÌ ÄÁÅ×ÀÌ³Ê °ü¸® Æ®·£Àè¼Çµé(CMT) ¿¡ Âü°¡ÇÏ°Ô ÇÏ·Á¸é,JtaTransactionManager ȤÀº ƯÁ¤ ÄÁÅ×À̳ʿ¡ ´ëÇÑ ±×°ÍÀÇ ¼­ºê Ŭ·¡½ºÁßÀÇ Çϳª·Î ½ºÇÁ¸µ ¼³Á¤À» ÇØÁà¾ß ÇÑ´Ù. ÀÌ°ÍÀ» Çϱâ À§ÇÑ °¡Àå ½¬¿î ¹æ¹ýÀº ½ºÇÁ¸µ Æ®·£Àè¼Ç ³×ÀÓ½ºÆäÀ̽º¸¦ »ç¿ëÇÏ´Â °ÍÀÌ´Ù.

<tx:jta-transaction-manager />

ÀÌ ¼³Á¤À¸·Î,  MyBatis ´Â CMT·Î ¼³Á¤µÈ ¿©Å¸ ´Ù¸¥ ½ºÇÁ¸µ Æ®·£Àè¼Ç 󸮵Ǵ ¸®¼Ò½ºµé°ú °°ÀÌ µ¿ÀÛÇÒ°ÍÀÌ´Ù. ½ºÇÁ¸µÀº ÀÚµ¿À¸·Î, Á¸ÀçÇÏ´Â ÄÁÅ×ÀÌ³Ê Æ®·£Àè¼ÇÀ» »ç¿ëÇÒ°ÍÀÌ°í  SqlSessionÀ» ±×°Í¿¡ µ¡ºÙÀÏ°ÍÀÌ´Ù.  ¸¸¾à Æ®·£Àè¼ÇÀÌ ½ÃÀÛ ¾ÈµÈ »óÅ¿¡¼­ Æ®·£Àè¼Ç ¼³Á¤À» ±âº»À¸·Î ±×°Í(Æ®·£Àè¼Ç)ÀÌ ÇÊ¿äÇÑ °æ¿ì¸é, ½ºÇÁ¸µÀº »õ·Î¿î ÄÁÅ×ÀÌ³Ê °ü¸® Æ®·£Àè¼ÇÀ» ½ÃÀÛÇÒ°ÍÀÌ´Ù.
¸¸¾à CMT¸¦ ¿øÇÏ°í ½ºÇÁ¸µ Æ®·£Àè¼Ç °ü¸®¸¦ ¿øÇÏÁö ¾Ê´Â´Ù¸é, ¾î¶°ÇÑ ½ºÇÁ¸µ Æ®·£Àè¼Ç ¼³Á¤µµ Çؼ­´Â ¾ÈµÈ´Ù.  ¶ÇÇÑ ±âº»MyBatis °ü¸® Æ®·£Àè¼Ç ÆÑÅ丮¸¦ »ç¿ëÇϱâ À§ÇØ SqlSessionFactoryBeanÀ» ¼³Á¤ÇؾßÇÑ´Ù.

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="transactionFactory">
    <bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" />
  </property>
</bean>

- Äڵ带 ÅëÇÑ Æ®·£Àè¼Ç °ü¸®

MyBatis SqlSession Àº ƯÁ¤ ¸Þ¼­µå¸¦ ÅëÇؼ­ Æ®·£Àè¼ÇµéÀ» ÇÁ·Î±×·¡¹ÖÀûÀ¸·Î ´Ù·ê¼ö ÀÖ´Ù. ÇÏÁö¸¸ MyBatis-Spring¸¦ »ç¿ëÇÒ¶§´Â ´ç½ÅÀÇ beanµé¿¡°Ô´Â ½ºÇÁ¸µÀÌ °ü¸®Çϴ SqlSessionÀ̳ª ½ºÇÁ¸µÀÌ °ü¸®ÇÏ´Â ¸ÊÆÛ°¡ ÀÎÁ§Æ® µÉ°ÍÀÌ´Ù. Áï, ½ºÇÁ¸µÀÌ ¾ðÁ¦³ª ´ç½ÅÀÇ Æ®·£Àè¼ÇÀ» ´Ù·é´Ù´Â ÀǹÌÀÌ´Ù. ÀÌ·¯ÇÑ ½ºÇÁ¸µ °ü¸®ÇÏÀÇ SqlSession¿¡¼­´Â SqlSession.commit(), SqlSession.rollback() ȤÀº SqlSession.close()À» È£ÃâÇÒ¼ö ¾ø´Ù. ¸¸¾à È£ÃâÇÑ´Ù¸é UnsupportedOperationException ¿¹¿Ü°¡ ¹ß»ý ÇÒ °ÍÀÌ´Ù. ÀÌ·¯ÇÑ ¸Þ¼­µåµéÀº ÀÎÁ§Æ®µÈ ¸ÊÆÛ Å¬·¡½º¾È¿¡¼­ ³ëÃâÀÌ ¾ÈµÈ´Ù´Â°ÍÀ» ¾Ë±â ¹Ù¶õ´Ù. ´ç½ÅÀÇ JDBC connection's autocommit ¼³Á¤¿¡ »ó°ü¾øÀÌ, SqlSession µ¥ÀÌÅÍ ¸Þ¼­µå ½ÇÇà ȤÀº ½ºÇÁ¸µ Æ®·£Àè¼Ç ¿ÜºÎ¿¡¼­ÀÇ mapper ¸Þ¼­µå È£ÃâÀº ¸ðµÎ ÀÚµ¿ÀûÀ¸·Î Ä¿¹Ô µÉ °ÍÀÌ´Ù.

¸¸¾à ´ç½ÅÀÌ Æ®·£Àè¼ÇÀ» ÇÁ·Î±×·¥ÀûÀ¸·Î Á¦¾îÇÏ±æ ¿øÇÑ´Ù¸é,  ½ºÇÁ¸µ ·¹ÆÛ·±½º ¸Å´º¾ó 10.6 ÀåÀ» Âü°íÇϱ⠹ٶõ´Ù. ´ÙÀ½ ÄÚµå´Â 10.6.2 section¿¡ ±â¼úµÈ ºÎºÐÀε¥, PlatformTransactionManager ¸¦  »ç¿ëÇؼ­ ¼öµ¿À¸·Î Æ®·£Àè¼ÇÀ» ´Ù·ç°í ÀÖ´Ù.

DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);

try {
    userMapper.insertUser(user);
}
catch (MyException ex) {
    txManager.rollback(status);
    throw ex;
}
txManager.commit(status);

ÀÌÄÚµå´Â mapper¸¦ »ç¿ëÇÏ°í ÀÖÁö¸¸,  SqlSessionÀ» ÀÌ¿ëÇؼ­µµ µ¿ÀÛÇÒ°ÍÀÌ´Ù.


********************************************************************************
SqlSession »ç¿ë
********************************************************************************
MyBatis ¿¡¼­´Â (±âº»ÀûÀÎ MyBatis¸¦ »ç¿ëÇÑ´Ù¸é, Áï MyBatis-SpringÀ» »ç¿ëÇϴ°ÍÀÌ ¾Æ´Ô!) SqlSession À» »ý¼ºÇϱâ À§ÇØ SqlSessionFactory ¸¦ »ç¿ëÇÑ´Ù. ±×¸®°í ÀÏ´Ü ¼¼¼ÇÀ» ¾ò°ÔµÇ¸é, ±×°ÍÀ» ÀÌ¿ëÇؼ­ ´ç½ÅÀÇ ¸ÊÇÎµÈ ±¸¹®µéÀ» ½ÇÇàÇÏ°í ¿¬°áµéÀ» Ä¿¹ÔÇÏ°í ·Ñ¹éÇϸç, ´õÀÌ»ó ÇÊ¿äÇÏÁö ¾Ê´Ù¸é ÃÖÁ¾ÀûÀ¸·Î ¼¼¼ÇÀ» ´Ý°Ô µÈ´Ù.

MyBatis-Spring¿¡¼­´Â SqlSessionFactory¸¦ Á÷Á¢ »ç¿ëÇÒ ÇÊ¿ä°¡ ¾ø´Ù. ½ºÇÁ¸µÀÇ Æ®·£Àè¼Ç ¼³Á¤¿¡ ±Ù°ÅÇؼ­ ÀÚµ¿À¸·Î Ä¿¹Ô, ·Ñ¹é, ¼¼¼ÇÁ¾·á°¡ ÀÌ·ç¾îÁö¸ç ¾²·¹µå¿¡µµ ¾ÈÀüÇÑ SqlSession ÀÌ ´ç½ÅÀÇ ºóµé¿¡°Ô ÀÎÁ§Æ® µÉ¼öÀֱ⠶§¹®ÀÌ´Ù.

¶ÇÇÑ, ÀϹÝÀûÀ¸·Î SqlSession À» Á÷Á¢ »ç¿ëÇÒ ÇÊ¿ä°¡ ¾ø´Ù´Â°Íµµ ¾Ë¾ÆµÑ ÇÊ¿ä°¡ ÀÖ´Ù. ´ëºÎºÐÀÇ °æ¿ì, ´ÜÁö ÇÊ¿äÇÑ °ÍÀº ´ç½ÅÀÇ ºóµé¿¡°Ô ¸ÊÆÛµéÀ» ÀÎÁ§Æ® ÇÒ MapperFactoryBean ÀÏ °ÍÀÌ´Ù(ÀÌ°ÍÀº ´ÙÀ½ Àå¿¡ ¼³¸íµÈ´Ù).


- SqlSessionTemplate

ÀÌ°ÍÀº MyBatis-SpringÀÇ ½ÉÀåÀÌ´Ù. ÀÌ Å¬·¡½º´Â MyBatis SQL methodµéÀ» È£ÃâÇϸ鼭 ¿¹¿ÜµéÀ» ÀüȯÇϸ鼭 MyBatis SqlSession µéÀ» °ü¸®Çϴ åÀÓÀÌ ÀÖ´Ù. SqlSessionTemplate Àº ¾²·¹µå ¾ÈÀüÇÏ°í ¿©·¯ DAOµé »çÀÌ¿¡¼­ °øÀ¯ °¡´ÉÇÏ´Ù.

getMapper() È£Ãâ·Î ¾òÀº ¸ÊÆÛÀÇ SQL ¸Þ¼­µåµéÀ» È£ÃâÇÒ¶§, SqlSessionTemplate Àº SqlSession ÀÌ ÇöÀç ½ºÇÁ¸µ Æ®·£Àè¼Ç¿¡ ¿¬°üµÈ °ÍÀÓÀ» º¸ÀåÇÒ°ÍÀÌ´Ù. ¶ÇÇÑ, ÇÊ¿ä¿¡ µû¶ó ¼¼¼ÇÀÇ Á¾·á, Ä¿¹Ô, ·Ñº¤À» Æ÷ÇÔÇÏ´Â ¼¼¼Ç »ý¸íÁֱ⸦ °ü¸®ÇÑ´Ù.

SqlSessionTemplate Àº SqlSessionÀ» ±¸ÇöÇÏ°í Àִµ¥, Äڵ忡 Á¸ÀçÇÏ´Â ±âÁ¸ SqlSession »ç¿ë¿¡ ´ëÇØ °£´ÜÈ÷ ±³Ã¼ µÈ´Ù. ±âº» MyBatis ±¸ÇöÀÎ DefaultSqlSession ´ë½Å SqlSessionTemplateÀÌ Ç×»ó »ç¿ëµÇ¾îÁ®¾ß Çϴµ¥ ½ºÇÁ¸µ Æ®·£Àè¼Ç¿¡ ÅÛÇø´ÀÌ Âü°¡ °¡´ÉÇÏ°í ¿©·¯°³ÀÇ ÀÎÁ§Æ® µÇ¾îÁø ¸ÊÆÛ Å¬·¡½º¿¡ ÀÇÇØ »ç¿ëµÉ¶§ ¾²·¹µå ¾ÈÀüÇϱ⠶§¹®ÀÌ´Ù. µ¿ÀÏ ¾îÇø®ÄÉÀ̼ǿ¡¼­ ÀÌ µÎ°¡Áö Ŭ·¡½º(DefaultSqlSession °ú SqlSessionTemplate)¸¦ È¥¿ëÇϴ°ÍÀº µ¥ÀÌÅÍ ¹«°á¼º ¹®Á¦¸¦ ¾ß±âÇÒ¼ö ÀÖ´Ù.

SqlSessionTemplate Àº »ý¼ºÀÚ ÀÎÀÚ·Î SqlSessionFactory ¸¦ »ç¿ëÇؼ­ »ý¼ºµÉ¼ö ÀÖ´Ù.

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

ÀÌ beanÀº ÀÌÁ¦ ´ç½ÅÀÇ DAO ºóµé¿¡°Ô Á÷Á¢ ÀÎÁ§Æ®µÉ¼ö ÀÖ´Ù. ´ç½ÅÀÇ bean¿¡ SqlSession ÇÁ·¯ÆÛƼ°¡ ´ÙÀ½Ã³·³ ÇÊ¿äÇÏ´Ù.

(´ÙÀ½ ÄÚµå´Â Á÷Á¢ SqlSession¸¦ »ç¿ëÇÏ´Â °æ¿ìÀÌ´Ù. Mapper¸¦ ÀÌ¿ë ÇÑ´Ù¸é ´Þ¶óÁú°ÍÀÌ´Ù)
public class UserDaoImpl implements UserDao
{
private SqlSession sqlSession;

    public void setSqlSession(SqlSession sqlSession)
   {
        this.sqlSession = sqlSession;
    }

    public User getUser(String userId)
    {
        return
        (User) sqlSession.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser",
         userId);
    }
}

±×¸®°í ´ÙÀ½Ã³·³ SqlSessionTemplate À» ÀÎÁ§Æ® ÇÑ´Ù.

<bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">
  <property name="sqlSession" ref="sqlSession" />
</bean>

SqlSessionTemplate Àº ¶ÇÇÑ ExecutorTypeÀ» ÀÔ·ÂÀÎÀÚ·Î °¡Áö´Â »ý¼ºÀÚ¸¦ °¡Áö°í ÀÖ´Ù. ´ÙÀ½Ã³·³ batch SqlSession À» »ý¼ºÇÒ¼ö ÀÖ°Ô ÇØÁØ´Ù.

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory" />
  <constructor-arg index="1" value="BATCH" />
</bean>

ÀÌÁ¦ ¸ðµç ´ç½ÅÀÇ ±¸¹®µéÀº ¹èÄ¡·Î µ¿ÀÛÇÒ°ÍÀÌ°í DAO¿¡¼­ ´ÙÀ½Ã³·³ ÄÚµùµÉ¼ö ÀÖ´Ù.

public void insertUsers(User[] users) {
    for (User user : users) {
     sqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", user);
    }
}

ÀÌ·± ¼³Á¤ ½ºÅ¸ÀÏÀº SqlSessionFactory¸¦ À§ÇÑ ±âº» ¼³Á¤°ú »óÀÌÇÑ ½ÇÇà ¸Þ¼­µå°¡ ¿ä±¸µÇ´Â °æ¿ì¸¸ ÇÊ¿ä ÇÏ´Ù´Â °ÍÀ» ¾Ë ÇÊ¿ä°¡ ÀÖ´Ù. ÀÌ·¯ÇÑ ¹æ½Ä¿¡¼­ ÀØÁö ¸»¾Æ¾ß ÇÒ°ÍÀº ÀÌ ¸Þ¼­µå°¡ È£ÃâµÉ¶§ ¼­·Î´Ù¸¥ ExecutorType¿¡ ÁøÇàÁßÀÎ ±âÁ¸ Æ®·£Àè¼ÇÀÌ Á¸ÀçÇÏ¸é ¾ÈµÈ´Ù´Â Á¡ÀÌ´Ù.  º°µµ Æ®·£Àè¼Ç¿¡¼­ µ¿ÀÛÇϴ ´Ù¸¥ ExecutorTypeÀ¸·Î SqlSessionTemplateµéÀ» È£Ãâ ÇÏ´øÁö (Áï, PROPAGATION_REQUIRES_NEW »ç¿ëÀ¸·Î) ¾Æ´Ï¸é ¿ÏÀüÈ÷ Æ®·£Àè¼Ç ¹Û¿¡¼­ µ¿ÀÛÇÏ°Ô ÇØ¾ß ÇÑ´Ù.

- SqlSessionDaoSupport

SqlSessionÀ» Á¦°øÇØÁÖ´Â Ãß»ó Ŭ·¡½ºÀÌ´Ù. ´ÙÀ½ ¿¹ ó·³ getSqlSession() À» È£ÃâÇؼ­ SqlSessionTemplate À» ¾òÀ»¼ö ÀÖ°í, SQL ¸Þ¼­µåµéÀ» ¼öÇàÇÒ¼ö ÀÖ´Ù.

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    public User getUser(String userId) {
        return (User) getSqlSession().
            selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
    }
}

ÀϹÝÀûÀ¸·Î ÀÌ Å¬·¡½ºº¸´Ù MapperFactoryBean ÀÌ ¼±È£µÈ´Ù(Ãß°¡ Äڵ尡 ºÒÇÊ¿äÇϱ⠶§¹®). ÇÏÁö¸¸, ´ç½ÅÀÇ DAO ¿¡¼­ ´Ù¸¥ ºñ-MyBatis ÀÛ¾÷À» ¼öÇàÇÒ ÇÊ¿ä°¡ ÀÖÀ»¶§³ª, concrete Ŭ·¡½ºµéÀÌ ÇÊ¿äÇÒ¶§ À¯¿ëÇÏ´Ù.

SqlSessionDaoSupport ´Â sqlSessionFactory ¿Í sqlSessionTemplate ¼Ó¼ºÁß Çϳª¸¸ ÇÊ¿ä·Î ÇÑ´Ù. Spring¿¡ ÀÇÇØ autowired µÇ°Å³ª, ¸í½ÃÀûÀ¸·Î ¼³Á¤ °¡´ÉÇÏ´Ù. ¸¸¾à µÎ ¼Ó¼ºÀÌ ¸ðµÎ ¼³Á¤ÀÌ µÈ °æ¿ì¶ó¸é, sqlSessionFactory ´Â ¹«½ÃµÈ´Ù.
´ÙÀ½ ¿¹¿¡¼­ UserDaoImpl °¡ ¸¸¾à SqlSessionDaoSupportÀÇ ÀÚ½Ä Å¬·¡½º¶ó°í Çϸé, ½ºÇÁ¸µ¿¡¼­ ´ÙÀ½Ã³·³ ¼³Á¤ µÇ¾îÁú ¼ö ÀÖ´Ù.

<bean id="userMapper" class="org.mybatis.spring.sample.mapper.UserDaoImpl">
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

********************************************************************************
Mappers ÀÎÁ§ÆÃ
********************************************************************************
SqlSessionDaoSupport ³ª SqlSessionTemplate »ç¿ëÇؼ­ data access objects (DAOs)¸¦ ¼öµ¿À¸·Î ÄÚµùÇÏ´Â °Íº¸´Ù, Mybatis-Spring ´Â MapperFactoryBean À̶ó´Â ÇÁ·Ï½Ã ÆÑÅ丮¸¦ Á¦°øÇÏ°í ÀÖ´Ù. ÀÌŬ·¡½º´Â data mapper ÀÎÅÍÆäÀ̽ºµéÀ» ´Ù¸¥ ºóµé¿¡°Ô Á÷Á¢ ÀÎÁ§Æ® ÇÒ¼ö ÀÖ°Ô ÇØÁØ´Ù. ¸ÊÆÛµéÀ» ÀÌ¿ëÇÒ¶§´Â DAOµéÀ» È£ÃâÇß´ø°Í°ú µ¿ÀÏÇÏ°Ô È£ÃâÇÏÁö¸¸, Mybatis-SpringÀÌ ÇÁ·Ï½Ã¸¦ »ý¼ºÇÒ°ÍÀ̱⠶§¹®¿¡, DAO ±¸ÇöÀ» À§Çؼ­ ¾î¶°ÇÑ ÄÚµùµµ ÇÊ¿äÇÏÁö ¾Ê´Ù.
ÀÎÁ§Æ®µÇ¾îÁø ¸ÊÆÛµé ÀÎÇØ ´ç½ÅÀÇ ÄÚµå´Â MyBatis, ½ºÇÁ¸µ ȤÀº MyBatis-Spring°ú Á÷Á¢ÀûÀÎ ÀÇÁ¸°ü°è¸¦ °®Áö ¾Ê°Ô µÉ°ÍÀÌ´Ù.

MapperFactoryBean °¡ »ý¼ºÇÏ´Â ÇÁ·Ï½Ã´Â ¼¼¼ÇÀ» ¿­°í ´Ý´Â °ÍÀ» ´Ù·ç¸ç ¶ÇÇÑ ¿¹¿ÜµéÀ» ½ºÇÁ¸µÀÇ DataAccessException ·Î º¯È¯ÇÑ´Ù. ´õ ³ª¾Æ°¡ ¸¸¾à ÇÊ¿äÇÑ°æ¿ì ±× ÇÁ·Ï½Ã´Â »õ·Î¿î Spring Æ®·£Àè¼ÇÀ» ½ÃÀÛÇϰųª, Æ®·£Àè¼ÇÀÌ È°¼ºÈ­ µÇ¾î ÀÖ´Â °æ¿ì, ±âÁ¸ Æ®·£Àè¼Ç¿¡ Âü°¡ ÇÒ°ÍÀÌ´Ù.

- MapperFactoryBean

µ¥ÀÌÅÍ ¸ÊÆÛ´Â ½ºÇÁ¸µ¿¡ ´ÙÀ½°ú °°ÀÌ Ãß°¡µÈ´Ù.

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

MapperFactoryBean ´Â UserMapper ¸¦ ±¸ÇöÇÏ´Â ÇÁ·Ï½Ã Ŭ·¡½º¸¦ »ý¼ºÇÏ°í, ±×°ÍÀ» ¾îÇø®ÄÉÀ̼ǿ¡ ÀÎÁ§Æ® ÇÑ´Ù. ÇÁ·Ï½Ã´Â ·±Å¸Àӽÿ¡ »ý¼ºµÇ¹Ç·Î, ÁöÁ¤µÈ ¸ÊÆÛ´Â ½ÇÁ¦ ±¸Çö Ŭ·¡½º°¡ ¾Æ´Ñ, ÀÎÅÍÆäÀ̽º¿©¾ß¸¸ ÇÑ´Ù.
¸¸¾à UserMapper °¡ »óÀÀÇÏ´Â MyBatis XML mapper ÆÄÀÏÀ» °¡Áö°í ÀÖ°í, mapper interface ¿Í µ¿ÀÏÇÑ Å¬·¡½º Æнº¿¡ Á¸ÀçÇÑ´Ù¸é, ÀÚµ¿ÀûÀ¸·Î MapperFactoryBean ¿¡ ÀÇÇØ Çؼ®µÉ°ÍÀÌ´Ù. ¸¸¾à mapper XML ÆÄÀÏÀÌ ´Ù¸¥ Ŭ·¡½º Æнº À§Ä¡»ó¿¡ ÀÖ´Â °æ¿ì¸¸ ¾Æ´Ï¸é MyBatis ¼³Á¤ ÆÄÀÏ¿¡ ¸ÊÆÛ¸¦ ÁöÁ¤ÇÒ ÇÊ¿ä´Â ¾ø´Ù.( SqlSessionFactoryBean ÀÇ configLocation ¼Ó¼º ÂüÁ¶)

MapperFactoryBean ´Â SqlSessionFactory ³ª SqlSessionTemplate¸¦ ÇÊ¿ä·Î Çϴ°Ϳ¡ À¯ÀÇÇϱ⠹ٶõ´Ù. À̰͵éÀº °¢°¢ÀÇ sqlSessionFactory  ³ª sqlSessionTemplate  ¼Ó¼ºÀ̳ª ½ºÇÁ¸µ¿¡ ÀÇÇØ autowired ¿¡ ÀÇÇØ ¼³Á¤µÇ¾îÁú¼ö ÀÖ´Ù.¸¸¾à µÑ´Ù ¼³Á¤ÀÌ µÈ´Ù¸é SqlSessionFactory °¡ ¹«½ÃµÈ´Ù. SqlSessionTemplate °¡ session factory ¼³Á¤À» ÇÊ¿ä·Î ÇϹǷÎ, ±× ÆÑÅ丮´Â
MapperFactoryBean¿¡ ÀÇÇØ »ç¿ëµÈ´Ù.

ÀÌÁ¦ ¸ÊÆÛµéÀ» ´ç½ÅÀÇ ºñÁî´Ï½º/¼­ºñ½º °´Ã¼µé¿¡°Ô Á÷Á¢ ÀÎÁ§Æ® °¡´ÉÇÏ´Ù.
<bean id="fooService" class="org.mybatis.spring.sample.mapper.FooServiceImpl">
  <property name="userMapper" ref="userMapper" />
</bean>

ÀÌ beanÀº ¾îÇø®ÄÉÀÌ¼Ç ·ÎÁ÷¿¡¼­ Á÷Á¢ »ç¿ë°¡´ÉÇÏ´Ù.
public class FooServiceImpl implements FooService
{
   private UserMapper userMapper;

   public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public User doSomeBusinessStuff(String userId) {
        return this.userMapper.getUser(userId);
    }
}

ÀÌ Äڵ忡´Â ¾î¶°ÇÑ SqlSession ³ª MyBatis ÂüÁ¶µµ ¾ø´Ù´Â°ÍÀ» ÁÖ¸ñÇϱ⠹ٶõ´Ù. ¶ÇÇÑ ¼¼¼ÇÀ» »ý¼ºÇÏ°í, ¿­°í ´ÝÀ» ÇÊ¿äµµ ¾ø´Ù. MyBatis-Spring°¡ ±× ÀϵéÀ» ÇØÁÙ °ÍÀÌ´Ù.

- MapperScannerConfigurer

¸ðµç ¸ÊÆÛµéÀ» ½ºÇÁ¸µ XMLÆÄÀÏ¿¡ µî·ÏÇÒ ÇÊ¿ä¾øÀÌ, MapperScannerConfigurer ¸¦ ÀÌ¿ëÇؼ­ MapperFactoryBean ó·³ ´ç½ÅÀÇ ¸ÊÆÛµéÀ» À§ÇØ Å¬·¡½º Æнº¸¦ °Ë»öÇÏ°í ÀÚµ¿ÀûÀ¸·Î ±×°ÍµéÀ» ¼³Á¤ÇÏ°Ô ÇÒ ¼ö ÀÖ´Ù. ´ÙÀ½Ã³·³ ½ºÇÁ¸µ ¼³Á¤¿¡ Ãß°¡ÇÏ¸é µÈ´Ù.

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>

basePackage ¼Ó¼ºÀº ¸ÊÆÛ ÀÎÅÍÆäÀ̽º ÆÄÀϵéÀÇ ±âº» ÆÐÅ°Áö¸¦ ¼³Á¤Çϴ°ÍÀÌ´Ù. ¼¼¹ÌÄÝ·ÐÀ̳ª ÄÞ¸¶ ±¸ºÐÀÚ¸¦ ÀÌ¿ëÇؼ­ ÇϳªÀÌ»óÀÇ ÆÐÅ°Áö¸¦ ¼³Á¤ÇÒ¼ö ÀÖ´Ù. ±×·¯¸é ÁöÁ¤µÈ ÆÐÅ°ÁöºÎÅÍ ½ÃÀÛÇؼ­ Àç±ÍÀûÀ¸·Î mapper°¡ °Ë»öµÈ´Ù.

SqlSessionFactory ³ª SqlSessionTemplate ¸¦ ÁöÁ¤ÇÒ ÇÊ¿ä°¡ ¾ø´Â°Í¿¡ ÁÖ¸ñÇϱ⠹ٶõ´Ù. ¿Ö³ÄÇϸé MapperScannerConfigurer °¡ autowiredµÉ¼ö ÀÖ´Â MapperFactoryBeanµéÀ» »ý¼ºÇÒ °ÍÀ̱⠶§¹®ÀÌ´Ù.
±×·±µ¥ ¸¸¾à Çϳª ÀÌ»óÀÇ µ¥ÀÌÅÍ ¼Ò½º¸¦ »ç¿ëÁßÀ̶ó¸é, autowire ´Â ÀÛµ¿ÇÏÁö ¾ÊÀ» ¼ö ÀÖ´Ù. ÀÌ °æ¿ì¿¡´Â sqlSessionFactoryBeanName ³ª sqlSessionTemplateBeanName ¼Ó¼ºµéÀ» »ç¿ëÇؼ­ »ç¿ëÇÒ ¿Ã¹Ù¸¥ bean À̸§À» ÁöÁ¤ ÇÒ ¼ö ÀÖ´Ù. ÁÖÀÇÇÒ°ÍÀº beanÀÇ ÂüÁ¶°¡ ¾Æ´Ï¶ó À̸§ÀÌ ÇÊ¿äÇÏ´Ù´Â °ÍÀÌ´Ù. ±× ¶§¹®¿¡ ÀϹÝÀûÀÎ ref ´ë½Å value ¾îÆ®¸®ºäÆ®°¡ »ç¿ëµÈ´Ù.

  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

¾Ë¾ÆµÑ°Í : sqlSessionFactoryBean °ú sqlSessionTemplateBean ¼Ó¼ºÀº MyBatis-Spring 1.0.2 ¹öÀü±îÁö °¡´ÉÇß´ø ±×³É ¿É¼ÇÀÏ »ÓÀ̾ú´Ù. ÇÏÁö¸¸ MapperScannerConfigurer°¡ ±âµ¿ ÇÁ·Î¼¼½ºÀÇ Ãʱ⿡ ±¸µ¿µÈ´Ù°í ÇÒ¶§ PropertyPlaceholderConfigurer ¿¡ ºó¹øÇÑ ¿¡·¯°¡ ¹ß»ýÇß´Ù. ±×·¡¼­ ÀÌ ¼Ó¼ºµéÀº deprecated µÇ¾ú°í »õ·Î¿î ¼Ó¼º, Áï sqlSessionFactoryBeanName°ú sqlSessionTemplateBeanName »ç¿ëÀÌ ±ÇÀåµÈ´Ù.

MapperScannerConfigurer ´Â marker interface ÁöÁ¤ ȤÀº annotation ¿¡ ÀÇÇØ »ý¼ºµÈ ¸ÊÆÛµéÀ» ÇÊÅ͸µÇϴ°ÍÀ» Áö¿øÇÑ´Ù.
annotationClass ¼Ó¼ºÀº ã°íÀÚ ÇÏ´Â annotation À» ÁöÁ¤ÇÑ´Ù. markerInterface ¼Ó¼ºÀº ã°íÀÚ ÇÏ´Â ºÎ¸ð ÀÎÅÍÆäÀ̽º ¸¦ ÁöÁ¤ÇÑ´Ù. ¸¸¾à µÎ ¼Ó¼ºÀÌ ¸ðµÎ ÁöÁ¤µÇ¸é ÇÑÂÊ Á¶°Ç¿¡ ÀÏÄ¡ÇÏ´Â ¸ãÆÛµéÀÌ Ãß°¡µÈ´Ù. µðÆúÆ®·Î À̼ӼºµéÀº nullÀÌ´Ù. µû¶ó¼­ ±âº» ÆÐÅ°Áö¿¡ ÀÖ´Â ¸ðµç ÀÎÅÍÆäÀ̽ºµéÀÌ ¸ÊÆÛµé·Î ·Îµå µÉ°ÍÀÌ´Ù.

¹ß°ßµÈ ¸ÊÆÛµéÀº ½ºÇÁ¸µÀÇ ÀÚµ¿ °¨ÁöµÈ ÄÄÆÛ³ÍÆ®µéÀ» À§ÇÑ ±âº» ³×ÀÌ¹Ö Àü·«¿¡ ÀÇÇØ À̸§ÀÌ Á¤ÇØÁø´Ù(section 3.14.4 of the Spring manual). Áï, ¾î³ëÅ×À̼ÇÀÌ ¹ß°ß ¾ÈµÈ °æ¿ì, ¼Ò¹®ÀÚ·Î ±¸¼ºµÈ ¸ÊÆÛÀÇ Å¬·¡½º(non-qualified Ŭ·¡½º¸í,Áï ÆÐÅ°Áö Á¦¿ÜÇÑ ´Ü¼ø Ŭ·¡½º¸í¸¸)¸¦ À̸§À¸·Î »ç¿ëÇÒ°ÍÀÌ´Ù. ÇÏÁö¸¸ @Component ȤÀº JSR-330 @Named ¾î³ëÅ×À̼ÇÀÌ ¹ß°ßµÈ´Ù¸é ±×°ÍÀ¸ºÎÅÍ À̸§ÀÌ °áÁ¤µÈ´Ù.annotationClass ¼Ó¼º¿¡ org.springframework.stereotype.Component, javax.inject.Named (JSE 6 °æ¿ì¸¸) ȤÀº »ç¿ëÀÚ °íÀ¯ÀÇ annotation À» ¼³Á¤ÇÒ¼ö µµ Àִµ¥ ÀÌ·¯¸é annotation ÀÌ ¸¶Ä¿¿Í ³×ÀÓ ÇÁ·Î¹ÙÀÌ´õ·Î ¸ðµÎ µ¿ÀÛÇÏ°Ô µÈ´Ù.

********************************************************************************
MyBatis API »ç¿ëÇϱâ
********************************************************************************
MyBatis-Spring ¿¡¼­ MyBatis API¸¦ Á÷Á¢ »ç¿ëÇÏ´Â ¹æ¹ýÀÌ´Ù. ½ºÇÁ¸µ¿¡¼­ SqlSessionFactoryBean  À» ÀÌ¿ëÇؼ­ SqlSessionFactory ¸¦ »ý¼ºÇÏ°í, ÄÚµå»ó¿¡¼­ factory ¸¦ »ç¿ëÇÑ´Ù.

public class UserMapperSqlSessionImpl implements UserMapper {
    // SqlSessionFactory would normally be set by SqlSessionDaoSupport
    private SqlSessionFactory sqlSessionFactory;

    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    public User getUser(String userId) {
        // note standard MyBatis API usage - opening and closing the session manually
        SqlSession session = sqlSessionFactory.openSession();

        try {
            return
            (User) session.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser",
                                    userId);
        } finally {
            session.close();
        }
    }
}

ÀÌ ¹æ¹ýÀ» »ç¿ëÇÒ¶§´Â ÁÖÀǸ¦ ±â¿ï¿©¾ß ÇÑ´Ù. À߸øµÈ »ç¿ëÀ¸·Î ÀÎÇØ ·±Å¸ÀÓ ¿¡·¯°¡ ¹ß»ýÇϰųª, ½ÉÇÏ¸é µ¥ÀÌÅÍ ¹«°á¼º¿¡ ¹®Á¦¸¦ ÀÏÀ¸Å³¼öµµ ÀÖ´Ù. Á÷Á¢ API¸¦ »ç¿ëÇϱâ À§Çؼ­´Â ´ÙÀ½À» ¸í½ÉÇØ¾ß ÇÑ´Ù.

 - ÀÌ°ÍÀº ¾î¶°ÇÑ ½ºÇÁ¸µ Æ®·£Àè¼Çµé¿¡°Ôµµ Âü°¡ÇÏÁö ¾Ê´Â´Ù

 - ¸¸¾à SqlSession »ç¿ëÁßÀÎ DataSource ¸¦ Spring transaction manager µµ »ç¿ëÁßÀÌ°í,
   ÇöÀç ÁøÇàÁßÀÎ Æ®·£Àè¼ÇÀÌ Á¸ÀçÇÑ´Ù¸é ÀÌ ÄÚµå´Â ¿¹¿Ü¸¦ ´øÁú°ÍÀÌ´Ù.

 - MyBatisÀÇ DefaultSqlSession Àº ¾²·¹µå¿¡ ¾ÈÀüÇÏÁö ¾Ê´Ù. ¸¸¾à ±×°ÍÀ» ´ç½ÅÀÇ beanµé¿¡°Ô
   ÀÎÁ§Æ® ÇÑ´Ù¸é ¿¡·¯°¡ ¹ß»ýÇÒ°ÍÀÌ´Ù.

 - DefaultSqlSession ¸¦ »ç¿ëÇؼ­ ¸¸µé¾îÁø mapperµéµµ ¾²·¹µå¿¡ ¾ÈÀüÇÏÁö ¾Ê´Ù.
   ¸¸¾à ±×°ÍÀ» ´ç½ÅÀÇ beanµé¿¡°Ô ÀÎÁ§Æ® ÇÑ´Ù¸é ¿¡·¯°¡ ¹ß»ýÇÒ°ÍÀÌ´Ù.

 - SqlSessions ÀÌ finally ºí·°¿¡¼­ Ç×»ó ´ÝÇôÁö´ÂÁö È®ÀÎÇØ¾ß ÇÑ´Ù.

°£´ÜÇÑ °Ô½ÃÆÇ ¿¹Á¦¸¦ Âü°íÇϱ⠹ٶõ´Ù.



Ãâó: http://unabated.tistory.com/569 [¶ö¶ó¶ó]