我正在尝试使用 mongoDB 创建一个基础 spring 应用程序,但我不知道如何连接到数据库。我试过这样的事情:
应用程序属性:
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
(你应该 native 使用 MongoDB,而不是通过 JDBC 桥)。
关于java - 如何在 spring boot 中连接到 MongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474164/