使用MongoRepository对数据进行增删改查,出现java.lang.IllegalArgumentException: MongoOperations 必须不能为null

使用MongoRepository对数据进行增删改查,出现java.lang.IllegalArgumentException: MongoOperations must not be null!异常,MongoOperations 不能为空。  

原因:百度MongoOperations 类是做什么的,发现MongoTemplate类实现MongoOperations类

image-20211103155627881

在使用MongoRepository时,程序也是借助MongoOperations 完成对应的操作,可以MongoRepository工厂构造会传入MongoOperations 类,获取它的上下文context。

image-20211103160033404

看出报错MongoOperations must not be null!,说明MongoOperations 是空的,想一下程序也没有过多的配置,无非xml中的配置,所以我们的目光转向配置文件,与MongoRepository相关的就这一行配置,标识我的repositories位置在哪里。

<mongo:repositories base-package="com.digiwin.app.ttt.service.mongo.repository" repository-impl-postfix="repository" />

然后MongoOperations 为空,那说明MongoTemplate也为空,而MongoTemplate模板的如下,也没有太大的问题

 <!-- mongodb的模板 -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    </bean>

所以repositories中需要把MongoTemplate模板引入进去,那肯定是少两者关联(mongo-template-ref="mongoTemplate")的配置了,最终的代码为

<!--    mongo-template-ref="mongoTemplate"  要加,不然MongoRepository用不了,血泪史-->
<mongo:repositories base-package="com.digiwin.app.ttt.service.mongo.repository"
                    mongo-template-ref="mongoTemplate" repository-impl-postfix="repository"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值