Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在SpringBoot中创建一个通用存储库,它接受一个实体和几个属性,并根据这些属性返回所有记录?_Java_Spring Boot_Spring Data Jpa_Spring Repositories - Fatal编程技术网

Java 如何在SpringBoot中创建一个通用存储库,它接受一个实体和几个属性,并根据这些属性返回所有记录?

Java 如何在SpringBoot中创建一个通用存储库,它接受一个实体和几个属性,并根据这些属性返回所有记录?,java,spring-boot,spring-data-jpa,spring-repositories,Java,Spring Boot,Spring Data Jpa,Spring Repositories,我想创建一个通用存储库,它接受entity类和一些属性,如start_date和end_date等,并返回表中的所有记录 要使用存储库获取单个实体的结果,我需要编写一个自定义查询。我不知道如何以通用方式为任何根据属性传递和筛选的实体编写自定义查询。因为您使用的是Spring Data JPA,所以您可以使用自己的方法声明自己的共享存储库接口并避免自定义查询。相同的方法用于提供中不存在的其他方法 例如,您可以声明: public interface SharedRepository<T, I

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


要使用存储库获取单个实体的结果,我需要编写一个自定义查询。我不知道如何以通用方式为任何根据属性传递和筛选的实体编写自定义查询。

因为您使用的是Spring Data JPA,所以您可以使用自己的方法声明自己的共享存储库接口并避免自定义查询。相同的方法用于提供中不存在的其他方法

例如,您可以声明:

public interface SharedRepository<T, ID> extends CrudRepository<T, ID> {

  List<T> findByStartDateAndEndDate(LocalDate startDate, LocalDate endDate); 

}
公共接口SharedRepository扩展了Crudepository{
列出findByStartDateAndEndDate(LocalDate startDate,LocalDate endDate);
}
然后从这个新接口扩展实体

@Repository
public interface PersonRepisotry extends SharedRepository<Person, Long> {

}

@Repository
public interface RoomRepository extends SharedRepository<Room, Long> {

}
@存储库
公共接口PersonRepisotry扩展了SharedRepository{
}
@存储库
公共接口RoomRepository扩展了SharedRepository{
}

PersonRepository
RoomRepository
都将具有
findByStartDateAndEndDate
方法。

由于您使用的是Spring Data JPA,您可以使用自己的方法声明自己的共享存储库接口,并避免自定义查询。相同的方法用于提供中不存在的其他方法

例如,您可以声明:

public interface SharedRepository<T, ID> extends CrudRepository<T, ID> {

  List<T> findByStartDateAndEndDate(LocalDate startDate, LocalDate endDate); 

}
公共接口SharedRepository扩展了Crudepository{
列出findByStartDateAndEndDate(LocalDate startDate,LocalDate endDate);
}
然后从这个新接口扩展实体

@Repository
public interface PersonRepisotry extends SharedRepository<Person, Long> {

}

@Repository
public interface RoomRepository extends SharedRepository<Room, Long> {

}
@存储库
公共接口PersonRepisotry扩展了SharedRepository{
}
@存储库
公共接口RoomRepository扩展了SharedRepository{
}
PersonRepository
RoomRepository
都将有
findByStartDateAndEndDate
方法。

服务:

Person person = new Person();
person.setFirstname("Dave");
Example<Person> example = Example.of(person); 
Person-Person=新的Person();
person.setFirstname(“Dave”);
例例=例(人);
回购接口:

public interface QueryByExampleExecutor<T> {
   <S extends T> S findOne(Example<S> example);
   <S extends T> Iterable<S> findAll(Example<S> example);
}
公共接口QueryByExampleExecutor{
S findOne(示例);
Iterable findAll(示例);
}

服务:

Person person = new Person();
person.setFirstname("Dave");
Example<Person> example = Example.of(person); 
Person-Person=新的Person();
person.setFirstname(“Dave”);
例例=例(人);
回购接口:

public interface QueryByExampleExecutor<T> {
   <S extends T> S findOne(Example<S> example);
   <S extends T> Iterable<S> findAll(Example<S> example);
}
公共接口QueryByExampleExecutor{
S findOne(示例);
Iterable findAll(示例);
}

这些只允许那些特定的属性。我想我需要写一个查询。因为实体实际上有一个名为timestamp的字段,它存储条目创建时的日期。我需要获取时间戳介于开始日期和结束日期之间的记录。还有其他一些字段,如member_id(外键)@KarolYes@mavriksc。这些只允许特定属性。我需要编写自定义查询。请尝试
findTimestampBetween(LocalDate开始,LocalDate结束)
,因为嵌套属性不是我的要求@KarolDowbeckithose只允许那些特定的属性。我想我需要写一个查询。因为实体实际上有一个名为timestamp的字段,它存储条目创建时的日期。我需要获取时间戳介于开始日期和结束日期之间的记录。还有其他一些字段,如member_id(外键)@KarolYes@mavriksc。这些只允许特定属性。我需要编写自定义查询。请尝试
findTimestampBetween(LocalDate开始,LocalDate结束)
,因为嵌套属性不是我的要求@在看到你关于约会间隔的评论后。我认为这行不通。你可以使用这个,然后只使用stream()。按日期筛选。。。这不是最好的解决方案,因为最好是在回购中进行所有操作,但必须对属性进行空检查并添加条件也有点麻烦。在看到您关于需要在日期之间进行操作的评论后。我认为这行不通。你可以使用这个,然后只使用stream()。按日期筛选。。。这不是最好的解决方案,因为最好是在回购中进行所有操作,但必须对属性进行空检查并添加条件也是一种麻烦。