Search code examples
javaspringmybatisspring-mybatis

MyBatis Spring DYNAMIC number of multiple databases Java configuration


This is fairly straight forward with a simple Spring DAO approach. However, using MyBatis, is there a way to setup multiple potential datasources?

The best approach I can think of is to use an ArraList of a Bean each containing datasource.driverclass,datasource.url, datasource.username, datasource.password etc.

The values for the datasources are stored in individual properties files. There could be 1 or 10 of these property files (or more).

So for example, one application startup all the property files would be loaded one at a time into an ArrayList. Then, based on the NAME=value line from the property file, we would know which datasource to hit.

So http:localhost:8080/name=db1

... would access all the data from the datasource configured with the name "09". Each property file would contain:

name=db1
datasource.driverclass=jdbc:sqlserver
datasource.url=jdbc:sqlserver://localhost:1433;databaseName=someDBname
datasource.username=user1
datasource.password=pass1

So the identifier here is "name=db1".

Would the best approach from a MyBatis implementation utilise an ArrayList of Beans?


Solution

  • I ended up using a hierarchical application.yml file detailing the multitenant connection values, based on a selected tenant code.