在JPA的@Query注解中使用limit條件(詳解)
更新時間:2017年06月06日 13:26:39 投稿:jingxian
下面小編就為大家?guī)硪黄贘PA的@Query注解中使用limit條件(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
在@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條件(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring MVC獲取參數(shù)和自定義參數(shù)類型轉換器及編碼過濾器
這篇文章主要為大家詳細介紹了Spring MVC獲取參數(shù)和自定義參數(shù)類型轉換器及編碼過濾器,文中通過代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-06-06

