您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“Spring Data JPA的介绍以及基础用法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
在昨天我们介绍了Spring Data JPA,以及基础使用,今天我们讲一下分页。
首先,我们修改RaindropUserRepository类
import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import run.halo.app.model.entity.*; import java.util.List; public interface RaindropUserRepository extends JpaRepository<RaindropUser, Integer> { RaindropUser findByUsername(String userName); RaindropUser findBySexAndEmail(String sex, String email); //修改方法————添加Pageable类 @Query("select new run.halo.app.model.entity.RaindropUser(raindrop.id," + "raindrop.username,raindrop.email,raindrop.sex," + "raindrop.age) from RaindropUser raindrop where raindrop.age < ?1") List<RaindropUser> findByLtAge(Integer age, Pageable pageable); // @Query("select new run.halo.app.model.entity.RaindropUser(a.id,a.username,a.email,a.sex,a.age) from spring_data_jpa a where age > ?1") // RaindropUser findByRtAge(Integer age); }
然后我们修改测试方法:
@Test public void Test(){ // 设置页码为0,每页显示数量15 int page=0,size=15; // 设置排序方式 以id列降序排列,升序为Sort.Direction.ASC Sort sort = new Sort(Sort.Direction.DESC, "id"); // 设置Pageable变量 Pageable pageable = PageRequest.of(page, size, sort); List<RaindropUser> list = raindropUserRepository.findByLtAge(15, pageable); System.out.println("List: " + list.get(0)); }
查看控制台打印信息:
Hibernate: select raindropus0_.id as col_0_0_, raindropus0_.username as col_1_0_, raindropus0_.email as col_2_0_, raindropus0_.sex as col_3_0_, raindropus0_.age as col_4_0_ from springdatajpa raindropus0_ where raindropus0_.age<? order by raindropus0_.id desc limit ? List: RaindropUser(id=1, username=Raindrop, email=123123@gmail.com, sex=man, age=12) Hibernate: select raindropus0_.id as id1_12_, raindropus0_.age as age2_12_, raindropus0_.email as email3_12_, raindropus0_.sex as sex4_12_, raindropus0_.username as username5_12_ from springdatajpa raindropus0_ where raindropus0_.sex=? and raindropus0_.email=? Hibernate: select raindropus0_.id as id1_12_, raindropus0_.age as age2_12_, raindropus0_.email as email3_12_, raindropus0_.sex as sex4_12_, raindropus0_.username as username5_12_ from springdatajpa raindropus0_ where raindropus0_.username=?
“Spring Data JPA的介绍以及基础用法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。