使用扩展MongoRepository的未实现接口

时间:2018-07-22 00:42:23

标签: java spring mongodb spring-boot

我下载并成功运行了该项目:

https://github.com/spring-guides/gs-accessing-data-mongodb

在文件上:

https://github.com/spring-guides/gs-accessing-data-mongodb/blob/master/complete/src/main/java/hello/CustomerRepository.java

定义了接口access_token

CustomerRepository

在文件上使用的

https://github.com/spring-guides/gs-accessing-data-mongodb/blob/master/complete/src/main/java/hello/Application.java

方法内:import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository; public interface CustomerRepository extends MongoRepository<Customer, String> { public Customer findByFirstName(String firstName); public List<Customer> findByLastName(String lastName); }

Application / run

我有2个问题:

  1. 接口中的这​​两个方法在哪里实现:@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); } }

  2. 系统如何知道要比较的字段(在CustomerRepository内),以便在使用这两种方法时获得正确的实例?

谢谢!

0 个答案:

没有答案