时间:2021-05-19
在@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
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条件(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
使用JPA中@Query注解实现update操作,代码如下:@Transactional@Modifying(clearAutomatically=true)@
本文介绍了SpringDataJPA系列之投影(Projection)的用法,分享给大家在JPA的查询中,有一个不方便的地方,@Query注解,如果查询直接是S
我们在使用SpringDataJPA框架时,进行条件查询,如果是固定条件的查询,我们可以使用符合框架规则的自定义方法以及@Query注解实现。如果是查询条件是动
可将如下语句query_cache_size=268435456query_cache_type=1query_cache_limit=1048576存放到/e
$limit){$query="select`id`from`news`orderby`id`desclimit".$limit;$result=mysql_q