1. springboot项目使用MongoRepository后,启动一直报错
Field xxxRepository in com.xxx.impl.xxxImpl required a bean of type 'com.xxx.repository.xxxRepository' that could not be found.
解决方案
在启动类上加@EnableMongoRepositories注解,如果启动类不在根目录的话,要写好basePages="com.xxx.xxx"包名,要不然容易找不到
2. 查询报错
Mapped Statements collection does not contain value for com.xxx.repository.xxx.findByxxxIdAndReader
这个问题废了我很大劲,一开始总以为是自己的方法写错了,为此,我特地研究了springboot整合mongdb的文档Spring Data MongoDB - Reference Documentation
发现我的代码都符合要求的
然后开始看这个报错信息,里面总有sqlUtil和LogAspect的代码报错,打断点看,发现在sqlSession中查找xxxRepository.findxxx的namespace本来就没有,然后就把这段代码给注释掉,发现一切正常
原因:我们代码中为了记日志,加了打印sql的日志,就是上面的SqlUtil中,把我的mongo也记进去了,因为是aop实现的,然后又找不到,所以报错的
3. Query failed with error code 2 and error message ‘Field ‘locale’ is invalid in: { locale: “xxx” }’ on server
把@Document(collation="xxx")改成@Document(collection="xxx")
这个问题遇到了两遍了,这个两个单词确实容易让人弄错