Search code examples
springibatismybatis

Get schema name from a properties file


I'm having an issue at the moment that I've been doing alot of searching on but still havent found an answer.

Backgorund of problem: We have multiple DB schemas at the one URL including test copies of schemas (e.g schema1, schema2, schema1_test, schema2_test are all at the same url). I'm trying to make which version of each schema is used configurable via a properties file.

We are using Spring and mybatis, and unfortunately i'm new to both (so please excuse my ignorance or any mistakes i make describing the issue!)


So in my spring config file, which is stored under /src/main/resources, I have the following snippets: (I only added "configLocation" property and later added "sqlSessionFactoryBeanName" property)

<!-- 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>


My mybatisConfig.xml (which is stored under /src/main/resources, which should be on the class path)

<?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>


sqlmapconfig.properties (in same folder)

schema1=schema1_test


And I try reference the property in one of the mapper files in com.example.something.persistence:

 <select id="test" resultType="result" parameterType="long">
    select ${schema1}.table.col  
    from ${schema1}.table 
 </select>

When I try build with maven it fails a test:

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'

Any suggestions would be greatly appreciated!


Solution

  • I gave up on trying to read the properties directly and went down the passing it in from Java route

    So I had to change the "parameterType" to be a map in the mapper file

    <select id="test" resultType="result" parameterType="map">
        select ${schema1}.table.col  
        from ${schema1}.table where number=#{number}
    </select>
    

    And edited the java mapper code as follows

    import org.apache.ibatis.annotations.Param;
    
    ...
    
    public List<result> test(@Param("number") long number, @Param("schema1") String schema1);
    

    Hope this helps someone.

    Notes & References:

    Careful on the use of ${} vs #{}, differences explained here

    How to use multiple params wwas taking from here