SpringDataJPA的介绍以及基础用法
本篇内容介绍了“Spring Data JPA的介绍以及基础用法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
10余年的敖汉网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整敖汉建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“敖汉网站设计”,“敖汉网站推广”以来,每个客户项目都认真落实执行。
在昨天我们介绍了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 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 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); Listlist = 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的介绍以及基础用法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
标题名称:SpringDataJPA的介绍以及基础用法
链接地址:http://scyanting.com/article/pdehih.html