欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring data jpa 创建方法名进行简单查询方式

程序员文章站 2024-03-27 12:45:28
spring data jpa 可以通过在接口中按照规定语法创建一个方法进行查询,spring data jpa 基础接口中,如crudrepository中findone,save,delete等,...

spring data jpa 可以通过在接口中按照规定语法创建一个方法进行查询,spring data jpa 基础接口中,如crudrepository中findone,save,delete等,那么我们自己怎么按照需要创建一个方法进行查询呢?

最常见的做法是

声明一个接口继承于crudrepository 或者 pagingandsortingrepository,jparepository,repository

或者利用注释的方式表名继承于jparepository,例如下面这俩种是等价的

继承crudrepository 或者 pagingandsortingrepository,jparepository会抽出一些常用的方法,如果你spring data jpa帮你自定义那么多方法,你可以继承于jparepository,然后复制一些方法到你的接口中,可以选择性的要一些方法

按照规范创建查询方法

一般按照java驼峰式书写规范加一些特定关键字,例如我们想通过任务名来获取任务实体类列表

利用属性获取任务列表

利用and 和 or来获取任务列表

利用pageable ,sort,slice获取分页的任务列表和排序

利用distinct去重

利用orderby进行排序

利用 top 和 first来获取限制数据

那么spring data jpa是怎么通过这些规范来进行组装成查询语句呢?

spring data jpa框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如 find、findby、read、readby、get、getby,然后对剩下部分进行解析。

假如创建如下的查询:findbytaskprojectname(),框架在解析该方法时,首先剔除 findby,然后对剩下的属性进行解析,假设查询实体为doc

  • 先判断 taskprojectname (根据 pojo 规范,首字母变为小写)是否为查询实体的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,继续第二步;
  • 从右往左截取第一个大写字母开头的字符串此处为name),然后检查剩下的字符串是否为查询实体的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,则重复第二步,继续从右往左截取;最后假设task为查询实体person的一个属性;
  • 接着处理剩下部分(projectname),先判断 task 所对应的类型是否有projectname属性,如果有,则表示该方法最终是根据 “ person.task.projectname”的取值进行查询;否则继续按照步骤 2 的规则从右往左截取,最终表示根据 “person.task.project.name” 的值进行查询。
  • 可能会存在一种特殊情况,比如 person包含一个 task 的属性,也有一个 projectname 属性,此时会存在混淆。可以明确在属性之间加上 “_” 以显式表达意图,比如 “findbytask_projectname()”

支持的规范表达式

这里以实体为user,有firstname和lastname,age

表达式 例子 hql查询语句
and findbylastnameandfirstname … where x.lastname = ?1 and x.firstname = ?2
or findbylastnameorfirstname … where x.lastname = ?1 or x.firstname = ?2
is,equals findbyfirstname,findbyfirstnameis,findbyfirstnameequal … where x.firstname = 1?
between findbystartdatebetween … where x.startdate between 1? and ?2
lessthan findbyagelessthan … where x.age < ?1
lessthanequal findbyagelessthanequal … where x.age ← ?1
greaterthan findbyagegreaterthan … where x.age > ?1
greaterthanequal findbyagegreaterthanequal … where x.age >= ?1
after findbystartdateafter … where x.startdate > ?1
before findbystartdatebefore … where x.startdate < ?1
isnull findbyageisnull … where x.age is null
isnotnull,notnull findbyage(is)notnull … where x.age not null
like findbyfirstnamelike … where x.firstname like ?1
notlike findbyfirstnamenotlike … where x.firstname not like ?1
startingwith findbyfirstnamestartingwith … where x.firstname like ?1 (parameter bound with appended %)
endingwith findbyfirstnameendingwith … where x.firstname like ?1 (parameter bound with prepended %)
containing findbyfirstnamecontaining … where x.firstname like ?1 (parameter bound wrapped in %)
orderby findbyageorderbylastnamedesc … where x.age = ?1 order by x.lastname desc
not findbylastnamenot … where x.lastname <> ?1
in findbyagein(collection ages) … where x.age in ?1
notin findbyagenotin(collection age) … where x.age not in ?1
true findbyactivetrue() … where x.active = true
false findbyactivefalse() … where x.active = false
ignorecase findbyfirstnameignorecase … where upper(x.firstame) = upper(?1)

发现这些查询都是只针对单表进行查询,如果是多表的复杂查询,还有分页该怎么查,下次再研究看看…

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。