2

i need to connect to multiple databases configured in my config.xml file

config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

    <configuration>

     <environments default="development">
        <environment id="development">
          <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.ibm.as400.access.AS400JDBCDriver"/>
                <property name="url" value="url"/>
                <property name="username" value="usernmae"/>
                <property name="password" value="password"/>
            </dataSource>
       </environment>
          <environment id="test">
          <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.ibm.as400.access.AS400JDBCDriver"/>
                <property name="url" value="url"/>
                <property name="username" value="usernmae"/>
                <property name="password" value="password"/>
            </dataSource>
       </environment>


     </environments> 

</configuration>

i am using mapper interface and mapper.xml for queries

Mapper Interface

 @Mapper
    public interface OrderMapper {
     List<Order> selectAll();
     }

Mapper.xml

<mapper namespace="com.mapper.OrderMapper" >

 <resultMap id="result" type="com.mapper.Order">
    <result property="orderId"  jdbcType="NUMERIC" column="ORD_ORDER_ID"/>  
 </resultMap> 

  <select id="selectAll" resultMap="result">
    SELECT * FROM table
  </select>
</mapper>

Application.properties

mybatis.config-location = classpath:./config.xml
mybatis.mapper-locations=./mapper/*.xml

i am able to work for single database by using spring datasource in properties file..nut i am unable to make it work through the config file..what am i doing wrong ?

Mohit Anand
  • 41
  • 1
  • 5

1 Answers1

0

Spring does not know about datasources configured in mybatis. Spring needs to have access to the datasource to be able to create connections. In spring-boot in simple case this is done by DataSourceTransactionManager which uses a datasource that is configured in spring context.

If you want to change that that would require you to implement your own PlatformTransactionManager similar to DataSourceTransactionManager but that uses datasource(s) created and managed by mybatis. This probably can be done by accessing mybatis Configuration object that is created by MybatisAutoConfiguration if you use spring boot (or maybe you would even need to check how datasources are created and injected into spring context). This is rather complicated and you would go against the way it should be (namely to define datasources in spring context).