gpt4 book ai didi

spring - MyBatis DB_VENDOR 不起作用

转载 作者:行者123 更新时间:2023-11-29 12:09:48 24 4
gpt4 key购买 nike

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

除依赖于供应商的查询外,一切正常。示例查询不依赖于供应商,这只是为了这个示例。 如果我从中删除 databaseId 属性并删除重复的属性,则此查询有效。

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

我得到的所有 databaseId 是:

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>

映射器接口(interface):

@Mapper
public interface ConfigurationMapper {

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

最佳答案

找到了。答案在 XMLConfigBuilder 中:

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

必须配置环境才能使databaseId起作用。

这里是如何在spring环境中使用多数据库支持: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>

关于spring - MyBatis DB_VENDOR 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42251978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com