nyl*_*l66 5 querydsl spring-data-jpa spring-data-rest
我期待用通用查找器构建REST接口.我们的想法是提供一个搜索表单,用户可以通过不提供任何参数来获取所有记录,也可以通过键入任何字段组合来优化搜索结果.
我用@RestResource注释JpaRepository的简单示例提供了一种开箱即用的方法来添加查找器,方法是使用@Query或方法名称约定
@RestResource(path = "users", rel = "users")
public interface UserRepository extends JpaRepository<User, Long>{
public Page<User> findByFirstNameStartingWithIgnoreCase(@Param("first") String fName, Pageable page);
}
Run Code Online (Sandbox Code Playgroud)
我希望添加一个自定义查找器来映射我的参数,并利用分页,排序和REST支持,其中实际的实现查询将动态组合(可能使用QueryDSL)该方法将有n个参数(p 1 ... p n)并且看起来像:
public Page<User> findCustom(@Param("p1") String p1, @Param("p2") String p2, ... @Param("pn") String pn, Pageable page);
Run Code Online (Sandbox Code Playgroud)
我尝试过以下描述的方法:
但我的自定义方法不能从存储库的REST接口(/ users/search)中获得
我希望有人已经明白了这一点,并会很友好地给我一些方向.
尝试这样的事情,但当然会适应您的场景:
public interface LocationRepository extends CrudRepository,
PagingAndSortingRepository,
LocationRepositoryExt {
}
Run Code Online (Sandbox Code Playgroud)
public interface LocationRepositoryExt {
@Query
public List findByStateCodeAndLocationNumber(@Param("stateCode") StateCode stateCode, @Param("locationNumber") String locationNumber);
}
Run Code Online (Sandbox Code Playgroud)
class LocationRepositoryImpl extends QueryDslRepositorySupport implements LocationRepositoryExt {
private static final QLocation location = QLocation.location;
public LocationRepositoryImpl() {
super(Location.class);
}
@Override
public Page findByStateAndLocationNumber(@Param("state") State state, @Param("locationNumber") String locationNumber, Pageable pageable) {
List locations = from(location)
.where(location.state.eq(state)
.and(location.locationNumber.eq(locationNumber)))
.list(location);
return new PageImpl(locations, pageable, locations.size());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1792 次 |
| 最近记录: |