10

First the problem: I'm using XML-defined queries and the SQL contains database name as part of a table name. For example: SELECT * from mydb.bar. Unfortunately, databases are created/named all over the place and mudb part is really dynamic and can change at any moment. So I wanted to replace it with a property so it would look like SELECT * FROM ${dbname}.bar and then I defined the following section in mybatis-config.xml:

<properties>
    <property name="dbname" value="mydb"/>
</properties>

But when I run the query ${dbname} evaluates to null. Same happens if I define this property in the properties file. I would hate to pass this as part of the each call parameters since this is truly a global property. Can this be done? And if yes - how?

3 Answers 3

8

Yes, you can! This is kind of a weird undocumented feature maybe. When building your Configuration object, do something like this. (org.apache.ibatis.session.Configuration)

configuration.getVariables().put("global_param", "123");

Then in your XML map, you can reference.

    select * from ${global_param}
7
  • 1
    Thanks!. Unfortunately I'm using Spring to abstract MyBatis and all my configurations are defined in applicationContext.xml. I wonder though if these variables are exposed and can be set through XML configuration in applicationContext.xml, going to look into this
    – Bostone
    Dec 7, 2011 at 21:16
  • It looks like it should work. I looked at source code for SqlSessionFactoryBean, it appears to set the variables to the be properties. I haven't work with the spring integration, but I'd try debugging the buildSqlSessionFactory method in SqlSessionFactoryBean to make see where it is loading the properties.
    – Andy
    Dec 7, 2011 at 21:55
  • The properties passed to 'SqlSessionFactoryBean.setConfigurationProperties()' get set as variables in the MyBatis Configuration. You should be able to create a Properties in Spring xml and use it when configuring the factory bean.
    – AngerClown
    Dec 21, 2011 at 2:47
  • Does know anyone if there are any way for adding variables per environment and use it in the mappers?
    – vzamanillo
    May 18, 2015 at 16:34
  • 1
    @egemen yes can use in interceptor. This can work with spring maybtis also
    – vsingh
    Dec 3, 2020 at 20:35
5

I had the same issue using Spring+MyBatis, and solved it by setting 'configurationProperties' using my Spring XML definition for sqlSessionFactory. My example below shows how to set a custom global property named 'encryptionKey', with a value which you can either hard-code in the XML file, or load from an external file using the context:property-placeholder tag (as below).

<context:property-placeholder location="/WEB-INF/spring/config-datasource.properties" />

<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="typeAliasesPackage" value="com.example.model" />
    <beans:property name="configurationProperties">
        <beans:props>
            <beans:prop key="encryptionKey">${jdbc.encryptionKey}</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>
2

I was using an XML configuration but not Spring and set a property inside the Configuration object but discovered that had to be done before the mapper files are loaded (see here). I abandoned the Configuration object approach and went with this approach, which worked for me:

  Reader reader = Resources.getResourceAsReader("..../mybatis-config.xml");
  Properties properties = new Properties();
  properties.setProperty("dbname", "mydb");
  SqlSessionFactory.factory = new SqlSessionFactoryBuilder().build(reader, "development", properties);

Then, as Andy Pryor posted, use select * from ${dbname} in the XML mapper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.