Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyBatis - defining a global parameter

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?

like image 251
Bostone Avatar asked Dec 05 '11 22:12

Bostone


People also ask

What is parameter type in MyBatis?

parameterType. The fully qualified class name or alias for the parameter that will be passed into this statement. This attribute is optional because MyBatis can calculate the TypeHandler to use out of the actual parameter passed to the statement. Default is unset .

What is the difference between Ibatis and MyBatis?

It is the same thing, a persistence framework! But until June 2010, iBatis was under Apache license and since then, the framework founders decided to move it to Google Code and they renamed it to MyBatis. The framework is still the same though, it just has a different name now.

Can I pass a list as a parameter to a MyBatis Mapper?

NOTE You can pass any Iterable object (for example List, Set, etc.), as well as any Map or Array object to foreach as collection parameter.

What is Resultmap in MyBatis?

resultMaps. It is the most important and powerful elements in MyBatis. The results of SQL SELECT statements are mapped to Java objects (beans/POJO). Once the result map is defined, we can refer these from several SELECT statements.


3 Answers

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.

like image 158
rimsky Avatar answered Sep 22 '22 20:09

rimsky


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}
like image 45
Andy Avatar answered Sep 21 '22 20:09

Andy


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>
like image 32
Raff Avatar answered Sep 21 '22 20:09

Raff