这在使用上是有限的,但是对于简单的自定义方法,您可以使用默认接口方法,例如:
import demo.database.Customer;
import org.springframework.data.repository.CrudRepository;
public interface CustomerService extends CrudRepository<Customer, Long> {
default void addSomeCustomers() {
Customer[] customers = {
new Customer("Józef", "Nowak", "nowakJ@o2.pl", 679856885, "Rzeszów", "Podkarpackie", "35-061", "Zamknięta 12"),
new Customer("Adrian", "Mularczyk", "adii333@wp.pl", 867569344, "Krosno", "Podkarpackie", "32-442", "Hynka 3/16"),
new Customer("Kazimierz", "Dejna", "sobieski22@weebly.com", 996435876, "Jarosław", "Podkarpackie", "25-122", "Korotyńskiego 11"),
new Customer("Celina", "Dykiel", "celina.dykiel39@yahoo.org", 947845734, "Żywiec", "Śląskie", "54-333", "Polna 29")
};
for (Customer customer : customers) {
save(customer);
}
}
}
编辑:
在这个春季教程中,它是这样写的:
Spring Data JPA 还允许您通过简单地声明其方法签名来定义其他查询方法。
因此,甚至可以只声明如下方法:
Customer findByHobby(Hobby personHobby);
如果 objectHobby
是 Customer 的属性,那么 Spring 会自动为您定义方法。