tags:

views:

71

answers:

1

I'm developing a Spring MVC application using ibatis for the database interaction layer. I'd like to be able to set the name of the database via a properties file in my classpath (or via the spring xml configuration) so I can change the database on the fly for a certain application and can be changed by setting the parameter and redeploying the application.

What I'm looking for is being able to set the database name on an existing database. Lets say I have a system called DB1 with databases: user, user_qa, and user_dev. I'd like to be able to parameterize the SQLs so that instead of doing:

SELECT * FROM user.Logins

I'd do

SELECT * FROM $database.user$.Logins

So I can change the database.user property and redeploy the application instead of rewriting a ton of SQL statements each time I changed the name of the databases.

A: 

Pretty simple to do in Spring. Say you have a basic conf file [database.properties]:

jdbc.username = user
jdbc.host = mydbserver.com

Then in your Spring config xml file :

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:database.properties</value>
</property>
</bean>

Now where you want those variables substitute the name in the config file:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="host"><value>${jdbc.host}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
</bean>
Gandalf