Java spring数据jpa@query和pageable

Java spring数据jpa@query和pageable,java,spring,hibernate,jpa,spring-data-jpa,Java,Spring,Hibernate,Jpa,Spring Data Jpa,我使用的是Spring数据JPA,当我使用@Query来定义一个查询,而不使用可分页的,它可以工作: public interface UrnMappingRepository extends JpaRepository<UrnMapping, Long> { @Query(value = "select * from internal_uddi where urn like %?1% or contact like %?1%", nativeQuer

我使用的是Spring数据JPA,当我使用
@Query
来定义一个查询,而不使用
可分页的
,它可以工作:

public interface UrnMappingRepository extends JpaRepository<UrnMapping, Long> {
    @Query(value = "select * from internal_uddi where urn like %?1% or contact like %?1%", 
           nativeQuery = true)
    List<UrnMapping> fullTextSearch(String text);
}

将查询重写为:

select iu from internal_uddi iu where iu.urn.... 
描述:

类似的问题是,有人指出,要应用分页,必须派生第二个子查询。因为子查询引用的是相同的字段,所以需要确保查询使用它所引用的实体/表的别名。这意味着,在你写的地方:

从内部uddi中选择*
你应该:

从内部uddi iu中选择*其中iu.urn类似于。。。

考虑到
UrnMapping
类被映射到
内部uddi
表,我建议:

@存储库
公共接口UrnMappingRepository扩展了JpaRepository{
@查询(value=“从UrnMapping iu中选择iu,其中iu.urn like%:text%或iu.contact like%:text%”)
页面全文搜索(@Param(“text”)字符串文本,可分页;
}

请注意,您可能必须关闭带有动态请求的本机查询。

您可以对本机查询使用分页。这里有记录:

“但是,您可以通过自己指定计数查询来使用本机查询进行分页: 示例59.使用@query在查询方法处声明本机计数查询以进行分页

public interface UserRepository扩展了JpaRepository{
@查询(value=“SELECT*FROM USERS,其中LASTNAME=?1”,
countQuery=“从LASTNAME=?1的用户中选择count(*),
nativeQuery=true)
PageFindBylastName(字符串lastname,可分页);
}
如果您使用的是Spring Data JPA 2.0.4及更高版本,请参考:。样本如下:

@Query(value = "SELECT u FROM User u ORDER BY id")
Page<User> findAllUsersWithPagination(Pageable pageable);
@Query(value=“按id从用户u订单中选择u”)
分页FindAllUsers(可分页);

我发现它在不同的jpa版本中工作不一样,为了调试,你最好添加这个配置来显示生成的sql,这样会节省你很多时间

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
对于spring boot 2.1.6.RELEASE,它运行良好

Sort sort = new Sort(Sort.Direction.DESC, "column_name");
int pageNumber = 3, pageSize = 5;
Pageable pageable = PageRequest.of(pageNumber - 1, pageSize, sort);
@Query(value=“从完整性评分视图中选择*+
“其中(?1为空或数据时间>=?1)”+
“和(?2为空或数据时间=?1)”+

“对于@Query(?2为null或data_hour,我们也可以在需要在JPA方法末尾传递Pageable类的对象时使用分页

例如:

Pageable pageableRequest = new PageRequest(page, size, Sort.Direction.DESC, rollNo);
在哪里,, 页面=页面索引(索引从零开始)
大小=记录数
Sort.Direction=根据rollNo进行排序
rollNo=用户类中的字段

userrepo
回购协议findByFirstname(“John”,pageableRequest);
公共接口用户存储库扩展了JpaRepository{
@查询(value=“SELECT*FROM USER WHERE FIRSTNAME=:FIRSTNAME)
Page findByLastname(@Param(“firstname”)字符串firstname,可分页;
}

使用@query在查询方法中声明本机计数查询以进行分页

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
  countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
  nativeQuery = true)
  Page<User> findByLastname(String lastname, Pageable pageable);

}
public interface UserRepository扩展了JpaRepository{
@查询(value=“SELECT*FROM USERS,其中LASTNAME=?1”,
countQuery=“从LASTNAME=?1的用户中选择count(*),
nativeQuery=true)
PageFindBylastName(字符串lastname,可分页);
}
希望这有帮助


我尝试了上述所有解决方案,但都没有成功,最后我从分页中删除了排序,它成功了

我也遇到了同样的问题-没有
Pageable
方法很好。
当添加为方法参数时-不起作用

在玩了DB控制台和本机查询支持之后,我们决定使用该方法。但是,仅适用于大写字母
我的应用程序的逻辑是,实体的所有
名称
都从大写字母开始

稍微玩玩一下。发现方法名处的
IgnoreCase
发挥了“魔力”,下面是有效的解决方案:

public interface EmployeeRepository 
                            extends PagingAndSortingRepository<Employee, Integer> {

    Page<Employee> findAllByNameIgnoreCaseStartsWith(String name, Pageable pageable);

}

下面的教程帮助了我 ->

此时,4.3.Spring数据是2.0.4之前的JPA版本

添加
\n--#可分页\n
没有这个,我错了

此外,分页设置必须是无顺序的

PageRequest paginaConf = new PageRequest ((param1 - 1)
                                                 , param2);
最后转换页面

Page list=myQueryofRepo();
List lstreurn=myConversor(List.getContent());
Page ret=new PageImpl(lstreurn,pageConf,param2);

使用具有(nativeQuery=true)的nativeQuery时,您可以通过添加(限制:sizeValue偏移量:第页)在查询中自己进行分页

注: 传递给此方法的页值应为offset*size

范例

 @Query(value = "SELECT * FROM person " +    
                   "LIMIT ?1 OFFSET ?2", nativeQuery = true)
    Optional<List<TDriverJob>> findPersons(int size, int page);
@Query(value=“SELECT*FROM person”+
“限制?1偏移?2”,nativeQuery=true)
可选findPersons(整型大小,整型页面);

不幸的是,这不适用于本机查询。您知道是否可以使用本机查询实现吗?如果您想编写本机查询,那么您显然需要自己编写查询来进行分页。
countQuery
也适用于非本机查询,因为Spring无法从原始查询中导出元素数al查询。在Spring数据JPA文档中向下滚动至。
public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
  countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
  nativeQuery = true)
  Page<User> findByLastname(String lastname, Pageable pageable);

}
public interface EmployeeRepository 
                            extends PagingAndSortingRepository<Employee, Integer> {

    Page<Employee> findAllByNameIgnoreCaseStartsWith(String name, Pageable pageable);

}
@Data
@Entity
@Table(name = "tblEmployees")
public class Employee {

    @Id
    @Column(name = "empID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotEmpty
    @Size(min = 2, max = 20)
    @Column(name = "empName", length = 25)
    private String name;

    @Column(name = "empActive")
    private Boolean active;

    @ManyToOne
    @JoinColumn(name = "emp_dpID")
    private Department department;
}
PageRequest paginaConf = new PageRequest ((param1 - 1)
                                                 , param2);
  Page <Object []> list = myQueryofRepo ();
         List <XXXModel> lstReturn = myConversor (list.getContent ());
         Page <XXXModel> ret = new PageImpl <XXXModel> (lstReturn, pageConf, param2);
 @Query(value = "SELECT * FROM person " +    
                   "LIMIT ?1 OFFSET ?2", nativeQuery = true)
    Optional<List<TDriverJob>> findPersons(int size, int page);