从属性文件中获取架构名称

wit*_*now 0 spring ibatis mybatis

目前我遇到了一个问题,我一直在进行大量搜索,但仍未找到答案。

问题的背景:我们在一个 URL 上有多个 DB 模式,包括模式的测试副本(例如,schema1、schema2、schema1_test、schema2_test 都在同一个 URL)。我正在尝试通过属性文件使每个架构的哪个版本使用可配置。

我们正在使用 Spring 和 mybatis,不幸的是我对这两个都是新手(所以请原谅我的无知或我在描述问题时犯的任何错误!)


所以在我的 spring 配置文件中,它存储在 /src/main/resources 下,我有以下片段:(我只添加了“configLocation”属性,后来添加了“sqlSessionFactoryBeanName”属性)

<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="com.example.domain" />
    <property name="configLocation" value="classpath:mybatisConfig.xml" />
</bean>

<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.example.something.persistence" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
Run Code Online (Sandbox Code Playgroud)


我的mybatisConfig.xml(存放在/src/main/resources下,应该在类路径下)

<?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>
<properties resource="sqlmapconfig.properties" />
</configuration>
Run Code Online (Sandbox Code Playgroud)


sqlmapconfig.properties(在同一文件夹中)

schema1=schema1_test
Run Code Online (Sandbox Code Playgroud)


我尝试在 com.example.something.persistence 中的映射器文件之一中引用该属性:

 <select id="test" resultType="result" parameterType="long">
    select ${schema1}.table.col  
    from ${schema1}.table 
 </select>
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 Maven 构建时,它无法通过测试:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'schema1' in 'class java.lang.Long'
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激!

wit*_*now 5

我放弃了直接读取属性的尝试,而是从 Java 路由中传递过来

所以我不得不将“parameterType”更改为映射器文件中的映射

<select id="test" resultType="result" parameterType="map">
    select ${schema1}.table.col  
    from ${schema1}.table where number=#{number}
</select>
Run Code Online (Sandbox Code Playgroud)

并编辑了java映射器代码如下

import org.apache.ibatis.annotations.Param;

...

public List<result> test(@Param("number") long number, @Param("schema1") String schema1);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助某人。

注释和参考:

小心使用 ${} 与 #{},这里解释差异

如何使用从这里获取的多个参数