SpringBoot的版本较高,不支持直接注入MongoTemplate
需要加入此配置
import com.mongodb.MongoClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.core.MongoTemplate; /** * @Author: cupid * Created by Cupid520 on 2019/1/16. * @Description: */ @Configuration public class MongoConfig { @Bean public MongoClient mongoClient() { return new MongoClient("127.0.0.1", 27017); } @Bean public MongoTemplate mongoTemplate() { return new MongoTemplate(mongoClient(), "testdb"); }