codecamp

Spring Cloud Datastore 按约定查询方法

public interface TradeRepository extends DatastoreRepository<Trade, String[]> {
  List<Trader> findByAction(String action);

  int countByAction(String action);

  boolean existsByAction(String action);

  List<Trade> findTop3ByActionAndSymbolAndPriceGreaterThanAndPriceLessThanOrEqualOrderBySymbolDesc(
  			String action, String symbol, double priceFloor, double priceCeiling);

  Page<TestEntity> findByAction(String action, Pageable pageable);

  Slice<TestEntity> findBySymbol(String symbol, Pageable pageable);

  List<TestEntity> findBySymbol(String symbol, Sort sort);
}

在上面的示例中TradeRepository查询方法是使用https://docs.spring.io/spring-data/data-commons/docs/current/reference/html#repositories基于方法名称生成的。 query-methods.query-creation [Spring Data查询创建命名约定]。

Cloud Datastore仅支持通过AND连接的过滤器组件以及以下操作:

  • equals
  • greater than or equals
  • greater than
  • less than or equals
  • less than
  • is null

在编写仅指定这些方法签名的自定义存储库接口之后,将为您生成实现,并且可以将其与存储库的自动关联实例一起使用。由于Cloud Datastore要求明确选择的字段必须全部一起出现在组合索引中,因此find基于名称的查询方法将以SELECT *的身份运行。

还支持删除查询。例如,诸如deleteByActionremoveByAction之类的查询方法会删除findByAction找到的实体。删除查询是作为单独的读取和删除操作而不是作为单个事务执行的,因为除非指定了查询的祖先,否则Cloud Datastore无法在事务中查询。 结果,removeBydeleteBy名称约定查询方法不能通过performInTransaction@Transactional批注在事务内部使用。

删除查询可以具有以下返回类型:

  • 一个整数类型,它是删除的实体数
  • 被删除的实体的集合
  • “无效”

方法可以具有org.springframework.data.domain.Pageable参数来控制分页和排序,或者具有org.springframework.data.domain.Sort参数来仅控制排序。有关详细信息, 请参见Spring Data文档

要在存储库方法中返回多个项目,我们支持Java集合以及org.springframework.data.domain.Pageorg.springframework.data.domain.Slice如果方法的返回类型为org.springframework.data.domain.Page,则返回的对象将包括当前页面,结果总数和页面总数。

返回Page的方法执行附加查询以计算总页数。另一方面,返回Slice的方法不会执行任何其他查询,因此效率更高。


Spring Cloud Datastore 对地图的读写支持
Spring Cloud Datastore 自定义GQL查询方法
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

三、Spring Cloud Netflix

SpringCloud Hystrix超时和Ribbon客户

SpringCloud 重试失败的请求

五、Spring Cloud Stream

六、SpringCloud Binder实现

SpringCloud 重试RabbitMQ Binder

SpringCloud Dead-Letter队列处理

八、Spring Cloud Sleuth

SpringCloud 当前Span

十二、Spring Cloud for Cloud Foundry

十三、Spring Cloud Contract

Spring Cloud Contract验证程序设置

SrpingCloud Gradle项目

十五、Spring Cloud网关

Spring Cloud 配置路由谓词工厂和网关过滤工厂

Spring Cloud TLS / SSL

Spring Cloud网关配置

SpringCloud 故障排除

十八、Spring Cloud GCP

Spring Cloud GCP Spring资源

Spring Cloud Spring JDBC

Spring Cloud Redis的Cloud Memorystore

Spring Cloud 云身份识别代理(IAP)身份验证

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }