在我的 Spring Data 存储库中,我(必须)使用@Query注释使用自定义查询。我知道我可以限制命名查询中的结果数量
Iterable<Person> findFirst5OrderByLastName()
或者可以通过传递这样的可分页来限制结果的数量
Iterable<Person> findByLastName(String lastName, Pageable pageable)
但是在使用自定义@Query注释时是否可以实现相同的目标?
TIA
编辑
正如我所看到的,我的问题有点令人困惑,请澄清一下:我想要的是在使用自定义查询时限制我得到的结果数量,这样我既不
1) 需要通过 pageable 指定结果大小
2) 需要使用命名查询来指定结果大小
事实上,我希望结果数量的限制在调用方法时完全透明(因此不传递 a Pageable)并且不依赖 Spring Data 的命名方案(因为方法的含义/功能最好通过自定义名称)
我目前正在学习 Spring 的知识。
我试图像这样自动装配一个方法参数:
@RequestMapping("/hello")
public String something(@Autowire AnInterface ai) {
ai.doSomething();
return "/";
}
Run Code Online (Sandbox Code Playgroud)
具有以下接口和类:
@Component
public interface AnInterface {
void doSomething();
}
@Component
public class Implementation implements AnInterface {
@Autowired private AnotherInterface ai;
public Implementation() { ; }
public Implementation(AnotherInterface ai) {
this.ai = ai;
}
public void setAi(AnotherInterface ai) {
this.ai = ai;
}
@Override
public void doSomething() {
System.out.println(ai.hello());
}
}
@Component
public interface AnotherInterface {
String hello();
}
@Component
public class AnotherImplementation implements …Run Code Online (Sandbox Code Playgroud)