Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and basic usage of Spring Data JPA

2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "introduction and basic usage of Spring Data JPA". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Yesterday we introduced Spring Data JPA, as well as basic use, today we talk about paging.

First, we modify the RaindropUserRepository class

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) / / modify method-add Pageable class @ 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);}

Then we modify the test method:

@ Test public void Test () {/ / set page number to 0, number of displays per page 15 int page=0,size=15;//, sort by id column in descending order, ascending order as Sort.Direction.ASC Sort sort = new Sort (Sort.Direction.DESC, "id"); / / set Pageable variable Pageable pageable = PageRequest.of (page, size, sort); List list = raindropUserRepository.findByLtAge (15, pageable) System.out.println ("List:" + list.get (0));}

View console print information:

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 303

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report