Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1.  
  2. https://docs.spring.io/spring-data/jpa/docs/2.1.5.RELEASE/reference/html/ muc 5.3.2. Query Creation
  3. https://spring.io/guides/gs/accessing-data-mysql/
  4. mysql workbench
  5.  
  6. ```java
  7.  
  8. @Entity
  9. public class Customer {
  10.  
  11. @Id
  12. @GeneratedValue(strategy=GenerationType.AUTO)
  13. private Long id;
  14. private String firstName;
  15. private String lastName;
  16.  
  17. protected Customer() {}
  18.  
  19. public Customer(String firstName, String lastName) {
  20. this.firstName = firstName;
  21. this.lastName = lastName;
  22. }
  23.  
  24. public Long getId() {
  25. return id;
  26. }
  27. }
  28.  
  29. ===========================
  30.  
  31. public interface CustomerRepository extends CrudRepository<Customer, Long> {
  32.  
  33. List<Customer> findByLastName(String lastName);
  34. }
  35.  
  36.  
  37. ===========================
  38.  
  39.  
  40. @Service
  41. public class CompanyService {
  42.  
  43. @Autowire
  44. private CustomerRepository customerRepository;
  45.  
  46. public void addCustomer(String firstName, String lastName){
  47. Customer customer = new Customer(fistName, lastName);
  48. customer = customerRepository.save(customer);
  49. System.out.println(customer.getId());
  50. }
  51.  
  52. public Customer getCustomerByid(Long id){
  53. Optional<Customer> optional = customerRepository.findById(id);
  54. Customer customer = optional.get();
  55.  
  56. return customer;
  57. }
  58.  
  59. public Customer findCustomerByLastName(String lastName){
  60. List<Customer> list = customerRepository.findByFirstName(lastName);
  61. return list;
  62. }
  63. }
  64. ```
  65.  
  66. ==========================
  67. ```yaml
  68. spring:
  69. jpa.hibernate.ddl-auto: create
  70. datasource:
  71. url: jdbc:mysql://localhost:3306/db_example
  72. username: springuser
  73. password: ThePassword
  74. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement