gpt4 book ai didi

java - Spring Boot 中的 CrudRespository 方法在哪里实现?

转载 作者:行者123 更新时间:2023-12-02 01:21:13 24 4
gpt4 key购买 nike

我是 Spring Boot 新手,我发现应用程序类使用了 CrudRepository 接口(interface)。我看到调用了 CrudRepository 接口(interface)中的 .save() 方法,但我不明白该方法是在哪里实现的。 Spring 后端是否会发生这种情况?

这是应用程序类:

@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);
}

最佳答案

根据已回答此问题的位置 Chan Chan :

JpaRepository 扩展了 PagingAndSortingRepository,而 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.

这是我当前项目中的一个示例,我在其中实现了 CrudRepositoryPagingAndSortingRepositoryJpaRepository。所以当你只需要CRUD功能时,你就实现它。当您需要分页和 CRUD 时。您可以选择PagingAndSortingRepository。然后,当您需要更多功能时,JpaRepository 适合您,

public interface CartRepository extends CrudRepository<ShoppingCart, Integer> {

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

public List<ShoppingCart> findByCartId(String cartId);

public ShoppingCart findByItemId(int itemId);
}


public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {

@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 extends JpaRepository<Attribute, Integer> {

@Query("select new com.turing.ecommerce.DTO.AttributeDTO(a.attributeId, a.name)"
+ " from Attribute a ")
List<AttributeDTO> findAllAttributes();
}

关于java - Spring Boot 中的 CrudRespository 方法在哪里实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57624503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com