MyBatis DB_VENDOR不起作用

时间:2017-02-15 14:23:00

标签: spring oracle postgresql mybatis spring-mybatis

你可以帮我弄清楚MyBatis中的DB_VENDOR有什么问题吗?我正在使用mybatis 3.4.2,mybatis-spring 1.3.1,spring 4.3.6.RELEASE。

一切,但依赖于供应商的查询,工作正常。示例查询不依赖于供应商,这仅用于此示例。 如果我从中删除了databaseId属性并删除了重复的属性,则此查询有效。

我需要支持Oracle和PostgreSQL,并且已经在我的测试范围内直接检查了元数据getDatabaseProductName(),得到了“Oracle”和“PostgreSQL”。因此,如果我的文档正确,它应该可以工作,不应该吗?

我得到的所有数据库都是:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): mappers.ConfigurationMapper.containsKey

测试的MyBatis-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>
    <databaseIdProvider type="DB_VENDOR">
        <property name="PostgreSQL" value="postgres"/>
        <property name="Oracle" value="oracle"/>
    </databaseIdProvider>

    <mappers>
        <mapper resource="mybatis/ConfigurationMapper.xml"/>
    </mappers>
</configuration>

test-context.xml(另一个使用PostgreSQL数据源)

    <mybatis:scan base-package="mappers" annotation="org.apache.ibatis.annotations.Mapper" factory-ref="sqlSessionFactory"/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
    <property name="username" value="test"/>
    <property name="password" value="test"/>
</bean>

<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:test-mybatis-config.xml"/>
</bean>

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

ConfigurationMapper.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="mappers.ConfigurationMapper">

    <select id="containsKey" resultType="string" parameterType="string" databaseId="oracle">
        SELECT '1' AS test FROM config_keys WHERE key = #{configKey}
    </select>

    <select id="containsKey" resultType="string" parameterType="string" databaseId="postgres">
        SELECT '1' AS test FROM config_keys WHERE key = #{configKey}
    </select>
</mapper>

mapper interface:

@Mapper
public interface ConfigurationMapper {

    String containsKey(@Param("configKey") String configKey);
}

1 个答案:

答案 0 :(得分:2)

找到它。答案在XMLConfigBuilder中:

    Environment environment = configuration.getEnvironment();
    if (environment != null && databaseIdProvider != null) {
      String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource());
      configuration.setDatabaseId(databaseId);
    }

必须配置环境才能使databaseId正常工作。

以下是如何在spring环境中使用multi-db支持:http://www.mybatis.org/spring/factorybean.html

<bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="SQL Server">sqlserver</prop>
            <prop key="DB2">db2</prop>
            <prop key="Oracle">oracle</prop>
            <prop key="MySQL">mysql</prop>
        </props>
    </property>
</bean>

<bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider">
    <property name="properties" ref="vendorProperties"/>
</bean>

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