jparepository查询所有_JPA – JpaRepository 中使用的查询方法

默认方法

User user=new User();

userRepository.findAll();

userRepository.findOne(1l);

userRepository.save(user);

userRepository.delete(user);

userRepository.count();

userRepository.exists(1l);

自定义查询

User findByUserName(String userName);

也使用一些加一些关键字And、 Or

User findByUserNameOrEmail(String username, String email);

修改、删除、统计也是类似语法

Long deleteById(Long id);

Long countByUserName(String userName)

基本上 SQL 体系中的关键词都可以使用,例如:LIKE、 IgnoreCase、 OrderBy。

ListfindByEmailLike(String email);

User findByUserNameIgnoreCase(String userName);

ListfindByUserNameOrderByEmailDesc(String email);

Query 自定义查询

@Query 书写时,数据库名和字段名要写成代码中的大小写格式,而不是数据库中的字段格式

使用 nativeQuery ,数据库名和字段名要写成数据库中的字段大小写

一、本地语法直接查询(Native SQL Query)

@Query(value = "select * from Book b where b.name=?1", nativeQuery = true)

List findByName(String name);

二、模糊查询

方法1:使用 “%”

Repository

List findByNameLike(String name);

Controller

teamRepository.findByNameLike("%"+name+"%");

方法2:使用 Query 自定义 sql 查询

Repository

@Query(value = "select t from Team t where t.name like %?1%")

List findByNameLike(String name);

Controller

teamRepository.findByNameLike(name);

三、范围查询

@Query(value = "select name,author,price from Book b where b.price>?1 and b.price2")

List findByPriceRange(long price1, long price2);

四、@Param注解注入参数

@Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")

List findByNamedParam(@Param("name") String name, @Param("author") String author, @Param("price") long price);

五、分页查询

Page findALL(Pageable pageable);

Page findByUserName(String userName,Pageable pageable);

在查询的方法中,需要传入参数Pageable ,当查询中有多个参数的时候Pageable建议做为最后一个参数传入 。

Pageable 是 Spring 封装的分页实现类,使用的时候需要传入页数、每页条数和排序规则。

int page=1,size=10; Sort sort = new Sort(Direction.DESC, "id"); Pageable pageable = new PageRequest(page, size, sort); userRepository.findALL(pageable); userRepository.findByUserName("testName", pageable);

六、限制查询

查询前N个元素

User findFirstByOrderByLastnameAsc();

User findTopByOrderByAgeDesc();

Page queryFirst10ByLastname(String lastname, Pageable pageable);

List findFirst10ByLastname(String lastname, Sort sort);

List findTop10ByLastname(String lastname, Pageable pageable);

七、多表查询

@Query("select h.city as city, h.name as name, avg(r.rating) as averageRating " - "from Hotel h left outer join h.reviews r where h.city = ?1 group by h")

Page findByCity(City city, Pageable pageable);

@Query("select h.name as name, avg(r.rating) as averageRating " - "from Hotel h left outer join h.reviews r group by h")

Page findByCity(Pageable pageable);

基础语法

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

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

Is, EqualsfindByFirstname,findByFirstnameIs,findByFirstnameEquals… where x.firstname = ?1

BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2

LessThanfindByAgeLessThan… where x.age < ?1

LessThanEqualfindByAgeLessThanEqual… where x.age <= ?1

GreaterThanfindByAgeGreaterThan… where x.age > ?1

GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1

AfterfindByStartDateAfter… where x.startDate > ?1

BeforefindByStartDateBefore… where x.startDate < ?1

IsNull, NullfindByAge(Is)Null… where x.age is null

IsNotNull, NotNullfindByAge(Is)NotNull… where x.age not null

LikefindByFirstnameLike… where x.firstname like ?1

NotLikefindByFirstnameNotLike… where x.firstname not like ?1

StartingWithfindByFirstnameStartingWith… where x.firstname like ?1 (parameter bound with appended %)

EndingWithfindByFirstnameEndingWith… where x.firstname like ?1 (parameter bound with prepended %)

ContainingfindByFirstnameContaining… where x.firstname like ?1 (parameter bound wrapped in %)

OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc

NotfindByLastnameNot… where x.lastname <> ?1

InfindByAgeIn(Collection ages)… where x.age in ?1

NotInfindByAgeNotIn(Collection ages)… where x.age not in ?1

TruefindByActiveTrue()… where x.active = true

FalsefindByActiveFalse()… where x.active = false

IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstame) = UPPER(?1)

参考:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/265888.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring jdbc 对象Mapper的简单封装

一般查询实体的时候&#xff0c;都需要这么使用/** * 根据id查询 * * return */ public Emp queryEmpById(Integer id) { String sql "select * from emp where empno ?"; ParameterizedRowMapper<Emp> mappe…

网络编程知识预备(1) ——了解OSI网络模型

参考&#xff1a;简单了解OSI网络模型 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-03-18 20:07:09 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/114968802?spm1001.2014.3001.5502 根据需求摘抄自下面这篇文章&#xff0c;内容非常详…

完全内核移植–kernel3.15.3

目标平台 明远智睿 EK314 CPU I.MAX6Q内核版本 3.15.3编译器 arm-none-linux-gnueabi-gcc-4.8.3 1。下载纯净内核 2。配置Makefile 3。拷贝cp imx_v6_v7_defconfig .config 4。# make menuconfig 退出保存 5。#make zImage 进行第一次编译&#xff0c;看能否通过 6。修改…

antd表格显示分页怎么取消_真相!Word里怎么也删不掉的文档空白页原来是这样...

大家好&#xff0c;我是你们的海宝老师在Word里&#xff0c;经常会遇到这种情况&#xff1a;文档莫名其妙地多出一个或多个空白页&#xff0c;没啥内容却怎么也删不掉。不着急&#xff0c;咱们来一一分析。1、标题前的空白页像这种标题前面有空白&#xff0c;基本就是【段落】设…

ubuntu 搜狗安装搜狗输入法(fcitx)亲测有用

安装搜狗拼音输入法网上很多方法&#xff0c;总结了一下&#xff0c;下面的方法绝对可以&#xff0c;第一步&#xff1a;Ubuntu 默认是安装了ibus.所以删除它sudo apt-get remove ibus对于已经安装老版本的fcitx,删掉再装.sudo apt-get remove fcitx*删除依赖库sudo apt-get au…

网络编程知识预备(2) ——三次握手与四次挥手、流量控制(滑动窗口)、拥塞控制、半连接状态、2MSL

参考&#xff1a;浅显易懂的三次握手与四次挥手 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-03-19 09:33:20 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/114990198?spm1001.2014.3001.5502 参考&#xff1a;&#xff08;四十七&…

面向对象2

python之路——面向对象进阶 阅读目录 isinstance和issubclass反射setattr  delattr  getattr  hasattr__str__和__repr____del__item系列__getitem__  __setitem__  __delitem____new____call____len____hash____eq__回到顶部isinstance和issubclass isinstance(obj…

linux命令deploy_linux命令:du 命令

Linux du命令也是查看使用空间的&#xff0c;但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看&#xff0c;还是和df命令有一些区别的.1&#xff0e;命令格式&#xff1a;du [选项][文件]2&#xff0e;命令功能&#xff1a;显示每个文件和目录的磁盘使用空…

DM9000网卡原理与基地址设置

从上面可以看出DM9000的地址总线就一根&#xff0c;它不像CS8900那样地址总线和数据总线都齐全。而这里只有一根地址线(CMD)&#xff0c;16跟数据线&#xff0c;所以可以确定位宽为16位&#xff0c;而地址线为什么只有一根&#xff0c;这是DM9000决定的&#xff0c;看手册可以知…

采购订单接收备注为必输项

应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name RCV_RCVRCERC 表单名 Form Name RCVRCERC 说明 Description 采购订单接收备注为必输项 条件 Condition 触发器事件 Tirgger Event WHEN-NEW-FORM-INSTANCE 触发器对象 Tirgger Object…

网络编程知识预备(3) ——SOCKET、TCP、HTTP之间的区别与联系

参考&#xff1a;SOCKET,TCP,HTTP之间的区别与联系 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-03-19 11:54:01 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/114992140?spm1001.2014.3001.5502 参考&#xff1a;TCP连接、Http连接与S…

debian 9 安装后的配置,debian 9 开发环境。

注意&#xff1a;以下命令用sudo或者以root用户进行 一.Xterm&#xff08;在安装KDE桌面情况下&#xff09;的配置&#xff08;可以黏贴&#xff0c;复制&#xff09;&#xff1a; 首先在根目录下编辑文件.Xresources(没有可以创建) rootdebian:~# vim ~/.Xresources rootdebi…

mysql 左连接 怎么走索引_mysql left join查询没走索引

SELECTt0.ID as id,t0.NAME as name,t0.PHONE as phone,t0.CITY_CODE as cityCode,t0.SHOOTING_TIME as shootingTime,t0.REMARK as remark,t0.SOURCE_FROM as sourceFrom,-- 平台来源t0.REFER as refer,t0.UPDATE_TIME as updateTime,CONCAT(IFNULL(t0.SHOOTING_NAME,),t1.SHO…

嵌入式RT3070 AP模式移植

环境&#xff1a;ubuntu1~14.04.3 编译器&#xff1a;arm-none-linux-gnueabi-gcc-4.8.3 无线网卡为RT3070&#xff0c;驱动分为STA驱动和SoftAP驱动两种&#xff0c;STA驱动支持无线网卡工作在STA模式下&#xff0c;而SoftAP的驱动支持无线网卡工作在软AP的模式下&#xff0…

Wireshark抓包介绍和TCP三次握手分析

wireshark介绍 wireshark的官方下载网站&#xff1a; http://www.wireshark.org/ wireshark是非常流行的网络封包分析软件&#xff0c;功能十分强大。可以截取各种网络封包&#xff0c;显示网络封包的详细信息。 wireshark是开源软件&#xff0c;可以放心使用。 可以运行在Wind…

网络编程知识预备(4) ——了解应用层的HTTP协议与HTTPS协议

参考&#xff1a;简单了解HTTP协议与HTTPS协议 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-03-15 10:55:13 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/114807328?spm1001.2014.3001.5502 编程实现人脸识别需要基于人工智能平台&…

Django之orm查询

ORM相关 MVC或者MVC框架中包括一个重要的部分&#xff0c;就是ORM&#xff0c;它实现了数据模型与数据库的解耦&#xff0c;即数据模型的设计不需要依赖于特定的数据库&#xff0c;通过简单的配置就可以轻松更换数据库&#xff0c;这极大的减轻了开发人员的工作量&#xff0c;不…

qq显示下线通知什么意思_最近时不时地收到QQ下线的通知

话说&#xff0c;姐妹们&#xff0c;你们两口子之间&#xff0c;有没有秘密的啊&#xff1f;换句话说&#xff0c;你们之间的所有密码&#xff0c;是否都共享的呢&#xff1f;其实吧&#xff0c;我们两个人之间&#xff0c;倒是没有什么秘密的&#xff0c;我们两个人的密码都是…

thttpd移植

1.官网下载http://www.acme.com/software/thttpd/ 版本thttpd-2.25b.tar.gz 2.解压 tar -zxvf thttpd-2.25b.tar.gz ./configure 4.修改makefile CC arm-none-linux-gnueabi-gcc 总共有3个makefile要改 压缩根目录包1个,cgi-src目录1个,extras目录1个 5.make 6. 拷…

有关HL7 的C# 源码

<?xml version"1.0" encoding"UTF-8"?> https://github.com/OSEHRA/mdo C# http://sourceforge.net/p/nhapi/code/HEAD/tree/NHapi20/ C#转载于:https://blog.51cto.com/muzizongheng/1333001