通过委托增强Spring数据存储库
我最近写了几篇有关Kotlin代表团的文章。 通过这样做,我实现了一种将其应用于Spring Data存储库的有用方法。 这将使Spring Data在提供定制路线的同时继续散布一些魔力。 这篇文章中显示的代码在Kotlin中,但仍然与Java有关。
这篇文章使用R2DBC,但是内容足够通用,可以应用于任何Spring Data模块。
如果您在这些领域没有太多的背景知识,那么在Kotlin中 使用Spring Data R2DBC和类委托读取异步RDBMS访问将是有益的。
作为回顾。 Spring Data提供的魔力是什么?
Spring Data允许您编写一个仅需要定义所需查询的接口。 然后它将完成创建实现和为您注入依赖项的所有工作。 看起来像这样:
@Repository interface PersonRepository : R2dbcRepository<Person, Int> { @Query ( "SELECT * FROM people WHERE age > $1" ) fun findAllByAgeGreaterThan(age: Int): Flux<Person> }
由于正在使用Spring Data R2DBC,因此尚不完全支持完全推断的查询。 这就是为什么手动写出查询的原因。
不利的一面是它正在基于接口创建实现。 因此,如果要进行任何类型的自定义,则需要自己创建接口的实例,注入其依赖关系并实现每个查询。 例如:
class PersonRepositoryImpl( private val entity: RelationalEntityInformation<Person, Int>, private val databaseClient: DatabaseClient, converter: R2dbcConverter, private val accessStrategy: ReactiveDataAccessStrategy ) : SimpleR2dbcRepository<Person, Int>(entity, databaseClient, converter, accessStrategy), PersonRepository { override fun findAllByAgeGreaterThan(age: Int): Flux<Person> { val mapper: StatementMapper.TypedStatementMapper<Person> = accessStrategy.statementMapper.forType(entity.javaType) val selectSpec: StatementMapper.SelectSpec = mapper .createSelect(entity.tableName) .withProjection(accessStrategy.getAllColumns(entity.javaType)) .withCriteria(Criteria.where( "age" ).greaterThan(age)) val operation: PreparedOperation<*> = mapper.getMappedObject(selectSpec) return databaseClient.execute().sql(operation).`as`(entity.javaType).fetch().all() } }
是的,该查询代码可能很糟糕,我相信您可以做得更好。 你明白我的意思。
可以通过委派基于您的接口实现的Spring仓库来消除创建此类的麻烦。 然后,您可以添加所需的所有自定义。
在Kotlin中,这看起来像:
@Repository class DelegatingPersonRepository( private val delegate: PersonRepository) : PersonRepository by delegate { override fun <S : Person> save(objectToSave: S): Mono<S> { // override `save` implementation } // any other overrides (kotlin provides delegated implementations) }
在Java中,这比较麻烦,但仍然可以轻松实现:
@Repository public class DelegatingPersonRepository implements PersonRepository { private final PersonRepository delegate; public DelegatingPersonRepository(PersonRepository delegate) { this .delegate = delegate; } @Override public Flux<Person> findAllByAgeGreaterThan( int age) { return delegate.findAllByAgeGreaterThan(age); } @Override public <S extends Person> Mono<S> save(S entity) { // override `save` implementation } // all other implementations of `PersonRepository` functions }
在这两个版本中, DelegatingPersonRepository
调用PersonRepository
定义的findAllByAgeGreaterThan
的实现。 到目前为止,还没有直接花费精力来编写查询数据库的功能。
使用DelegatingPersonRepository
,所有未覆盖的函数调用将委托给Spring创建的PersonRepository
的实现。
对于像我这样的人,他真的不喜欢将SQL查询放在一起并编写所有转换代码。 通过这种方式使用委派确实可以使您充分利用Spring Data的优势,同时仍然为您提供自定义结果的空间。 您节省的代码量实际上可能不是那么大。 但是,将其组合在一起所需的工作量大大减少了。 让Spring为您完成所有繁重的工作!
翻译自: https://www.javacodegeeks.com/2019/09/augmenting-spring-data-repository-delegation.html
通过委托增强Spring数据存储库相关推荐
- 将自定义功能添加到Spring数据存储库
Spring Data非常方便,并且避免了样板代码,从而加快了开发速度. 但是,在某些情况下,注释查询不足以实现您可能想要实现的自定义功能. 因此,spring数据允许我们向Spring数据存储库添加 ...
- 将EntityManager.refresh添加到所有Spring数据存储库
在我以前的文章<从Spring Data JPA访问EntityManager>中,我展示了如何扩展单个Spring Data JPA存储库以访问EntityManager.refresh ...
- 小型数据库_如果您从事“小型科学”工作,那么您是否正在利用数据存储库?
小型数据库 If you're a scientist, especially one performing a lot of your research alone, you probably ha ...
- 大数据:70多个网站让你免费获取大数据存储库
大数据:70多个网站让你免费获取大数据存储库 你是否需要大量的数据来检验你的APP性能?最简单的方法是从网上免费数据存储库下载数据样本.但这种方法最大的缺点是数据很少有独特的内容并且不一定能达到预期的 ...
- spring jpa 流式_从响应式Spring Data存储库流式传输实时更新
spring jpa 流式 这篇文章详细介绍了从数据库到对该数据感兴趣的任何其他组件进行流更新的幼稚实现. 更准确地说,如何更改Spring Data R2DBC存储库以向相关订阅者发出事件. 对R2 ...
- 从响应式Spring Data存储库流式传输实时更新
这篇文章详细介绍了从数据库到对该数据感兴趣的任何其他组件进行流更新的幼稚实现. 更确切地说,如何更改Spring Data R2DBC存储库以向相关订阅者发出事件. 对R2DBC和Spring的一点背 ...
- 5G NGC — UDR 统一数据存储库,UDSF 非结构化数据存储功能
目录 文章目录 目录 计算与存储分离 增强网络执行效率 增强网元容灾冗余 UDR 与 UDSF UDR UDSF 计算与存储分离 增强网络执行效率 在 4G EPC 中,用户签约信息被存储在 HSS ...
- java 分页 不 排序_java - 我应该如何将未分页但已排序的Pageable传递给Spring JPA存储库? - 堆栈内存溢出...
现在,我实现了自己的Pageable类,如下所示: import org.springframework.data.domain.Pageable; import org.springframewor ...
- 使用JPA和@NamedQueries的Spring数据
如果在JPA实体上使用Spring Data和@NamedQuery批注,则可以使用spring数据存储库以更方便的方式轻松使用它们. 在先前的博客中,我们使用spring boot和docker 创 ...
最新文章
- JetBrains大力推广Kotlin为哪般?
- 在Fragment中实现百度地图,定位到当前位置(基于SDKv2.1.0)
- python显示图像文件要放在哪儿_opencv显示图像要把图像放在哪里
- 获取其他线程的数据用 queue, 多进程Q
- 存储基础知识二 主要协议SCSI、FC、iSCSI
- 数据库-优化-检查慢日志是否开启
- 在 dotnet runtime 的容器中安装 dotnet global tool
- Stack Overflow 上人气最旺的 10 个 Java 问题
- android 拖动缩放窗口大小,Android小应用----图片的拖动、缩放
- HSRP与VRRP以及GLBP区别
- es6 混合commjs_前端模块化——CommonJS、ES6规范
- 手撸反向传播算法(附代码)
- 25.软件磁盘阵列(Software RAID)
- tomcat内存溢出,性能优化配置讲解
- Mybatis框架(一):一步步编写入门Mybatis程序(内附Mybatis各种配置文件)
- 完全数是由古人创造的一种整数,其因数的和(不包含本身)加起来就是数字本身。例如 6= 1+2+3 28=1+2+4+7+14 因数的和比数字本身大的数称为充沛数,因数和比数字本身大的数称为充沛数,因数
- 人机对话这件事为什么难?| 清华x-lab人工智能研习社
- 一个For语句导致死循环的例子
- 【ML】LambdaMART算法原理--应用场景
- ​力扣解法汇总648-单词替换