资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

在@Query注解注释的JPQL语句中写limit语句是会报错的

昌邑ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

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 getBaselineOverview() 
  { 
    return misakaService.getMisaka(); 
  } 
 
} 

service

import java.util.List; 
 
public interface MisakaService 
{ 
  List 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 getMisaka() 
  { 
    Pageable pageable = new PageRequest(1, 2, Direction.ASC, "name"); 
    Page misakaPage = misakaDao.search(pageable); 
    List 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 
{ 
  @Query("SELECT m FROM Misaka m WHERE m.id>4") 
  Page 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

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

输出

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条件(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持创新互联。


文章标题:在JPA的@Query注解中使用limit条件(详解)
网页路径:http://www.cdkjz.cn/article/gpjihj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220