溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在JPA的@Query注解中使用limit條件(詳解)

發(fā)布時間:2020-08-28 06:10:16 來源:腳本之家 閱讀:543 作者:jingxian 欄目:編程語言

在@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 + "]"; 
  } 
 
} 

數(shù)據(jù)庫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條件(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI