我正在尝试使用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?
答案 0 :(得分:6)
您似乎正在尝试将主要用于关系数据存储的JPA与MongoDB混合使用,而MongoDB是一个&#34;无关的&#34;文件商店。放弃对spring-boot-starter-data-jpa
(您根本不需要它)和spring.datasource.driver-class-name
的依赖关系(您应该本地使用MongoDB,而不是通过JDBC桥接器。)