我们已经探索了 spring data jpa 的基础知识以及方法命名约定如何使查询变得简单。如果没有,我强烈建议您先关注该博客。在第二部分中,我们将深入研究更高级的查询技术,使您能够利用强大的组合、排序、分页和特定于字符串的操作来更好地控制数据检索。
我们采用与 book 实体相同的示例,具有 bookname、authorname、isbn、price、publisheddate、published 属性。
spring data jpa 允许使用 and 和 or 关键字组合多个条件来创建更复杂的查询。
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';
list<book> findbybooknameorauthorname(string bookname, string authorname);
此方法返回 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);
以下是每个部分的含义:
pageable:spring jpa 提供了一个 pageable 接口,允许我们检索具有定义数量的记录的特定数据页。它支持分页和排序。
page<book> findbyauthorname(string authorname, pageable pageable);
此方法将返回一页书籍,按作者姓名过滤。
为了对数据进行分页和排序,我们在调用该方法时传递一个 pageable 对象。例如,如果我们想获取第二页,每页有 10 本书,按书名升序排列:
pageable pageable = pagerequest.of(1, 10, sort.by("bookname").ascending()); page<book> books = bookrepository.findbyauthorname("the lost hero", pageable);
pagerequest.of(1, 10, sort.by("the lost hero").ascending()):这将创建一个可分页对象,该对象请求第二页(页面索引从 0 开始),每页 10 本书,按书名升序排序。
它们用于比较数值、日期或其他可比较的数据类型。它们帮助我们根据特定的比较标准过滤数据。
list<book> findbypublisheddateafter(localdate date);
此方法对应的 sql 查询如下所示
select * from book where publisheddate > '2024-01-01';
list<book> findbypricebetween(double startprice, double endprice);
此方法对应的 sql 查询如下所示
select * from book where price between 10 and 50;
这些操作用于对字符串字段进行模式匹配和比较,类似于sql的like子句。
list<book> findbybooknamecontaining(string keyword);
如果搜索关键字值,此方法对应的 sql 查询将如下所示。
select * from book where bookname like '%hunted%';
list<book> findbybooknamestartingwith(string prefix);
如果前缀值为 the lost,则此方法对应的 sql 查询如下所示。
select * from book where bookname like 'the lost%';
list<book> findbybooknameendingwith(string suffix);
如果后缀值为 hero,则此方法对应的 sql 查询如下所示。
select * from book where bookname like '%hero';
spring jpa 提供了简单的关键字来直接查询布尔字段。
list<book> findbypublishedistrue(); list<book> findbypublishedisfalse();
此方法对应的 sql 查询如下所示
select * from book where published = true;
这些关键字用于检查字段是否与集合中的任何值匹配或不与集合中的任何值匹配。
list<book> findbybooknamein(list<string> names);
如果字符串名称列表是“the lost hero”、“alice in wonderland”,则此方法相应的 sql 查询如下所示。
select * from book where bookname in ('the lost hero', 'alice in wonderland');
list<book> findbybooknamenotin(list<string> names);
如果字符串名称列表是“the lost hero”、“alice in wonderland”,则此方法相应的 sql 查询如下所示。
select * from book where bookname not in ('the lost hero', 'alice in wonderland');
这些关键字用于限制返回的记录数。我们可以检索前 n 或前 n 个结果,通常与排序结合使用。
list<book> findtop3byorderbypublisheddatedesc();
select * from book order by publisheddate desc limit 3;
list<book> findfirst5byorderbybooknameasc();
SELECT * FROM Book ORDER BY bookName ASC LIMIT 5;
让我们以 findfirst5byorderbybooknameasc() 方法为例,并将其分解为不同的部分,以便于理解:
find:这是该方法的核心部分,它告诉 spring jpa 我们要从数据库中检索数据。
first5 / first
by:此关键字有助于将“查找”操作与我们要搜索的字段连接起来。
orderbybookname:这告诉 spring jpa 我们要根据 bookname 字段对结果进行排序。因此,在选择前 5 本书之前,它会按字母顺序(按名称)排列书籍。
asc:这代表“升序”顺序,意思是从 a 到 z(按字母顺序)排序。假设我们想要从 z 到 a 排序,我们将使用 desc(“降序”顺序)。
总之,此方法将在数据库中查找书籍,按名称升序(a 到 z)对它们进行排序,并返回满足此排序条件的前 5 条记录。
在第二部分中,我们探讨了 spring data jpa 如何允许我们通过方法命名约定创建更高级、更灵活的查询。结合条件、分页、排序、比较运算符和其他强大功能,无需编写 sql 即可构造复杂查询。这些约定遵循“约定优于配置”原则,节省了我们的时间,同时提供了巨大的灵活性。
当使用字符串、数字、日期、布尔值或集合时,spring data jpa 使查询我们的数据库变得更简单、更直观。 当我们的查询对于方法约定来说变得过于复杂时,我们始终可以使用 @query 注释来使用自定义查询。但对于大多数用例来说,这些约定就是我们有效处理查询所需的全部。
以上就是Spring Data JPA 中的高级查询技术的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号