Spring Data JPA 中的高级查询技术(高级.查询.技术.Spring.Data...)

wufei1232024-10-06java17

我们已经探索了 spring data jpa 的基础知识以及方法命名约定如何使查询变得简单。如果没有,我强烈建议您先关注该博客。在第二部分中,我们将深入研究更高级的查询技术,使您能够利用强大的组合、排序、分页和特定于字符串的操作来更好地控制数据检索。

我们采用与 book 实体相同的示例,具有 bookname、authorname、isbn、price、publisheddate、published 属性。

Spring Data JPA 中的高级查询技术

将条件与关键字相结合

spring data jpa 允许使用 and 和 or 关键字组合多个条件来创建更复杂的查询。

  • and:使用 and 运算符组合条件。

book findbybooknameandauthorname(string bookname, string authorname);


上面的方法将执行一个查询,从数据库中检索一本书,其中 bookname 和authorname 都与方法参数中提供的值匹配。

如果我们搜索书名,即书名,“the great book”,作者是“a. qwerty”。如果存在这样的书,它将返回该记录。否则,它将返回 null。

该方法将执行的等效 sql 查询


select * from book
where bookname = 'the great book' 
and authorname = 'a. qwerty';


  • 或:使用 or 运算符组合条件。 方法名称中的 or 运算符允许我们检索至少满足一个条件的结果。

list<book> findbybooknameorauthorname(string bookname, string authorname);


</book>

此方法返回 book 对象的列表,其中 bookname 或authorname 与指定参数匹配。它不需要两个条件都为真。如果一本书与名称或作者匹配,它将包含在结果列表中。

此方法对应的 sql 查询如下所示


select * from book
where bookname = 'the great book'
or authorname = 'albert hero';


此查询将返回标题为“the great gatsby”或作者为“george orwell”的所有书籍。如果一本书符合任一条件,它将成为结果集的一部分。

排序和分页
  • 排序:这使我们能够控制返回记录的顺序。

orderby:我们可以在方法名称中使用 orderby 来指定排序。例如,如果我们想要按名称升序(a 到 z)排序书籍,我们可以使用:


list<book> findbyauthornameorderbybooknameasc(string authorname);


</book>

以下是每个部分的含义:

  1. findbyauthorname:根据作者姓名检索书籍。
  2. orderbybooknameasc:按书籍名称升序(a 到 z)对书籍进行排序。 如果我们想按降序排序(z 到 a),我们可以将 asc 替换为 desc。
  • 分页:这有助于我们将大量数据分解为较小的块或“页面”,以更有效地加载。这在处理长记录列表(例如产品、搜索结果或博客文章)时非常有用。

pageable:spring jpa 提供了一个 pageable 接口,允许我们检索具有定义数量的记录的特定数据页。它支持分页和排序。


page<book> findbyauthorname(string authorname, pageable pageable);


</book>

此方法将返回一页书籍,按作者姓名过滤。

如何使用分页:

为了对数据进行分页和排序,我们在调用该方法时传递一个 pageable 对象。例如,如果我们想获取第二页,每页有 10 本书,按书名升序排列:


pageable pageable = pagerequest.of(1, 10, sort.by("bookname").ascending());

page<book> books = bookrepository.findbyauthorname("the lost hero", pageable);


</book>

pagerequest.of(1, 10, sort.by("the lost hero").ascending()):这将创建一个可分页对象,该对象请求第二页(页面索引从 0 开始),每页 10 本书,按书名升序排序。

比较关键词

它们用于比较数值、日期或其他可比较的数据类型。它们帮助我们根据特定的比较标准过滤数据。

  • isbefore / isafter:这些用于比较日期或时间字段。

list<book> findbypublisheddateafter(localdate date);


</book>

此方法对应的 sql 查询如下所示


select * from book where publisheddate &gt; '2024-01-01';


  • lessthan / greaterthan / between:这些用于数字比较。

list<book> findbypricebetween(double startprice, double endprice);


</book>

此方法对应的 sql 查询如下所示


select * from book where price between 10 and 50;


字符串特定操作

这些操作用于对字符串字段进行模式匹配和比较,类似于sql的like子句。

  • 包含:用于执行 like %hunted% 操作,即查找部分匹配

list<book> findbybooknamecontaining(string keyword);


</book>

如果搜索关键字值,此方法对应的 sql 查询将如下所示。


select * from book where bookname like '%hunted%';


  • startingwith:用于检查字符串字段是否以特定前缀开头。

list<book> findbybooknamestartingwith(string prefix);


</book>

如果前缀值为 the lost,则此方法对应的 sql 查询如下所示。


select * from book where bookname like 'the lost%';


  • endingwith:用于检查字符串字段是否以特定后缀结尾。

list<book> findbybooknameendingwith(string suffix);


</book>

如果后缀值为 hero,则此方法对应的 sql 查询如下所示。


select * from book where bookname like '%hero';


布尔字段

spring jpa 提供了简单的关键字来直接查询布尔字段。

  • istrue / isfalse:用于查询值为 true 或 false 的布尔字段。

list<book> findbypublishedistrue();
list<book> findbypublishedisfalse();


</book></book>

此方法对应的 sql 查询如下所示


select * from book where published = true;


在和不在

这些关键字用于检查字段是否与集合中的任何值匹配或不与集合中的任何值匹配。

  • in:用于查找字段值包含在值集合中的实体。

list<book> findbybooknamein(list<string> names);


</string></book>

如果字符串名称列表是“the lost hero”、“alice in wonderland”,则此方法相应的 sql 查询如下所示。


select * from book where bookname in ('the lost hero', 'alice in wonderland');


  • notin:用于查找字段值未包含在值集合中的实体。与 in 操作相反。

list<book> findbybooknamenotin(list<string> names);


</string></book>

如果字符串名称列表是“the lost hero”、“alice in wonderland”,则此方法相应的 sql 查询如下所示。


select * from book where bookname not in ('the lost hero', 'alice in wonderland');


顶部和第一

这些关键字用于限制返回的记录数。我们可以检索前 n 或前 n 个结果,通常与排序结合使用。

  • top:用于从结果集中检索前 n 条记录。

list<book> findtop3byorderbypublisheddatedesc();


</book>

select * from book order by publisheddate desc limit 3;


  • 第一:用于从结果集中检索前 n 条记录,通常按特定字段排序。

list<book> findfirst5byorderbybooknameasc();


</book>

SELECT * FROM Book ORDER BY bookName ASC LIMIT 5;


让我们以 findfirst5byorderbybooknameasc() 方法为例,并将其分解为不同的部分,以便于理解:

  1. find:这是该方法的核心部分,它告诉 spring jpa 我们要从数据库中检索数据。

  2. first5 / first:这部分方法指定我们要获取前 5 条记录。我们可以根据选择或要求将 5 替换为任何数字。当我们不需要所有结果,只需要少量结果时,这很有用。

  3. by:此关键字有助于将“查找”操作与我们要搜索的字段连接起来。

  4. orderbybookname:这告诉 spring jpa 我们要根据 bookname 字段对结果进行排序。因此,在选择前 5 本书之前,它会按字母顺序(按名称)排列书籍。

  5. asc:这代表“升序”顺序,意思是从 a 到 z(按字母顺序)排序。假设我们想要从 z 到 a 排序,我们将使用 desc(“降序”顺序)。

总之,此方法将在数据库中查找书籍,按名称升序(a 到 z)对它们进行排序,并返回满足此排序条件的前 5 条记录。

结论

在第二部分中,我们探讨了 spring data jpa 如何允许我们通过方法命名约定创建更高级、更灵活的查询。结合条件、分页、排序、比较运算符和其他强大功能,无需编写 sql 即可构造复杂查询。这些约定遵循“约定优于配置”原则,节省了我们的时间,同时提供了巨大的灵活性。

当使用字符串、数字、日期、布尔值或集合时,spring data jpa 使查询我们的数据库变得更简单、更直观。 当我们的查询对于方法约定来说变得过于复杂时,我们始终可以使用 @query 注释来使用自定义查询。但对于大多数用例来说,这些约定就是我们有效处理查询所需的全部。

以上就是Spring Data JPA 中的高级查询技术的详细内容,更多请关注知识资源分享宝库其它相关文章!

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。