MongoCollection.insertOne找不到方法错误修复

在我学习spring-data-mongodb过程中,使用了spring-data-mongodb最新版本3.2.1和mongo-java-driver最新版本3.12.8时出现

com.mongodb.client.MongoCollection.insertOne(Ljava/lang/Object;)Lcom/mongodb/client/result/InsertOneResult;

错误,原因出在spring-data-mongod依赖mongodb-driver:4.2.3,与我们导入的mongo-java-driver不匹配。

解决方法:
将spring-data-mongodb降版本到2.2.13以下,因为2.2.13依赖mongo-java-driver:3.12.8,所以我们另外导入mongodb-driver:3.12.8,正常配置;

package com.hyh.spring.ssm2.review.config;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration
@ComponentScan({"com.hyh.spring.ssm2.review.mongo.dao"})
@EnableMongoRepositories(basePackages = "com.hyh.spring.ssm2.review.mongo.dao")
public class MongoConfig extends AbstractMongoClientConfiguration {
    @Override
    protected String getDatabaseName() {
        return "ssm_review";
    }

    @Override
    public MongoClient mongoClient() {
        return MongoClients.create();
    }
}

测试插入数据

@Test
    public void testSave(){

        LinkedHashSet<Item> items = new LinkedHashSet<>();
        items.add(new Item(1L,"1","test",25.0,100));
        items.add(new Item(2L,"1","test",25.0,100));
        items.add(new Item(3L,"1","test",25.0,100));
        Order order = new Order("1", "hyh", "test",items);

        orderDao.save(order);
    }
E:\Java\jdk1.8.0_202\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:I:\ideaIU-2020.3.3.win\lib\idea_rt.jar=5980:I:\ideaIU-2020.3.3.win\bin -Dfile.encoding=UTF-8 -classpath I:\ideaIU-2020.3.3.win\lib\idea_rt.jar;I:\ideaIU-2020.3.3.win\plugins\junit\lib\junit5-rt.jar;I:\ideaIU-2020.3.3.win\plugins\junit\lib\junit-rt.jar;E:\java\jdk1.8.0_202\jre\lib\charsets.jar;E:\java\jdk1.8.0_202\jre\lib\deploy.jar;E:\java\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;E:\java\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;E:\java\jdk1.8.0_202\jre\lib\ext\dnsns.jar;E:\java\jdk1.8.0_202\jre\lib\ext\jaccess.jar;E:\java\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;E:\java\jdk1.8.0_202\jre\lib\ext\localedata.jar;E:\java\jdk1.8.0_202\jre\lib\ext\nashorn.jar;E:\java\jdk1.8.0_202\jre\lib\ext\sunec.jar;E:\java\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;E:\java\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;E:\java\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;E:\java\jdk1.8.0_202\jre\lib\ext\zipfs.jar;E:\java\jdk1.8.0_202\jre\lib\javaws.jar;E:\java\jdk1.8.0_202\jre\lib\jce.jar;E:\java\jdk1.8.0_202\jre\lib\jfr.jar;E:\java\jdk1.8.0_202\jre\lib\jfxswt.jar;E:\java\jdk1.8.0_202\jre\lib\jsse.jar;E:\java\jdk1.8.0_202\jre\lib\management-agent.jar;E:\java\jdk1.8.0_202\jre\lib\plugin.jar;E:\java\jdk1.8.0_202\jre\lib\resources.jar;E:\java\jdk1.8.0_202\jre\lib\rt.jar;H:\java_review\java_web_review\spring_ssm_review2\target\test-classes;H:\java_review\java_web_review\spring_ssm_review2\target\classes;H:\maven_repository\junit\junit\4.13.2\junit-4.13.2.jar;H:\maven_repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;H:\maven_repository\org\springframework\spring-webmvc\5.3.7\spring-webmvc-5.3.7.jar;H:\maven_repository\org\springframework\spring-aop\5.3.7\spring-aop-5.3.7.jar;H:\maven_repository\org\springframework\spring-beans\5.3.7\spring-beans-5.3.7.jar;H:\maven_repository\org\springframework\spring-core\5.3.7\spring-core-5.3.7.jar;H:\maven_repository\org\springframework\spring-jcl\5.3.7\spring-jcl-5.3.7.jar;H:\maven_repository\org\springframework\spring-expression\5.3.7\spring-expression-5.3.7.jar;H:\maven_repository\org\springframework\spring-web\5.3.7\spring-web-5.3.7.jar;H:\maven_repository\org\springframework\spring-context\5.3.7\spring-context-5.3.7.jar;H:\maven_repository\org\springframework\spring-aspects\5.3.7\spring-aspects-5.3.7.jar;H:\maven_repository\org\aspectj\aspectjweaver\1.9.6\aspectjweaver-1.9.6.jar;H:\maven_repository\org\springframework\spring-test\5.3.7\spring-test-5.3.7.jar;H:\maven_repository\org\mockito\mockito-core\3.11.1\mockito-core-3.11.1.jar;H:\maven_repository\net\bytebuddy\byte-buddy\1.11.2\byte-buddy-1.11.2.jar;H:\maven_repository\net\bytebuddy\byte-buddy-agent\1.11.2\byte-buddy-agent-1.11.2.jar;H:\maven_repository\org\objenesis\objenesis\3.2\objenesis-3.2.jar;H:\maven_repository\javax\servlet\javax.servlet-api\4.0.1\javax.servlet-api-4.0.1.jar;H:\maven_repository\javax\servlet\jstl\1.2\jstl-1.2.jar;H:\maven_repository\org\hibernate\validator\hibernate-validator\6.0.22.Final\hibernate-validator-6.0.22.Final.jar;H:\maven_repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;H:\maven_repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;H:\maven_repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;H:\maven_repository\com\fasterxml\jackson\core\jackson-databind\2.12.3\jackson-databind-2.12.3.jar;H:\maven_repository\com\fasterxml\jackson\core\jackson-annotations\2.12.3\jackson-annotations-2.12.3.jar;H:\maven_repository\com\fasterxml\jackson\core\jackson-core\2.12.3\jackson-core-2.12.3.jar;H:\maven_repository\org\thymeleaf\thymeleaf-spring5\3.0.12.RELEASE\thymeleaf-spring5-3.0.12.RELEASE.jar;H:\maven_repository\org\thymeleaf\thymeleaf\3.0.12.RELEASE\thymeleaf-3.0.12.RELEASE.jar;H:\maven_repository\org\attoparser\attoparser\2.0.5.RELEASE\attoparser-2.0.5.RELEASE.jar;H:\maven_repository\org\unbescape\unbescape\1.1.6.RELEASE\unbescape-1.1.6.RELEASE.jar;H:\maven_repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;H:\maven_repository\org\springframework\security\spring-security-core\5.5.0\spring-security-core-5.5.0.jar;H:\maven_repository\org\springframework\security\spring-security-crypto\5.5.0\spring-security-crypto-5.5.0.jar;H:\maven_repository\org\springframework\security\spring-security-config\5.5.0\spring-security-config-5.5.0.jar;H:\maven_repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.5.0\kotlin-stdlib-jdk8-1.5.0.jar;H:\maven_repository\org\jetbrains\kotlin\kotlin-stdlib\1.5.0\kotlin-stdlib-1.5.0.jar;H:\maven_repository\org\jetbrains\annotations\13.0\annotations-13.0.jar;H:\maven_repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.5.0\kotlin-stdlib-common-1.5.0.jar;H:\maven_repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.5.0\kotlin-stdlib-jdk7-1.5.0.jar;H:\maven_repository\org\springframework\security\spring-security-web\5.5.0\spring-security-web-5.5.0.jar;H:\maven_repository\org\apache\commons\commons-dbcp2\2.8.0\commons-dbcp2-2.8.0.jar;H:\maven_repository\org\apache\commons\commons-pool2\2.8.1\commons-pool2-2.8.1.jar;H:\maven_repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;H:\maven_repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar;H:\maven_repository\org\springframework\spring-jdbc\5.3.7\spring-jdbc-5.3.7.jar;H:\maven_repository\org\springframework\spring-tx\5.3.7\spring-tx-5.3.7.jar;H:\maven_repository\org\projectlombok\lombok\1.18.20\lombok-1.18.20.jar;H:\maven_repository\org\springframework\data\spring-data-jpa\2.5.1\spring-data-jpa-2.5.1.jar;H:\maven_repository\org\springframework\data\spring-data-commons\2.5.1\spring-data-commons-2.5.1.jar;H:\maven_repository\org\springframework\spring-orm\5.3.7\spring-orm-5.3.7.jar;H:\maven_repository\org\aspectj\aspectjrt\1.9.6\aspectjrt-1.9.6.jar;H:\maven_repository\org\hibernate\hibernate-core\5.5.2.Final\hibernate-core-5.5.2.Final.jar;H:\maven_repository\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;H:\maven_repository\org\javassist\javassist\3.27.0-GA\javassist-3.27.0-GA.jar;H:\maven_repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;H:\maven_repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.1.1.Final\jboss-transaction-api_1.2_spec-1.1.1.Final.jar;H:\maven_repository\org\jboss\jandex\2.2.3.Final\jandex-2.2.3.Final.jar;H:\maven_repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;H:\maven_repository\org\hibernate\common\hibernate-commons-annotations\5.1.2.Final\hibernate-commons-annotations-5.1.2.Final.jar;H:\maven_repository\javax\xml\bind\jaxb-api\2.3.1\jaxb-api-2.3.1.jar;H:\maven_repository\org\glassfish\jaxb\jaxb-runtime\2.3.1\jaxb-runtime-2.3.1.jar;H:\maven_repository\org\glassfish\jaxb\txw2\2.3.1\txw2-2.3.1.jar;H:\maven_repository\com\sun\istack\istack-commons-runtime\3.0.7\istack-commons-runtime-3.0.7.jar;H:\maven_repository\org\jvnet\staxex\stax-ex\1.8\stax-ex-1.8.jar;H:\maven_repository\com\sun\xml\fastinfoset\FastInfoset\1.2.15\FastInfoset-1.2.15.jar;H:\maven_repository\org\springframework\data\spring-data-mongodb\2.2.13.RELEASE\spring-data-mongodb-2.2.13.RELEASE.jar;H:\maven_repository\org\mongodb\mongo-java-driver\3.12.8\mongo-java-driver-3.12.8.jar;H:\maven_repository\org\mongodb\mongodb-driver\3.12.8\mongodb-driver-3.12.8.jar;H:\maven_repository\org\mongodb\bson\3.12.8\bson-3.12.8.jar;H:\maven_repository\org\mongodb\mongodb-driver-core\3.12.8\mongodb-driver-core-3.12.8.jar com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.hyh.spring.ssm2.review.test.OrderDaoTest,testInsert
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
六月 17, 2021 8:24:28 下午 org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [name: default]
六月 17, 2021 8:24:28 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 5.5.2.Final
六月 17, 2021 8:24:29 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
六月 17, 2021 8:24:30 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
六月 17, 2021 8:24:31 下午 org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 6.0.22.Final
六月 17, 2021 8:24:32 下午 org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]

Process finished with exit code 0

插入成功

60cae2c8a95e0000f7007d02	1	hyh	test		
60cb3e2136a6a15d9026b9b3	2	hyh	test	(Array) 3 Elements	com.hyh.spring.ssm2.review.mongo.pojo.Order
60cb3e709f1cc97a10df7444	1	hyh	test	(Array) 3 Elements	com.hyh.spring.ssm2.review.mongo.pojo.Order
60cb3f01f278096bcdcb1c02	2	hyh	test	(Array) 3 Elements	com.hyh.spring.ssm2.review.mongo.pojo.Order
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值