如何在Spring Boot中创建一个通用存储库,该存储库接受一个实体和少量属性,并基于该属性返回所有记录? [英] How to make a generic repository in Spring Boot that accepts an entity and few attributes and returns all the records based on the attributes?

查看:195
本文介绍了如何在Spring Boot中创建一个通用存储库,该存储库接受一个实体和少量属性,并基于该属性返回所有记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个通用存储库,该存储库接受实体类和一些属性,例如start_date和end_date等,并返回表中的所有记录.

I want to make a generic repository that accepts the entity class and few attributes like start_date and end_date, etc and returns all the records in the table.

要使用存储库获取单个实体的结果,我需要编写一个自定义查询.我不确定如何为传递的所有实体以及根据属性进行过滤的通用方法编写自定义查询.

To fetch the results for a single entity using repository I will need to write a custom query. I am not sure how would I write a Custom query in a generic way for any entity that is passed and filter according to the attributes.

推荐答案

服务:

Person person = new Person();
person.setFirstname("Dave");
Example<Person> example = Example.of(person); 

回购接口:

public interface QueryByExampleExecutor<T> {
   <S extends T> S findOne(Example<S> example);
   <S extends T> Iterable<S> findAll(Example<S> example);
}

这篇关于如何在Spring Boot中创建一个通用存储库,该存储库接受一个实体和少量属性,并基于该属性返回所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆