Search code examples
mysqlmybatisspring-mybatis

How can i get the current schema name with Mybatis?


Basically i need to know if there is any way to get the current schema name using Mybatis. The DB engine I'm using is MySQL


Solution

  • The most easy way, for which you don't even need to do anything MyBatis-specific, would simply be a query:

    SELECT DATABASE();
    

    This should, according to the documentation, return the current database.

    Alternatively, you should be able to get the Configuration from your SqlSession via getConfiguration() and get it from there somewhere, perhaps from the environment which allows you access to the DataSource, but you will probably need some database-specific code there.