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

在@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

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]]




评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值