Saturday, February 27, 2016

Set global properties for your MyBatis mappers using Spring-MyBatis

If you need to define some global properties in MyBatis, but using spring-mybatis

In your spring config file:



In application.properties:

fooTableName=FOO_T

In your spring config file: (note sorry about the odd xml declaration, this blogger pre xml doesn't let me define the xml in one line using "/"?)


 
  
 
  
   ${fooTableName}
  
 



Now in your mapper:




Thanks to post at http://stackoverflow.com/questions/8392741/mybatis-defining-a-global-parameter/8422082#8422082

Thursday, February 18, 2016

Spring look up property dynamically

You need to look up a property from your properties file dynamically in a Spring class. If you google for this you'll find different approaches. Here is what I'm doing, which seems to work nicely.

My Spring class implements EmbeddedValueResolverAware

You then need to implement the setEmbeddedValueResolver method, so you create a field in your class:

private StringValueResolver stringValueResolver;

public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
    this.stringValueResolver = stringValueResolver;
}

Now let's say have a props file:

something.1.foo=something2
something.2.foo=something1

In my Spring class above I can look it up dynamically...

int dynamicVal = getSomeDyanmicVal();
String foo = stringValueResolver.resolveStringValue("${something."+dynamicVal+".foo}");

Blogger Syntax Highliter