Extensions
Kotlin extensions 提供了扩展现有类并使其具附加功能的能力。Spring Data Kotlin API 使用这些扩展为现有的 Spring API 添加新的,特定的 Kotlin 便利性功能。
Kotlin extensions provide the ability to extend existing classes with additional functionality. Spring Data Kotlin APIs use these extensions to add new Kotlin-specific conveniences to existing Spring APIs.
请记住,需要导入 Kotlin 扩展才能使用它们。类似于静态导入,在大多数情况下,IDE 应自动建议导入。 Keep in mind that Kotlin extensions need to be imported to be used. Similar to static imports, an IDE should automatically suggest the import in most cases. |
例如, Kotlin reified type parameters 提供针对 JVM generics type erasure 的解决方法,而 Spring Data 提供了一些扩展来利用此功能。这使得获得更好的 Kotlin API 成为可能。
For example, Kotlin reified type parameters provide a workaround for JVM generics type erasure, and Spring Data provides some extensions to take advantage of this feature. This allows for a better Kotlin API.
要在 Java 中检索 SWCharacter
对象列表,您通常会编写以下内容:
To retrieve a list of SWCharacter
objects in Java, you would normally write the following:
Flux<SWCharacter> characters = template.query(SWCharacter.class).inTable("star-wars").all()
使用 Kotlin 和 Spring Data 扩展,您可以编写以下内容:
With Kotlin and the Spring Data extensions, you can instead write the following:
val characters = template.query<SWCharacter>().inTable("star-wars").all()
// or (both are equivalent)
val characters : Flux<SWCharacter> = template.query().inTable("star-wars").all()
与 Java 一样,Kotlin 中的 characters
是强类型的,但 Kotlin 的巧妙类型推断允许使用更简洁的语法。
As in Java, characters
in Kotlin is strongly typed, but Kotlin’s clever type inference allows for shorter syntax.
适用于 Apache Cassandra 的 Spring Data 提供以下扩展:
Spring Data for Apache Cassandra provides the following extensions:
-
Reified generics support for
CassandraOperations
(including async and reactive variants),CqlOperations
(including async and reactive variants)FluentCassandraOperations
,ReactiveFluentCassandraOperations
,Criteria
, andQuery
. -
[kotlin.coroutines] extensions for
ReactiveFluentCassandraOperations
.