如何创建 Spring 规范对象?
How to create a Spring Specification object?
使用 tkaczmarzyk arg-resolver library
see here
我的 Spring Boot/Hibernate 项目
我想搜索我的数据库。
实体:
public class Animal {
private String color;
private String name;
...
}
存储库 class:
public inteface AnimalRepository extends JpaRepository<Animal, Long>, JpaSpecificationExecutor<Animal> {
}
我创建了一个扩展 Specification
:
的新界面
@And({
@Spec(params = "color", spec = Equal.class),
@Spec(params = "name", spec = Equal.class)
})
public interface AnimalSearch extends Specification<Animal> {
}
现在我想用某种方法做这样的事情:
public void myMethod() {
AnimalSearch search; /* here is my question. How can I create some kind of object with 2 parameters: color and name for the interface?*/
Collection<Animal> animalsFiltered = animalRepository.findAll(search);
// etc
}
我是否需要创建一个像这样的 class:
public class AnimalSearchObject implements AnimalSearch {
private String color;
private String name;
...
}
并拥有:
AnimalSearchObject search = new AnimalSearchObject("my_color", "my_name");
Collection<Animal> animalsFiltered = animalRepository.findAll(search);
或者有更简单的方法吗?
你不能这样做。tkaczmarzyk arg-resolver library
仅对请求中收到的映射参数有帮助。使用此库无法完成我所要求的
使用 tkaczmarzyk arg-resolver library
see here
我的 Spring Boot/Hibernate 项目
我想搜索我的数据库。
实体:
public class Animal {
private String color;
private String name;
...
}
存储库 class:
public inteface AnimalRepository extends JpaRepository<Animal, Long>, JpaSpecificationExecutor<Animal> {
}
我创建了一个扩展 Specification
:
@And({
@Spec(params = "color", spec = Equal.class),
@Spec(params = "name", spec = Equal.class)
})
public interface AnimalSearch extends Specification<Animal> {
}
现在我想用某种方法做这样的事情:
public void myMethod() {
AnimalSearch search; /* here is my question. How can I create some kind of object with 2 parameters: color and name for the interface?*/
Collection<Animal> animalsFiltered = animalRepository.findAll(search);
// etc
}
我是否需要创建一个像这样的 class:
public class AnimalSearchObject implements AnimalSearch {
private String color;
private String name;
...
}
并拥有:
AnimalSearchObject search = new AnimalSearchObject("my_color", "my_name");
Collection<Animal> animalsFiltered = animalRepository.findAll(search);
或者有更简单的方法吗?
你不能这样做。tkaczmarzyk arg-resolver library
仅对请求中收到的映射参数有帮助。使用此库无法完成我所要求的