当前位置:  开发笔记 > 编程语言 > 正文

如何在springboot中连接到MongoDB?

如何解决《如何在springboot中连接到MongoDB?》经验,为你挑选了1个好方法。

我正在尝试使用mongoDB创建一个基础Spring应用程序,但我不知道如何连接到数据库.我试过这样的事情:

application.properties:

spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.database=mongulet
spring.datasource.driver-class-name=mongodb.jdbc.MongoDriver
spring.data.mongodb.port=27017

主要用途:

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RestSuperAdvancedApplication implements CommandLineRunner{

    @Autowired
    private CustomerRepository repository;

    public static void main(String[] args) {
        SpringApplication.run(RestSuperAdvancedApplication.class, args);
    }

    @Override
    public void run(String... strings) throws Exception {
        repository.deleteAll();

        repository.save(new Customer("Crisan", "Raoul"));
        repository.save(new Customer("Smith", "Martha"));
        repository.save(new Customer("Erie", "Jayne"));
        repository.save(new Customer("Robinson", "Crusoe"));

        System.out.println("Customers found : ");

        repository.findAll().forEach(System.out::println);

        System.out.println();

        System.out.println("Customer found by first name: (Erie)");
        System.out.println("----------------");
        System.out.println(repository.findOneByFirstName("Erie"));


    }
}

客户类:

package com.example;

import javax.persistence.Id;

/**
 * Created by rcrisan on 7/19/2016.
 */
public class Customer {
    @Id
    private String id;
    private String firstName;
    private String lastName;

    public Customer() {
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id='" + id + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

库:

package com.example;

import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

/**
 * Created by rcrisan on 7/19/2016.
 */
public interface CustomerRepository extends MongoRepository {
    Customer findOneByFirstName(String fistName);
    List findByLastName(String lastName);
}

pom.xml中:



    4.0.0

    com.example
    restsuperadvanced
    0.0.1-SNAPSHOT
    jar

    restSuperAdvanced
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.6.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        


        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



运行程序后,我得到了这个异常:

Caused by: java.lang.IllegalStateException: Cannot load driver class: mongodb.jdbc.MongoDriver

有没有其他方法连接到mongoDB而不使用驱动程序类?



1> chrylis -on ..:

您似乎正在尝试将主要用于关系数据存储的JPA与MongoDB(一个"无关的"文档存储)混合使用.删除依赖spring-boot-starter-data-jpa(你根本不需要它)和spring.datasource.driver-class-name(你应该本地使用MongoDB,而不是通过JDBC桥).

推荐阅读
刘美娥94662
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有