我是Spring Boot的新手,我看到Application-class使用CrudRepository接口。我看到从CrudRepository接口调用了.save()方法,但是我不知道在哪里实现此方法。这会在春季后端发生吗?

这是应用程序类:

@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,这又扩展了CrudRepository

它们的主要功能是:


CrudRepository主要提供CRUD功能。

PagingAndSortingRepository提供分页和
排序记录。

JpaRepository提供了一些与JPA相关的方法,例如刷新
持久性上下文并批量删除记录。


这是我当前项目中实现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();
}

09-29 22:18