![](/img/trans.png)
[英]How to use @NamedQuery in spring a CrudRepository @Query?
[英]How to query for boolean property with Spring CrudRepository?
我正在使用Spring CrudRepository
進行數據庫查詢。 如何為布爾屬性創建方法簽名(不是自己編寫SQL select語句)?
以下不起作用:
class MyEntity {
private boolean active;
}
interface MyEntityRepository implements CrudRepository<MyEntity, Long> {
List<MyEntity> findActive(); //or also: findNotActive();
}
我會做:
interface MyEntityRepository implements CrudRepository<MyEntity, Long> {
List<MyEntity> findByActive(Boolean active);
}
那么服務層就是
public class MyEntityServiceImpl implements MyEntityService{
public List<MyEntity> findActive() {
return myEntityRepository.findByActive(true);
}
}
UPDATE
正如@OliverGierke所指出的,您可以通過以下方式進一步簡化您的存儲庫:
interface MyEntityRepository implements CrudRepository<MyEntity, Long> {
List<MyEntity> findByActiveTrue(); //you could also use findByActiveFalse
}
對於所有受支持的關鍵字,您應該看到該部分
查詢創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.