SPRING
2016.11.03 / 10:06

½ºÇÁ¸µ°ú ¸¶À̹ÙƼ½º ¿¡¼­ ´ÙÁß µ¥ÀÌŸ¼Ò½º »ç¿ëÇϱâ

±â¸®¾Æºü
Ãßõ ¼ö 508

Spring °ú MyBatis (iBatis) ¸¦ °³¹ß ȯ°æÀ¸·Î »ç¿ëÇÒ °æ¿ì ¿©·¯ °³ÀÇ datasource ¸¦ ½á¾ß µÇ´Â °æ¿ì°¡ ÀÖ´Ù.

 

¿©·¯ °³ÀÇ data source ¿¡ ¿¬°áÇØ¾ß ÇÒ °æ¿ì Mybatis config¿Í mapper ¸¦ º°µµÀÇ ÆÐÅ°Áö·Î ºÐ¸®ÇÏ´Â°Ô °³ÀÎÀûÀ¸·Î´Â °ü¸®°¡ ¿ëÀÌÇÏ´Ù.

 

application context xml

´ÜÀ§ Å×½ºÆ® ¹× stand-alone ¿ëÀ̶ó Data Source ¼³Á¤ÀÌ WAS¿¡ ÀÖÁö ¾Ê°í spring context ¿¡ ÀÖ°í BoneCP ³ª c3p0 °°Àº Connection Pool À» »ç¿ëÇÏÁö ¾Ê°í ½ºÇÁ¸µÀÇ  SimpleDriverDataSource ·Î ¼³Á¤µÇ¾î ÀÖ´Ù.

database-context.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version="1.0" encoding="UTF-8"?>
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/jee
 
    <!-- multiple data source & sqlSessionFactory -->
    <bean id="ds-one" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@//db1.example.com:1521/ocrl" />
        <property name="username" value="user" />
        <property name="password" value="userpwd" />
    </bean>
    <bean id="ds-two" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />       
        <property name="url" value="jdbc:mysql://db2.example.com:3306/lesstif?useUnicode=true&amp;characterEncoding=utf8" />
        <property name="username" value="userid" />
        <property name="password" value="useriduserPwd" />
    </bean>
 
    <bean id="dsOneSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:mapperLocations="classpath:/com/example/mapper-one/*mapper.xml"
        p:configLocation="classpath:/com/example/dsone-mybatis-config.xml"
        p:dataSource-ref="ds-one" />
 
    <bean id="dsTwoSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:mapperLocations="classpath:/com/example/mapper-two/*mapper.xml"
        p:configLocation="classpath:/com/example/dstwo-mybatis-config.xml"
        p:dataSource-ref="ds-two" />
     
    <bean id="dsOneScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.example.mapper-one" />
  
    <bean id="dsTwoScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.example.mapper-two" />
 
    <bean id="dsOnetransactionManager"   class="org.springframework.jdbc.datasource.DataSourceTransactionManager"    p:dataSource-ref="ds-one" />
    <bean id="dsTwotransactionManager"   class="org.springframework.jdbc.datasource.DataSourceTransactionManager"    p:dataSource-ref="ds-two" />
 
    <context:component-scan base-package="com.example.service-one, com.example.service-two">
    </context:component-scan>
</beans>

 

ÀÌ »óÅ·Π¾îÇø®ÄÉÀ̼ÇÀ» ±¸µ¿ÇÏ¸é ´ÙÀ½°ú ºñ½ÁÇÑ ¿¡·¯°¡ ¹ß»ýÇÏ°í µðÇ÷ÎÀÌ°¡ ¾È µÉ °ÍÀÌ´Ù.

Ignoring bean creation exception on FactoryBean type check: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'myMapper' defined in file : Unsatisfied dependency expressed through bean property 'sqlSessionFactory': : 
No qualifying bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined: expected single matching bean but found 2: dsOneSqlSessionFactory, dsTwoSqlSessionFactory; 

 

¿¡·¯ ¸Þ½ÃÁö¸¦ º¸¸é µÎ °³ÀÇ SqlSessionFactory °¡ ÁöÁ¤µÇ¾î ÀÖ´Ù°í Ç¥½ÃµÇ¾î ÀÖ´Ù.

¿øÀÎÀº ¼³Á¤ÀÇ 38 ¶óÀκÎÅÍ 42 ¶óÀαîÁö Àִ MyBatis ÀÇ MapperScannerConfigurer °¡ ¾î¶²  SqlSessionFactory ¸¦ ÂüÁ¶ÇØ¾ß ÇÒÁö ¸ô¶ó¼­ ¹ß»ýÇÑ´Ù.

 

ÀÏ´Ü ³»°¡ ¾Æ´Â ÇØ°á ¹æ¹ýÀº µÎ °¡Áö°¡ ÀÖ´Ù.

  1. @Autowired ´ë½Å @Resource »ç¿ë
    @Resource(name="dsOneSqlSessionFactory")  Ã³·³ ¾î¶² sessionFactory ¸¦ ¾µÁö Java Config ¸¦ ½Ã¿ëÇÏ¿© Mapper ¼Ò½º¸¶´Ù ÁöÁ¤ÇØ ÁØ´Ù.  
  2. MyBatis ÀÇ sqlSessionFactoryBeanName À» »ç¿ëÇؼ­ ´ÙÀ½°ú °°ÀÌ ¸ÅÆÛ¿¡ ¾î¶² sqlSessionFactoryBeanName ¸¦ ¾µÁö¸¦ ¸í½ÃÀûÀ¸·Î ÁöÁ¤ÇÑ´Ù.
database-context.xml
1
2
3
4
5
6
7
8
<bean id="dsOneScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"
    p:basePackage="com.example.mapper-one"
    p:sqlSessionFactoryBeanName="dsOneSqlSessionFactory" />
 
<bean id="dsTwoScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"
    p:basePackage="com.example.mapper-two"
    p:sqlSessionFactoryBeanName="dsTwoSqlSessionFactory" />