Jparepository là gì

  • Spring Daa JPA là gì?
  • Các tính năng cơ bản của Spring Data JPA.
  • Spring Data Commons and Spring Data JPA Repositories/interfaces
    • The Repository interface
    • Đoạn code trên mô tả nó là 1 interface marker, để hiểu hơn về các bạn có thể xem tại đây
    • The CrudRepository interface
    • The PagingAndSortingRepository interface
    • The QueryDslPredicateExecutor interface
    • Spring Data JPA Interfaces
    • JpaRepository interface
    • JpaSpecificationExecutor interface
    • Làm thế nào để sử dụng Spring Data JPA.

Spring Daa JPA là gì?

Spring Data JPA không phải là một JPA provider điều này khác với Hibernate. Hiberate là JPA provider hay nói cách khác Hibernate implement JPA.

Các tính năng cơ bản của Spring Data JPA.

  • Hỗ trợ xây dựng respositories dựa trên Spring và JPA.
  • Hỗ trợ phân trang, thực hiện query động..
  • Hỗ trợ XML mapping
  • Cấu hình JavaConfig bằng việc sử dụng @EnableJpaRepositories.

Spring Data Commons and Spring Data JPA Repositories/interfaces

Bên dưới là sơ đồ các interface từ Spring Data Commons và Spring Data JPA modules.

Spring Data Commonds là một phần của Spring data nó cung cấp nền tảng chia sẻ. Nó bao gồm các repository interfaces cũng như các metadata model cho persit java class. Nó có các giao diện sau.

  1. Repository interface
  2. CrudRepository interface
  3. PagingAndSortingRepository interface
  4. QueryDslPredicateExecutor interface

Chúng ta sẽ xem chi tiết về interface với source code và các hàm.

The Repository interface

package org.springframework.data.repository;

import org.springframework.stereotype.Indexed;

@Indexed
public interface Repository {

}

Đoạn code trên mô tả nó là 1 interface marker, để hiểu hơn về các bạn có thể xem tại đây

//www.baeldung.com/java-marker-interfaces

The CrudRepository interface

The CrudRepository : Interface này cung cấp các hành động CRUD.

package org.springframework.data.repository;

import java.util.Optional;  @NoRepositoryBean
public interface CrudRepository < T, ID > extends Repository < T, ID > {

     S save[S entity];

     Iterable < S > saveAll[Iterable < S > entities];

    Optional < T > findById[ID id];

    boolean existsById[ID id];

    Iterable < T > findAll[];

    Iterable < T > findAllById[Iterable < ID > ids];

    long count[];

    void deleteById[ID id];

    void delete[T entity];

    void deleteAll[];
}

Cách sử dụng:

  1. long count[] – Trả về số entity
  2. void delete[T entity] – Xóa một entity
  3. void deleteAll[] – Xóa tất cả các entity
  4. void deleteAll[Iterable

    Inject các thành phần tới các lớp khác..

    @Service
    public class CustomerServiceImpl implements CustomerService {
    
        @Autowired
        private CustomerRepository customerRepository;
    
        @Override
        @Transactional
        public List < Customer > getCustomers[] {
            return customerRepository.findAll[];
        }
    
        @Override
        @Transactional
        public void saveCustomer[Customer theCustomer] {
            customerRepository.save[theCustomer];
        }
    
        @Override
        @Transactional
        public Customer getCustomer[int id] throws ResourceNotFoundException {
            return customerRepository.findById[id].orElseThrow[
                [] - > new ResourceNotFoundException[id]];
        }
    
        @Override
        @Transactional
        public void deleteCustomer[int theId] {
            customerRepository.deleteById[theId];
        }
    }

    Cảm ơn các bạn đã theo dõi.

Chủ Đề