假設我想有一種方法,即獲得超級主客戶,其中有id=0
。如何在自定義彈簧庫中實現自定義方法?
我有Customer類:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
protected Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
According to dox,我應該創建兩個附加的類/接口。
所以我有CustomerCustom
接口:
public interface CustomerCustom {
Customer getVeryMainCustomer();
}
在方法getVeryMainCustomer
聲明。
然後我敞口庫接口輪番以下:
public interface CustomerRepository extends CrudRepository<Customer, Long>, CustomerCustom {
List<Customer> findByLastName(String lastName);
}
它同時擴展CrudRepository
和我CustomerCustom
。
但接下來我應該執行CustomerCustom
。但如何做到這一點?
我寫
public class CustomerCustomImpl implements CustomerCustom {
@Override
public Customer getVeryMainCustomer() {
return null;
}
}
殭屍不知道,是什麼在執行寫入。如何到達客戶?
什麼是應該做的方法?你有沒有想過使用'@ Query'?什麼是表格結構或「客戶」? – 2015-12-21 17:01:43
客戶的表結構由JPA根據「客戶」類定義自動定義。該方法應該選擇ID = 0的客戶。是的,如果可能,我想使用'@ Query' – Dims