Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://docs.spring.io/spring-data/jpa/docs/2.1.5.RELEASE/reference/html/ muc 5.3.2. Query Creation
- https://spring.io/guides/gs/accessing-data-mysql/
- mysql workbench
- ```java
- @Entity
- public class Customer {
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- private Long id;
- private String firstName;
- private String lastName;
- protected Customer() {}
- public Customer(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public Long getId() {
- return id;
- }
- }
- ===========================
- public interface CustomerRepository extends CrudRepository<Customer, Long> {
- List<Customer> findByLastName(String lastName);
- }
- ===========================
- @Service
- public class CompanyService {
- @Autowire
- private CustomerRepository customerRepository;
- public void addCustomer(String firstName, String lastName){
- Customer customer = new Customer(fistName, lastName);
- customer = customerRepository.save(customer);
- System.out.println(customer.getId());
- }
- public Customer getCustomerByid(Long id){
- Optional<Customer> optional = customerRepository.findById(id);
- Customer customer = optional.get();
- return customer;
- }
- public Customer findCustomerByLastName(String lastName){
- List<Customer> list = customerRepository.findByFirstName(lastName);
- return list;
- }
- }
- ```
- ==========================
- ```yaml
- spring:
- jpa.hibernate.ddl-auto: create
- datasource:
- url: jdbc:mysql://localhost:3306/db_example
- username: springuser
- password: ThePassword
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement