2

I am using Spring and Mybatis in my project . Project can run in any platform like SQL Server Oracle etc.

I am facing 1 Problem i want to access variable value From properties file,application Context file to Mybatis Mapper file.

For.eg : ApplicationContext.xml - Spring file
config.properties file

in above file want to decalare variable lets say
pName = XYZ

i want to access this pName in Mybatis Mapper XML file.

<select id="getValue" parameterType="java.lang.String" >
${pName}
</select>

How is it possible ?if have any other solution most welcome.

2 Answers 2

3

To access spring way : Use

<util:properties id="myPropertyConfigurer" location="classpath:yourpropertiesfile.properties"/>

<context:property-placeholder  properties-ref="myPropertyConfigurer" order="1" ignore-unresolvable="true" />

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="applicationDataSource" />
    <property name="configLocation" value="classpath:sqlMapConfig.xml" />
    <property name="configurationProperties" ref="myPropertyConfigurer"></property>
</bean>

In your mapper xml file :

<select id="searchSomeOne" parameterType="map" .....>
        SELECT
        ${pName} AS module
        FROM MY_TABLE
        WHERE
        COL_ONE = #{moduleName} and
        COL_TWO like #{username}
</select>

and define pName=MODULE in the yourpropertiesfile.properties

Sign up to request clarification or add additional context in comments.

Comments

0

You can do in this way - 1. Add the below line of code in your mybatis-config.xml file

<properties resource="path/to/config.properties" />

Then you should be able access all the keys as ${keyName} in the mapper file .

Reference from the mybatis docs - http://mybatis.org/mybatis-3/configuration.html#properties

1 Comment

I am not using mybatis-config.xml file ,i am using Spring [applicationcontext.xml file].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.