The MyBatis Spring Boot Starter documentation lists properties that can be set in the application.properties file, and that list includes:
configuration: A MyBatis Configuration bean. About available properties see the MyBatis reference page
It's not very obvious from there, but what I think it's suggesting is that you can add something like this:
mybatis.configuration.jdbcTypeForNull=VARCHAR
This seems to work; certainly, if I change VARCHAR
for something that is not a valid value for the enum JdbcType, I get an error. However, it also doesn't appear to be picking up my setting.
When I use a debugger, I can see that MybatisProperties.setConfiguration()
is called, before Configuration.setJdbcTypeForNull()
, which means that the default value (JdbcType.OTHER
) is used instead of the value I'm trying to set.
Am I misunderstanding how this functionality is supposed to be used, or is this a bug? Is anyone else setting config values like this? Elsewhere I can only see examples using XML files, which I'd rather avoid, if possible.
I gave up on trying to use the configuration
property in application.properties
, and resorted to the XML config that I was trying to avoid, but just now, having seen some useful suggestions here, I went back to my code, changed it from the XML config back to having mybatis.configuration.jdbcTypeForNull=NULL
and re-ran my test, which was definitely failing under the same conditions before, but now it passes.
I've no idea what's different from before - debugging I see the same behaviour as mentioned above - the Configuration bean seems to get the jdbcTypeForNull parameter set back to OTHER during startup, but then when it's accessed during a query it's NULL again. I'll leave this question here in case it's a race condition and it starts failing again later, but for now my problem seems to have mysteriously disappeared.
If you are going to use mybatis.configuration.jdbcTypeForNull
, please remember NOT to specify configLocation
property at same time. Otherwise your setting will be erase to default.
As what spring boot do, MybatisProperties.setConfiguration()
is called before Configuration.setJdbcTypeForNull()
is really right. Because spring boot try to set configuration
to MybatisProperties
but found its member configuration
is null, so spring boot will instantiate a new Configuration
and assign it to MybatisProperties
immediately. Afterwards, spring boot will try to assign all mybatis.configuration.*
to this member of MybatisProperties
.
If you are going to use XML to configure mybatis, you need specify <setting name="jdbcTypeForNull" value="VARCHAR"/>
under element <settings/>
as example.
This would definitely work.