为什么错误的 @MapperScan 能够正常运行

huangapple go评论91阅读模式
英文:

Why wrong @MapperScan can run ok

问题

我创建了一个简单的Spring Boot项目,使用了MyBatis Plus,在使用@MapperScan("xxx.xxx.xxx.Mapper")的情况下,我可以成功运行它。然而,我的Mapper文件位于文件夹xxx/xxx/xxx/mapper中,所以这有些奇怪。使用@MapperScan("xxx.xxx.xxx.Mapper"),我可以正确运行项目,但是将其打包为jar文件后,运行jar文件会出错,提示找不到mapper bean的bean工厂。在进行修正后,使用@MapperScan("xxx.xxx.xxx.mapper"),并打包项目,错误消失了。
我不知道为什么会发生这种情况。

英文:

I create a simple spring boot project, with mybatis plus, and I can run it well with @MapperScan("xxx.xxx.xxx.Mapper"). However, my mapper files are in folder xxx/xxx/xxx/mapper, so it is weird. With @MapperScan("xxx.xxx.xxx.Mapper"), I can run project right, but after package it as jar, run jar file will get error that bean factory can't find mapper bean. After correct it, @MapperScan("xxx.xxx.xxx.mapper"), and package the project, the error disappears.
I have no idea why this happens.

答案1

得分: 1

Java区分大小写,包括包名。您正在Windows上运行,通常情况下NTFS文件系统是不区分大小写的。很有可能,在Boot执行其扫描时,不正确的Mapper恰好与NTFS中的实际目录名mapper匹配,它会找到您的bean,但在打包jar时,它会在内部使用真实的包名,而这个包名是不匹配的。

英文:

Java is case-sensitive, including package names. You are running on Windows, and in general the NTFS filesystem is not case-sensitive. Most likely, when Boot is performing its scan, the incorrect Mapper happens to match the actual directory name mapper in NTFS, and it finds your bean, but when it packages up the jar, it uses the real package name inside and doesn't match.

huangapple
  • 本文由 发表于 2020年10月12日 09:25:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64310550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定