【问题标题】:Spring Boot JPA Like querySpring Boot JPA Like 查询
【发布时间】:2021-10-13 21:51:33
【问题描述】:

我正在尝试为我的用户表实现搜索功能。我想创建一个 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);
}

有时它可以工作并填充我的表格,有时则不能。问题是什么?如何实现?

它工作正常,但一段时间后它就坏了,我不知道为什么,然后它就找不到任何东西了

【问题讨论】:

  • 现在,我发现了问题...该方法有效,但我在不同的页面(比如第 7 页)而不是第 1/2 页,每次我搜索它都会返回我一页或两页...但是在第 7 页上,我看到的是没有结果。

标签: spring spring-boot jpa spring-data-jpa


【解决方案1】:

方法有很多,这里用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>

弹簧数据 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);
    }

存储库 CountryRepository 调用方法 findAll(@Nullable Specification spec, Pageable pageable) 返回页面

【讨论】:

    【解决方案2】:

    您正在寻找的是:

    public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
    
        List<Product> findAllByPrice(double price, Pageable pageable);
    }
    

    您只需添加一个额外的函数参数 Pageable 并且您需要使用 PagingAndSortingRepository 而不是 JpaRepository 扩展您的 Repository 类

    Pagination example

    【讨论】:

      猜你喜欢
      • 2021-08-21
      • 2019-05-29
      • 2021-09-15
      • 2022-01-24
      • 1970-01-01
      • 2017-04-01
      • 2021-04-16
      • 2020-03-29
      • 1970-01-01
      相关资源
      最近更新 更多