简体   繁体   English

如何使用Spring CrudRepository查询布尔属性?

[英]How to query for boolean property with Spring CrudRepository?

I'm using Spring CrudRepository for database queries. 我正在使用Spring CrudRepository进行数据库查询。 How can I create a method signature (not writing SQL select statement myself) for a boolean property? 如何为布尔属性创建方法签名(不是自己编写SQL select语句)?

The following does not work: 以下不起作用:

class MyEntity {
       private boolean active;
}


interface MyEntityRepository implements CrudRepository<MyEntity, Long> {
   List<MyEntity> findActive(); //or also: findNotActive();
}

I would do: 我会做:

interface MyEntityRepository implements CrudRepository<MyEntity, Long> {
   List<MyEntity> findByActive(Boolean active);
}

Then the service layer would be 那么服务层就是

public class MyEntityServiceImpl implements MyEntityService{


   public List<MyEntity> findActive() {
      return myEntityRepository.findByActive(true);
   }
}

UPDATE UPDATE

As pointed out by @OliverGierke you could simplify your repository even more by doing: 正如@OliverGierke所指出的,您可以通过以下方式进一步简化您的存储库:

interface MyEntityRepository implements CrudRepository<MyEntity, Long> {
   List<MyEntity> findByActiveTrue(); //you could also use findByActiveFalse
}

For all the supported keywords you should see the section 对于所有受支持的关键字,您应该看到该部分

Query creation 查询创建

of the reference documentation 参考文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM