1 Star 0 Fork 0

gsean-springboot/spring-data-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
bom
cassandra
example
kotlin
src
README.md
pom.xml
reactive
util
pom.xml
couchbase
elasticsearch
geode
jdbc
jpa
ldap
map
mongodb
multi-store
neo4j
r2dbc
redis
rest
web
.gitignore
.travis.yml
LICENSE
README.md
pom.xml
settings.xml
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Spring Data Cassandra - Kotlin examples

This project contains samples of Kotlin-specific features of Spring Data (Cassandra).

Value defaulting on entity construction

Kotlin allows defaulting for constructor- and method arguments. Defaulting allows usage of substitute values if a field in the document is absent or simply null. Spring Data inspects objects whether they are Kotlin types and uses the appropriate constructor.

@Table
data class Person(@PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED) val firstname: String? = "", val lastname: String = "White")

operations.cqlOperations.execute(QueryBuilder.insertInto("person").value("firstname", "Walter"))

val person = operations.query<Person>()
				.matching(query(where("firstname").isEqualTo("Walter")))
				.firstValue()!!

assertThat(person.lastname).isEqualTo("White")

Kotlin Extensions

Spring Data exposes methods accepting a target type to either query for or to project results values on. Kotlin represents classes with its own type, KClass which can be an obstacle when attempting to obtain a Java Class type.

Spring Data ships with extensions that add overloads for methods accepting a type parameter by either leveraging generics or accepting KClass directly.

operations.getTableName<Person>()
 
operations.getTableName(Person::class)

Nullability

Declaring repository interfaces using Kotlin allows expressing nullability constraints on arguments and return types. Spring Data evaluates nullability of arguments and return types and reacts to these. Passing null to a non-nullable argument raises an IllegalArgumentException, as you're already used to from Kotlin. Spring Data helps you also to prevent null in query results. If you wish to return a nullable result, use Kotlin's nullability marker ?. To prevent null results, declare the return type of a query method as non-nullable. In the case a query yields no result, a non-nullable query method throws EmptyResultDataAccessException.

interface PersonRepository : CrudRepository<Person, String> {

	/**
	 * Query method declaring a nullable return type that allows to return null values.
	 */
	fun findOneOrNoneByFirstname(firstname: String): Person?

	/**
	 * Query method declaring a nullable argument.
	 */
	fun findNullableByFirstname(firstname: String?): Person?

	/**
	 * Query method requiring a result. Throws [org.springframework.dao.EmptyResultDataAccessException] if no result is found.
	 */
	fun findOneByFirstname(firstname: String): Person
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gsean-springboot/spring-data-examples.git
git@gitee.com:gsean-springboot/spring-data-examples.git
gsean-springboot
spring-data-examples
spring-data-examples
main

搜索帮助