欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在JPA的@Query注解中使用limit条件(详解)

程序员文章站 2023-12-21 10:15:46
在@query注解注释的jpql语句中写limit语句是会报错的 unexpected token :limit near line .... 解决方法是讲@query...

在@query注解注释的jpql语句中写limit语句是会报错的

unexpected token :limit near line ....

解决方法是讲@query注解中的limit语句去掉,然后传一个pageable pageable=new pagerequest(offset,limit)进去

示例代码:

controller

import java.util.list; 
 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.restcontroller; 
 
@restcontroller 
@requestmapping(value = "/misaka") 
public class misakacontroller 
{ 
  @autowired 
  private misakaservice misakaservice; 
 
  @requestmapping(value = "/list") 
  public list<misaka> getbaselineoverview() 
  { 
    return misakaservice.getmisaka(); 
  } 
 
} 

service

import java.util.list; 
 
public interface misakaservice 
{ 
  list<misaka> getmisaka(); 
} 

serviceimpl

import java.util.list; 
 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.data.domain.page; 
import org.springframework.data.domain.pagerequest; 
import org.springframework.data.domain.pageable; 
import org.springframework.data.domain.sort.direction; 
import org.springframework.stereotype.service; 
 
@service 
public class misakaserviceimpl implements misakaservice 
{ 
  @autowired 
  private misakadao misakadao; 
 
  @override 
  public list<misaka> getmisaka() 
  { 
    pageable pageable = new pagerequest(1, 2, direction.asc, "name"); 
    page<misaka> misakapage = misakadao.search(pageable); 
    list<misaka> misakalist = misakapage.getcontent(); 
    system.out.println(misakalist); 
    return misakalist; 
  } 
} 

dao

import org.springframework.data.domain.page; 
import org.springframework.data.domain.pageable; 
import org.springframework.data.jpa.repository.query; 
import org.springframework.data.repository.crudrepository; 
 
public interface misakadao extends crudrepository<misaka, long> 
{ 
  @query("select m from misaka m where m.id>4") 
  page<misaka> search(pageable pageable); 
} 

model

import javax.persistence.column; 
import javax.persistence.entity; 
import javax.persistence.generatedvalue; 
import javax.persistence.generationtype; 
import javax.persistence.id; 
import javax.persistence.table; 
 
@entity 
@table(name = "t_test") 
public class misaka 
{ 
  @id 
  @generatedvalue(strategy = generationtype.auto) 
  private long id; 
 
  @column(name = "name") 
  private string name; 
 
  public long getid() 
  { 
    return id; 
  } 
 
  public void setid(long id) 
  { 
    this.id = id; 
  } 
 
  public string getname() 
  { 
    return name; 
  } 
 
  public void setname(string name) 
  { 
    this.name = name; 
  } 
 
  @override 
  public string tostring() 
  { 
    return "misaka [id=" + id + ", name=" + name + "]"; 
  } 
 
} 

数据库t_test

id name
1 m1
2 m2
3 m3
4 m4
5 m5
6 m6
7 m7
8 m8
9 m9

输出

hibernate: select count(misaka0_.id) as col_0_0_ from t_test misaka0_ where misaka0_.id>4 
hibernate: select misaka0_.id as id1_29_, misaka0_.name as name2_29_ from t_test misaka0_ where misaka0_.id>4 order by misaka0_.name asc limit ?, ? 
[misaka [id=7, name=m7], misaka [id=8, name=m8]] 

以上这篇在jpa的@query注解中使用limit条件(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: