[英]How to connect to MongoDB in springboot?
我正在嘗試使用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, String> {
Customer findOneByFirstName(String fistName);
List<Customer> findByLastName(String lastName);
}
pom.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>restsuperadvanced</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>restSuperAdvanced</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
運行程序后,我得到了這個異常:
Caused by: java.lang.IllegalStateException: Cannot load driver class: mongodb.jdbc.MongoDriver
有沒有其他方法連接到mongoDB而不使用驅動程序類?
您似乎正在嘗試將主要用於關系數據存儲的JPA與MongoDB(一種“無關的”文檔存儲)混合使用。 刪除對spring-boot-starter-data-jpa
(你根本不需要它)和spring.datasource.driver-class-name
的spring.datasource.driver-class-name
(你應該本地使用MongoDB,而不是通過JDBC橋)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.