我的任务就是让与Spring数据REST高级搜索。我怎样才能实现呢?
我设法让做一个简单的搜索方法,像这样的:
public interface ExampleRepository extends CrudRepository<Example, UUID>{
@RestResource(path="searchByName", rel="searchByName")
Example findByExampleName(@Param("example") String exampleName);
}
这个例子完美的作品,如果我必须只需转到的网址:
.../api/examples/search/searchByName?example=myExample
但我必须做什么,如果有多个字段进行搜索吗?
例如,如果我的示例类有5场,我应该有什么的实施,使所有possibiles的Fileds高级搜索?
看看这个例子:
.../api/examples/search/searchByName?filed1=value1&field2=value2&field4=value4
还有这个:
.../api/examples/search/searchByName?filed1=value1&field3=value3
我必须做的,实施适当的方式本次搜索?
谢谢。
春节休息的数据已经集成了网络支持QueryDSL以及您可以使用您的高级搜索需求。你需要改变你的资料库,以实现QueryDslPredicateExecutor
事情会工作的开箱。
下面是从对功能的blog文章示例:
$ http :8080/api/stores?address.city=York
{
"_embedded": {
"stores": [
{
"_links": {
…
},
"address": {
"city": "New York",
"location": { "x": -73.938421, "y": 40.851 },
"street": "803 W 181st St",
"zip": "10033-4516"
},
"name": "Washington Hgts/181st St"
},
{
"_links": {
…
},
"address": {
"city": "New York",
"location": { "x": -73.939822, "y": 40.84135 },
"street": "4001 Broadway",
"zip": "10032-1508"
},
"name": "168th & Broadway"
},
…
]
},
"_links": {
…
},
"page": {
"number": 0,
"size": 20,
"totalElements": 209,
"totalPages": 11
}
}
查询方法的实现被广泛记录在Spring reference documentation和吨的技术博客,但是相当多的一堆已经过时。
由于您的问题可能是“我怎样才能执行与字段的任何组合的多参数搜索没有宣布一个可怕的很多findBy *方法?”,答案是Querydsl,这是由Spring支持。
我已经找到了此任务的工作方案。
@RepositoryRestResource(excerptProjection=MyProjection.class)
public interface MyRepository extends Repository<Entity, UUID> {
@Query("select e from Entity e "
+ "where (:field1='' or e.field1=:field1) "
+ "and (:field2='' or e.field2=:field2) "
// ...
+ "and (:fieldN='' or e.fieldN=:fieldN)"
Page<Entity> advancedSearch(@Param("field1") String field1,
@Param("field2") String field2,
@Param("fieldN") String fieldN,
Pageable page);
}
有了这个解决方案,使用该基地网址:
http://localhost:8080/api/examples/search/advancedSearch
我们可以把高级搜索与我们所需要的所有领域。
一些例子:
http://localhost:8080/api/examples/search/advancedSearch?field1=example
// filters only for the field1 valorized to "example"
http://localhost:8080/api/examples/search/advancedSearch?field1=name&field2=surname
// filters for all records with field1 valorized to "name" and with field2 valorized to "surname"
我想你可以尝试以下操作:
List<Person> findDistinctPeopleByLastnameOrFirstname(@Param("lastName")String lastname, @Param("firstName")String firstname);
和examples/search/searchByLastnameOrFirstname?firstName=value1&lastName=value2
我成功地实现这个使用Query by Example。
比方说,你有以下型号:
@Entity
public class Company {
@Id
@GeneratedValue
Long id;
String name;
String address;
@ManyToOne
Department department;
}
@Entity
public class Department {
@Id
@GeneratedValue
Long id;
String name;
}
和库:
@RepositoryRestResource
public interface CompanyRepository extends JpaRepository<Company, Long> {
}
(请注意,JpaRepository器具QueryByExampleExecutor)。
现在你实现自定义控制器:
@RepositoryRestController
@RequiredArgsConstructor
public class CompanyCustomController {
private final CompanyRepository repository;
@GetMapping("/companies/filter")
public ResponseEntity<?> filter(
Company company,
Pageable page,
PagedResourcesAssembler assembler,
PersistentEntityResourceAssembler entityAssembler
){
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnoreCase()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
Example example = Example.of(company, matcher);
Page<?> result = this.repository.findAll(example, page);
return ResponseEntity.ok(assembler.toResource(result, entityAssembler));
}
}
然后你就可以让查询:
localhost:8080/companies/filter?name=google&address=NY
你甚至可以像查询嵌套实体:
localhost:8080/companies/filter?name=google&department.name=finances
我忽略了一些细节为简洁起见,但我创建了一个Github上working example。