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:

  1. <context:property-placeholder location="classpath:application.properties">  
  2. </context:property-placeholder>  

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 "/"?)

  1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  2.  <property name="dataSource" ref="myDS"></property>  
  3.  <property name="typeAliasesPackage" value="com.foo.domain"></property>   
  4.  <property name="configurationProperties">  
  5.   <props>  
  6.    <prop key="fooTableName">${fooTableName}</prop>  
  7.   </props>  
  8.  </property>  
  9. </bean>  


Now in your mapper:

  1. <select id="fetchStuff" resultmap="myResultMap">  
  2.     SELECT stuff FROM ${fooTableName}   
  3. </select>  


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

No comments :

Post a Comment

Blogger Syntax Highliter