我是Spring Boot的新手,我发现Application-class使用了CrudRepository接口。我看到来自CrudRepository接口的.save()-method被调用,但我不明白这个方法的实现位置。这是否发生在Spring的后端某处?

这是Application-class:

@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public CommandLineRunner demo(CustomerRepository repository) {
        return (args) -> {
            // save a couple of customers
            repository.save(new Customer("Jack", "Bauer"));
            repository.save(new Customer("Chloe", "O'Brian"));
            repository.save(new Customer("Kim", "Bauer"));
            repository.save(new Customer("David", "Palmer"));
            repository.save(new Customer("Michelle", "Dessler"));

            // fetch all customers
            log.info("Customers found with findAll():");
            log.info("-------------------------------");
            for (Customer customer : repository.findAll()) {
                log.info(customer.toString());
            }
            log.info("");

            // fetch an individual customer by ID
            repository.findById(1L)
                .ifPresent(customer -> {
                    log.info("Customer found with findById(1L):");
                    log.info("--------------------------------");
                    log.info(customer.toString());
                    log.info("");
                });

            // fetch customers by last name
            log.info("Customer found with findByLastName('Bauer'):");
            log.info("--------------------------------------------");
            repository.findByLastName("Bauer").forEach(bauer -> {
                log.info(bauer.toString());
            });
            // for (Customer bauer : repository.findByLastName("Bauer")) {
            //  log.info(bauer.toString());
            // }
            log.info("");
        };
    }

}

CustomerRepository扩展了CrudRepository:

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
}
分析解答

根据陈灿的回答:

JpaRepository扩展了PagingAndSortingRepository,后者又扩展了CrudRepository。

他们的主要职能是:

CrudRepository mainly provides CRUD functions.

PagingAndSortingRepository provides methods to do pagination and sorting records.

JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.

以下是我当前项目中的一个示例,其中我实现了CrudRepository,PagingAndSortingRepository和JpaRepository。因此,当您只需要CRUD函数时,就可以实现它。当你需要与CRUD一起分页时。你去PagingAndSortingRepository。然后,当您需要更多功能时,JpaRepository会适合您,

public interface CartRepository扩展了CrudRepository {

@Query("select max(s.itemId) FROM ShoppingCart s")
public Integer findMaxItemId();

public List<ShoppingCart> findByCartId(String cartId);

public ShoppingCart findByItemId(int itemId);
}

公共接口ProductRepository扩展PagingAndSortingRepository {

@Query(value = "select p from Product p where p.description = SUBSTRING(p.description, 1,:description_length)")
List<Product> getAll(@Param("description_length")Integer description_length, Pageable pageable);


@RepositoryRestResource

public interface AttributesRepository扩展JpaRepository {

/**
 * Retrieves all attributes.
 *
 * 
 * @return the attribute objects
 */
@Query("select new com.turing.ecommerce.DTO.AttributeDTO(a.attributeId, a.name)"
        + " from Attribute a ")
List<AttributeDTO> findAllAttributes();
} 

}