Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package hello;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.CommandLineRunner;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
  8.  
  9. @SpringBootApplication
  10. public class Application implements CommandLineRunner {
  11.  
  12. @Autowired
  13. private CustomerRepository repository;
  14.  
  15. public static void main(String[] args) {
  16. SpringApplication.run(Application.class, args);
  17. }
  18.  
  19. @Override
  20. public void run(String... args) throws Exception {
  21.  
  22. repository.deleteAll();
  23.  
  24. // save a couple of customers
  25. repository.save(new Customer("Alice", "Smith"));
  26. repository.save(new Customer("Bob", "Smith"));
  27.  
  28. // fetch all customers
  29. System.out.println("Customers found with findAll():");
  30. System.out.println("-------------------------------");
  31. for (Customer customer : repository.findAll()) {
  32. System.out.println(customer);
  33. }
  34. System.out.println();
  35.  
  36. // fetch an individual customer
  37. System.out.println("Customer found with findByFirstName('Alice'):");
  38. System.out.println("--------------------------------");
  39. System.out.println(repository.findByFirstName("Alice"));
  40.  
  41. System.out.println("Customers found with findByLastName('Smith'):");
  42. System.out.println("--------------------------------");
  43. for (Customer customer : repository.findByLastName("Smith")) {
  44. System.out.println(customer);
  45. }
  46.  
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement