Type-safe Queries for Kotlin

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.

Type-safe Queries for Kotlin

Kotlin 通过其语言语法及其扩展系统包含特定于领域的语言创建。Spring Data MongoDB 使用 Criteria 的 Kotlin 扩展,该扩展使用 Kotlin property references 构建类型安全查询。使用此扩展的查询通常具有更佳的可读性。Criteria 中大多数关键字都有一个匹配的 Kotlin 扩展,例如 inValuesregex

Kotlin embraces domain-specific language creation through its language syntax and its extension system. Spring Data MongoDB ships with a Kotlin Extension for Criteria using Kotlin property references to build type-safe queries. Queries using this extension are typically benefit from improved readability. Most keywords on Criteria have a matching Kotlin extension, such as inValues and regex.

请考虑以下示例来说明类型安全查询:

Consider the following example explaining Type-safe Queries:

import org.springframework.data.mongodb.core.query.*

mongoOperations.find<Book>(
  Query(Book::title isEqualTo "Moby-Dick")               1
)

mongoOperations.find<Book>(
  Query(titlePredicate = Book::title exists true)
)

mongoOperations.find<Book>(
  Query(
    Criteria().andOperator(
      Book::price gt 5,
      Book::price lt 10
    ))
)

// Binary operators
mongoOperations.find<BinaryMessage>(
  Query(BinaryMessage::payload bits { allClear(0b101) }) 2
)

// Nested Properties (i.e. refer to "book.author")
mongoOperations.find<Book>(
  Query(Book::author / Author::name regex "^H")          3
)
1 isEqualTo() is an infix extension function with receiver type KProperty<T> that returns Criteria.
2 For bitwise operators, pass a lambda argument where you call one of the methods of Criteria.BitwiseCriteriaOperators.
3 To construct nested properties, use the / character (overloaded operator div).