Spring 启动 JPA Like 查询
Spring Boot JPA Like query
我正在尝试为我的 table 用户实现搜索功能。我想制作一个 JpaRepository 方法,该方法将给我一个可分页的用户列表,这些用户的电子邮件包含给定的字符串,我该怎么做?
这是我试过的
存储库:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findByEmailContaining(String email, Pageable pageable);
}
发球:
public Page<User> findPaginatedEmail(int pageNo, int pageSize, String sortField, String sortDirection, String text) {
Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortField).ascending() :
Sort.by(sortField).descending();
Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort);
return this.userRepository.findByEmailContaining(text, pageable);
}
有时它会工作并填充我的 table 有时它不会。问题是什么?我该如何实施?
它工作正常,但一段时间后它就坏了,我不知道为什么,然后它找不到任何东西
您要找的是:
public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
List<Product> findAllByPrice(double price, Pageable pageable);
}
您只需添加一个额外的函数参数 Pageable 并且您还需要使用 PagingAndSortingRepository 而不是 JpaRepository
扩展您的 Repository class
方法有很多种,如果这里使用了SimpleJpaRepository调用参数Specification和Pageable的findAll方法,返回Page接口。
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
@GetMapping(path = "/pageable")
public Page<?> getCountries(@RequestParam(name = "countryName", required = false) String countryName,
@PageableDefault(page = 0, size = 20) @SortDefault.SortDefaults({
@SortDefault(sort = "id", direction = Sort.Direction.ASC) }) Pageable pageable)
{
}
我的存储库界面:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface CountryRepository extends JpaRepository<Country, Long>, JpaSpecificationExecutor<Country> {
}
如果有动态查询,我使用:
org.springframework.data.jpa.domain.Specification<T>
spring 数据 jpa :
org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
org.springframework.data.jpa.repository.support.SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID>
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {
TypedQuery<T> query = getQuery(spec, pageable);
return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList())
: readPage(query, getDomainClass(), pageable, spec);
}
the repository CountryRepository calling the method findAll(@Nullable
Specification spec, Pageable pageable) returns the Page
我正在尝试为我的 table 用户实现搜索功能。我想制作一个 JpaRepository 方法,该方法将给我一个可分页的用户列表,这些用户的电子邮件包含给定的字符串,我该怎么做? 这是我试过的 存储库:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findByEmailContaining(String email, Pageable pageable);
}
发球:
public Page<User> findPaginatedEmail(int pageNo, int pageSize, String sortField, String sortDirection, String text) {
Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortField).ascending() :
Sort.by(sortField).descending();
Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort);
return this.userRepository.findByEmailContaining(text, pageable);
}
有时它会工作并填充我的 table 有时它不会。问题是什么?我该如何实施?
它工作正常,但一段时间后它就坏了,我不知道为什么,然后它找不到任何东西
您要找的是:
public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
List<Product> findAllByPrice(double price, Pageable pageable);
}
您只需添加一个额外的函数参数 Pageable 并且您还需要使用 PagingAndSortingRepository 而不是 JpaRepository
扩展您的 Repository class方法有很多种,如果这里使用了SimpleJpaRepository调用参数Specification和Pageable的findAll方法,返回Page接口。
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
@GetMapping(path = "/pageable")
public Page<?> getCountries(@RequestParam(name = "countryName", required = false) String countryName,
@PageableDefault(page = 0, size = 20) @SortDefault.SortDefaults({
@SortDefault(sort = "id", direction = Sort.Direction.ASC) }) Pageable pageable)
{
}
我的存储库界面:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface CountryRepository extends JpaRepository<Country, Long>, JpaSpecificationExecutor<Country> {
}
如果有动态查询,我使用:
org.springframework.data.jpa.domain.Specification<T>
spring 数据 jpa :
org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
org.springframework.data.jpa.repository.support.SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID>
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {
TypedQuery<T> query = getQuery(spec, pageable);
return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList())
: readPage(query, getDomainClass(), pageable, spec);
}
the repository CountryRepository calling the method findAll(@Nullable Specification spec, Pageable pageable) returns the Page