jpa常用骚操作

8 篇文章 0 订阅
2 篇文章 0 订阅

1.基于方法名字查询

方法名和参数名需要遵守一定的规则, Spring Data JP A 才能自动转化为JPQL:
方法名通常包含多个实体属性用于查询,属性之间可以使用M叫D 和OR 连接,也支持
Between 、LessThan 、GreaterThan 、Like:
方法名可以以自ndBy 、getBy 、queryBy 开头:
查询结果可以排序,方法名包含OrderBy+属性+ASC(DESC):
可以通过Top 、First 来限定查询结果集:

一些特殊的参数可以出现在参数列表里,比如Pageable 、sort

以下是一些例子:
//根据名字查询,且按照名字升序
List<Person> findByLastnameOrderByFirstnameAsc(String lastname) ;
//根据名字查询,且使用翻页查询
Page <User > findByLastname(String lastname , Pageable pageable) ;
//查询满足条件的前10 个用户
List<User > findFirstlOByLastname(String lastname , Sort sort);
//使用And 联合查询
List<Person> findByLastnameAndFirstname(String lastname, String firstname);
I I 1.使用用Or 查询
List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname ,
String firstname) ;
//使用like 查询, name 必须包含like 中的毛或者?
public User findByNameLike(String name) ;

下表是常用的Spring Data 支持的关键字。
关键字例子转化的JPQL 片段
And findByLastnameAndFirstname ... where x.lastname =? I and x.firstname = ?2
Or findByLastnameOrFirstname ... where x.lastname =? l or x.firstname = ?2
Between findByStartDateBetween where x.startDate between ? l and ?2
Less Than findBy AgeLessThan where x.age <?I
LessThanEqual findBy AgeLessThanEqual where x.age <= ? I
GreaterThan findBy AgeGreaterThan ... where x.age > ? 1
GreaterThanEqual findBy AgeGreaterThanEqual . . . where x.age >= ? 1
After findByStartDateAfter .. . where x.startDate >?I
Before findByStartDateBefore where x.startDate <? I
IsNull findByAgelsNull ... where x.age is null
IsNotNull ,NotNu ll findByAge(Is)NotNull ... where x.age not null
OrderBy findByAgeOrderByLastnameDesc ... where x.age =? I order by x.lastname desc
Not findByLastnameNot .. where x.lastname <>? I
In findByAgeln(Collection ages) . . where x.age in ? I
Notln findByAgeNotln(Collection age) ... where x.age not in ?I
True findByActiveTrueO . . where x.active = true

False fin dB y Ac ti veF alse() .. where x.active = fal se
IgnoreCase findByFirstnamelgnore Case where UPPER(x.fir stname) = UPPER(? !)
Like findByFirstnameLike … where x. fir stname like ? l
NotLike findByFirstnameNotLike . . . where x. fir stname not like ? I

 

 

注意: S pring D ata 的Qu e ry 构造既适合J PA , 也适合其他S prin g Data
支持的N o SO L 。在大部分Sprin g Boot 应用中, Query 构造都只能创建一些简单的查询。
但对于N oSQL 来说已经足够了,不需要自己再构造NoSQ L 查询。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值