springspring-bootkotlinmybatisspring-mybatis

Spring & MyBatis - how to get MyBatis databseId in a @Bean annotated function


I am trying to create a Spring bean based on a MyBatis databaseId like so:

@Bean
fun getSomeBean(@Autowired databaseId: String): SomeBean {
  if(databaseId.equals("mysql")){
    return SomeMySqlBean()
  } else {
    return SomeNonMysqlBean()
  }

}

How do I get the databaseId to be injected with MyBatis?


Solution

  • I had a look at some of the API documentation and came up with this - works nicely and now I can construct a couple of the vendor-specific objects I need:

    @Bean
    fun databaseId(sqlSessionFactory: SqlSessionFactory): String {
        return sqlSessionFactory.configuration.databaseId
    }