[英]How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application?
The MyBatis Spring Boot Starter documentation lists properties that can be set in the application.properties file, and that list includes: MyBatis Spring Boot Starter 文档列出了可以在 application.properties 文件中设置的属性,该列表包括:
configuration: A MyBatis Configuration bean.
配置:一个 MyBatis 配置 bean。 About available properties see the MyBatis reference page
关于可用属性见 MyBatis 参考页面
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.当然,如果我将
VARCHAR
更改为不是枚举 JdbcType 的有效值,我会收到错误消息。 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.当我使用调试器时,我可以看到
MybatisProperties.setConfiguration()
在Configuration.setJdbcTypeForNull()
之前被调用,这意味着使用默认值( JdbcType.OTHER
)而不是我试图设置的值。
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.
在其他地方,我只能看到使用 XML 文件的示例,如果可能的话,我宁愿避免这种情况。
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.我放弃了在
application.properties
使用configuration
属性的尝试,并求助于我试图避免的 XML 配置,但刚才,在这里看到了一些有用的建议,我回到了我的代码,从XML 配置返回mybatis.configuration.jdbcTypeForNull=NULL
并重新运行我的测试,这在之前的相同条件下肯定会失败,但现在它通过了。
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.我不知道与以前有什么不同 - 调试时我看到与上面提到的相同的行为 - 配置 bean似乎在启动期间将 jdbcTypeForNull 参数设置回 OTHER,但是当它在查询期间被访问时它再次为 NULL。 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. 如果您要使用
mybatis.configuration.jdbcTypeForNull
,请记住不要同时指定configLocation
属性。 Otherwise your setting will be erase to default. 否则,您的设置将被删除为默认值。
As what spring boot do, MybatisProperties.setConfiguration()
is called before Configuration.setJdbcTypeForNull()
is really right. 正如Spring引导所做的那样,在
Configuration.setJdbcTypeForNull()
非常正确之前调用MybatisProperties.setConfiguration()
。 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. 因为spring boot尝试将
configuration
设置为MybatisProperties
但发现其成员configuration
为null,因此spring boot将实例化一个新Configuration
并立即将其分配给MybatisProperties
。 Afterwards, spring boot will try to assign all mybatis.configuration.*
to this member of MybatisProperties
. 随后,春天开机时会尝试分配所有
mybatis.configuration.*
以该成员MybatisProperties
。
If you are going to use XML to configure mybatis, you need specify <setting name="jdbcTypeForNull" value="VARCHAR"/>
under element <settings/>
as example . 如果要使用XML配置mybatis,则需要在元素
<settings/>
下指定<setting name="jdbcTypeForNull" value="VARCHAR"/>
<settings/>
作为示例 。
This would definitely work. 这肯定会奏效。
Add this <setting name="jdbcTypeForNull" value="OTHER"/>
to your mybatis-config.xml
it should work fine. 将此
<setting name="jdbcTypeForNull" value="OTHER"/>
到mybatis-config.xml
它应该可以正常工作。 Else try this to your query which works perfectly fine for me with oracle/mysql as db 另外尝试使用oracle / mysql作为db的查询,这对我来说非常好
insert into students
(id,name,class,date_of_joining)
values
(#{id,jdbcType=INTEGER},#{class,jdbcType=TIMESTAMP},
#{class,jdbcType=NVARCHAR},#{date_of_joining,jdbcType=TIMESTAMP})
Here is list of jdbcTypes provided by mybatis. 以下是mybatis提供的jdbcTypes列表 。 You need to add it to each constraint if you feel the constraint can be empty/null at times.
如果您觉得约束有时可以为空/ null,则需要将其添加到每个约束。
If you are using application.yml try如果你使用 application.yml 试试
mybatis:
configuration:
jdbc-type-for-null: 'NULL'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.