jpa命名规则 jpa使用sql语句 @Query
关键字
方法命名
sql where字句
And
findByNameAndPwd
where name= ? and pwd =?
Or
findByNameOrSex
where name= ? or sex=?
Is,Equals
findById,findByIdEquals
where id= ?
Between
findByIdBetween
where id between ? and ?
LessThan
findByIdLessThan
where id < ?
LessThanEquals
findByIdLessThanEquals
where id <= ?
GreaterThan
findByIdGreaterThan
where id > ?
GreaterThanEquals
findByIdGreaterThanEquals
where id > = ?
After
findByIdAfter
where id > ?
Before
findByIdBefore
where id < ?
IsNull
findByNameIsNull
where name is null
isNotNull,NotNull
findByNameNotNull
where name is not null
Like
findByNameLike
where name like ?
NotLike
findByNameNotLike
where name not like ?
StartingWith
findByNameStartingWith
where name like '?%'
EndingWith
findByNameEndingWith
where name like '%?'
Containing
findByNameContaining
where name like '%?%'
OrderBy
findByIdOrderByXDesc
where id=? order by x desc
Not
findByNameNot
where name <> ?
In
findByIdIn(Collection<?> c)
where id in (?)
NotIn
findByIdNotIn(Collection<?> c)
where id not in (?)
True
findByAaaTue
where aaa = true
False
findByAaaFalse
where aaa = false
IgnoreCase
findByNameIgnoreCase
where UPPER(name)=UPPER(?)
//where name in(?,?...) and age<?
public List<Employee> findByNameInAndAgeLessThan(List<String> names,Integer age);
在方法上加@Query就不需要遵守上面的规则了,可以进行自定义sql。
有两种方式传参:
@Query("select o from Employee o where o.name=?1 and o.age=?2")
public List<Employee> queryParams1(String name,Integer age);
@Query("select o from Employee o where o.name=:name and o.age=:age")
public List<Employee> queryParams2(@Param("name") String name, @Param("age") Integer age);
修改数据的时候需要使用到三个注解
@Modifying
@Query("update Employee o set o.age=:age where o.id=:id")
public void update(@Param("id") Integer id,@Param("age") Integer age);
首先是query这里需要加上@Modifying
但是仅仅加上这个会报错,需要打开事务
所以还需要在service层方法上开启事务,(service层开启事务,事务会作用于整个查询部分的方法)
@Transactional
public void update(Integer id,Integer age){
employeeRepository.update(id,age);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/csdnchen666666/article/details/78153229
jpa命名规则 jpa使用sql语句 @Query的更多相关文章
- sqlServer 2008修改字段类型和重命名字段名称的sql语句
sqlServer 2008修改字段类型和重命名字段名称的sql语句 //修改字段的类型 alter table fdi_news alter column c_author nvarchar(50) ...
- JPA命名规则
jpa中方法的命名规则必须按照严格的要求来写.不能随便的命名方法名字,具体的方法操作如下. 参照方法地址:https://blog.csdn.net/csdnchen666666/article/de ...
- sql 语句大小写的问题
关键字不区分大小写 例如 select ,from, 大小写均可 标识符区分大小写 例如 表名,列名 标识符如果不加双引号,默认是按大写执行 标识符如果加双引号,则是按原始大小写执行 但是,当表名加上 ...
- 打开黑盒:从 MySQL架构设计出发,看它是如何执行一条 SQL语句的
1.把MySQL当个黑盒子一样执行SQL语句 我们的系统采用数据库连接池的方式去并发访问数据库,然后数据库自己其实也会维护一个连接池,其中管理了各种系统跟这台数据库服务器建立的所有连接 当我们的系统只 ...
- 通过JPA注解映射视图的实体类 jpa 视图 无主键 @Query注解的用法(Spring Data JPA) jpa 使用sql语句
参考: https://blog.csdn.net/qq465235530/article/details/68064074 https://www.cnblogs.com/zj0208/p/6008 ...
- jpa数据库表实体命名规则 Unknown column 'user0_.create_time' in 'field list'
数据库,表字段命名是驼峰命名法(createTime),Spring data jpa 在操作表的时候,生成的sql语句中却是create_time, 表字段不对照, Spring data jpa基 ...
- 【spring data jpa】使用jpa的@Query,自己写的语句,报错:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' cannot be found on null
报错: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' ...
- spring data jpa 使用方法命名规则查询
按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写.框架在进行方法名解析时,会先把方法名多余的前缀 ...
- jpa自定义sql语句
/** * 查询还没生成索引的帖子 * @return */ @Query(value = "SELECT * FROM t_article WHERE index_state=0" ...
随机推荐
- P2429 制杖题
P2429 制杖题这个题用线性筛会WA一个点,因为这个题是给定的质数集,最大的质数会比当前的倍数大,就会出现上面的情况.怎办?判重用set啊!set+线性筛就过掉了.16ms #include< ...
- mini-css-extract-plugin简介
将css单独打包成一个文件的插件,它为每个包含css的js文件都创建一个css文件.它支持css和sourceMaps的按需加载. 目前只有在webpack V4版本才支持使用该插件 和extract ...
- Codeforces-1077C
title: Codeforces-1077C date: 2018-11-24 15:22:31 tags: acm 刷题 categories: Codeforces 题意 题目链接 给你一个数组 ...
- 苹果Mac OS 显示隐藏文件
苹果Mac OS 操作系统下,隐藏文件默认为隐藏状态,隐藏文件是否显示有多种方法可以设置. 方法一: 打开终端,输入命令行 显示Mac隐藏文件的命令: defaults write com.apple ...
- Java 多线程 - synchronize 关键字
目录 Java 多线程 - synchronize 关键字 Java 多线程 - synchronize 关键字 学习自 http://cmsblogs.com/?p=2071 https://www ...
- Xamarin iOS教程之使用按钮接接收用户输入
Xamarin iOS教程之使用按钮接接收用户输入 Xamarin iOS使用按钮接接收用户输入 按钮是用户交互的最基础控件.即使是在iPhone或者iPad中,用户使用最多操作也是通过触摸实现点击. ...
- html5解决ajax破坏浏览器机制
pjax是一种基于ajax+history.pushState的新技术,该技术可以无刷新改变页面的内容,并且可以改变页面的URL.pjax是ajax+pushState的封装,同时支持本地存储.动画等 ...
- BZOJ.1901.Dynamic Rankings(树状数组套主席树(动态主席树))
题目链接 BZOJ 洛谷 区间第k小,我们可以想到主席树.然而这是静态的,怎么支持修改? 静态的主席树是利用前缀和+差分来求解的,那么对于每个位置上的每棵树看做一个点,拿树状数组更新. 还是树状数组的 ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- CF 222 (DIV 1)
A: 我是bfs出一颗树,然后删掉树后面的k个结点. 其实也可以直接bfs出一块连通的s - k个点,其余的.打X就可以了. 很水的题目. /* *************************** ...