Query creation

Generally the query creation mechanism for JPA works as described in Query methods. Here’s a short example of what a JPA query method translates into:

Example 43. Query creation from method names
public interface UserRepository extends Repository<User, Long> {    List<User> findByEmailAddressAndLastname(String emailAddress, String lastname); }

We will create a query using the JPA criteria API from this but essentially this translates into the following query:select u from User u where u.emailAddress = ?1 and u.lastname = ?2. Spring Data JPA will do a property check and traverse nested properties as described in Property expressions. Here’s an overview of the keywords supported for JPA and what a method containing that keyword essentially translates to.

Table 4. Supported keywords inside method names
Keyword Sample JPQL snippet

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstname,findByFirstnameIs,findByFirstnameEquals

… 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<Age> ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection<Age> 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)

JPA query 基本语法解释的更多相关文章

  1. 20191217-关于JPA @Query查询数据一直为空,直接在数据库里执行SQL则可以查出来

    20191217-关于JPA @Query查询数据一直为空,直接在数据库里执行SQL则可以查出来 前提:数据库中查询,由于在视图中无主键概念,只是在代码中由逻辑主键.结果:数据中作为逻辑主键中有个字段 ...

  2. 【Spring Data 系列学习】Spring Data JPA @Query 注解查询

    [Spring Data 系列学习]Spring Data JPA @Query 注解查询 前面的章节讲述了 Spring Data Jpa 通过声明式对数据库进行操作,上手速度快简单易操作.但同时 ...

  3. spring data jpa @query的用法

    @Query注解的用法(Spring Data JPA) 参考文章:http://www.tuicool.com/articles/jQJBNv . 一个使用@Query注解的简单例子 @Query( ...

  4. JPA query between的多种方式(mongodb为例)

    背景 JPA+MongoDB查询,给定一段时间范围查询分页结果,要求时间范围包含. Page<Log> findByCtimeBetweenOrderByCtime( LocalDateT ...

  5. jpa @Query()参数设置,:冒号方式、?NO.问号方式、实体类对象参数设置

    一.service层事务(update/delete) @Transactional(rollbackFor = Exception.class) 二.@Query()参数设置 ?x  和:XX不能混 ...

  6. spring jpa @Query中使用in

    @Modifying @Query("delete from SmTenant s where s.id in ?1") void deleteByIds(List<Long ...

  7. JSON.stringify 语法解释

    行为:此函数的作用主要是串行化对象. 或许有些人是过敏的字系列.我非常理解easy.是对象的类型转换成字符串类型(或者更确切的说是json类型的).就这么简单.打个例如说,你有一个类,那么你能够通过这 ...

  8. MySql最土的语法解释使用一。

    create database namedb charset utf8;解释:创建一个数据库 namedb改成你的数据库名字,charset是字符集的意思 utf8代表数据库支持中文字符集.必须分号结 ...

  9. Python的方法和语法解释

    ---------------------------------------------------------------------------------------------------- ...

随机推荐

  1. 【转】iOS8 推送 获取 devicetoken

    标签:推送 push ios8 devicetoken token xcode6 原文:http://roybaby.blog.51cto.com/1508945/1557854 打开AppDeleg ...

  2. APNs改动 (转)

    对 APNs 的吐槽 APNs 是 Apple Push Notification service 的简称(注意 APNs 的大小写, s不需要大写). 以下是我收集的一些关于 APNs 的吐槽,你先 ...

  3. [转]dos命令 cd命令使用说明[图文说明]

    Cddir(change directory,可以缩写为cd),其功能是显示当前目录的名称,或更改当前的目录. 应用时公带一个驱动器号(如: cd c:)在命令行cmd中输入 cd /? 可显示帮助信 ...

  4. Android界面布局学习总结

    参考文章: http://blog.csdn.net/shakespeare001/article/details/7843460 http://www.cnblogs.com/w-y-f/p/412 ...

  5. xv6中存储cpu和进程信息的技巧

    xv6是一个支持多处理器的Unix-like操作系统, 近日阅读源码时发现xv6在记录当前CPU和进程状态时非常tricky 首先,上代码: extern struct cpu cpus[NCPU]; ...

  6. HTML5 Canvas画数字时钟

    先不说废话,没代码算个蛋. 一些地方注释都写得比较清楚,不过这只是部分,因为只有秒针,但是时针,分针的逻辑都是一致的. 代码中有些坐标不知道为什么较不准,看看就好

  7. php流行笔试题及答案

    1.在PHP中,当前脚本的名称(不包括路径和查询字符串)记录在预定义变量(1)中:而链接到当前页面的URL记录在预定义变量(2)中. 答:echo $_SERVER['PHP_SELF']; echo ...

  8. ubuntu 设置root启动

    http://www.linuxidc.com/Linux/2012-05/60806.htm sudo -s 输入普通用户的password 回车即可进入root权限 vi /etc/lightdm ...

  9. Unity3d开发IOS游戏 基础

    Unity3d开发IOS游戏 基础 @阿龙 -  649998群 1.先说明两个问题,我在WIN7下面的U3D里面,用了雅黑字体,但是导出为ios后,字体就看不见了,这是为什么呢?这是需要在MAC下找 ...

  10. cf B Red and Blue Balls

    思路:把字符串转化为一个二进制,遇到B就是一个数二进制中的1,答案就是这个数. #include <cstdio> #include <cstring> #include &l ...