我的弹簧启动项目有一个非常奇怪的问题: 我使用的是STS,它的版本是3.6.3.SR1。我也安装了mongodb,它的版本是2.6.7。 我想用STS连接到mongodb。所以,我从教程中尝试了这个项目: 档案 - >新的 - >导入Spring入门内容。然后我选择“访问数据Mongodb”。然后我运行gs-access-data-mongodb-complete,它运行得很好。教程在这里[在此输入链接描述] [1] 但是,当我创建一个Spring starter项目时,依赖项我选择了MongoDB,Rest Repositories。我从上面复制完全相同的代码,但它不起作用。
任何人都可以帮助我吗?
的pom.xml http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>org.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</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>
包演示;
import org.springframework.data.annotation.Id;
公共类客户{
@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 String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
公共接口CustomerRepository扩展了MongoRepository {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
公共类DemoApplication实现CommandLineRunner {
@Autowired
private CustomerRepository repository;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
repository.deleteAll();
// save a couple of customers
repository.save(new Customer("Alice", "Smith"));
repository.save(new Customer("Bob", "Smith"));
// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
// fetch an individual customer
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
System.out.println(repository.findByFirstName("Alice"));
System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByLastName("Smith")) {
System.out.println(customer);
}
}
}