服务器之家

服务器之家 > 正文

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

时间:2020-11-10 16:28     来源/作者:Java之家

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

unexpected token :limit near line ....

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

示例代码:

controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

?
1
2
3
4
5
6
import java.util.List;
 
public interface MisakaService
{
  List<Misaka> getMisaka();
}

serviceimpl

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

?
1
2
3
4
5
6
7
8
9
10
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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

 

输出

?
1
2
3
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条件(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部