关键字
方法命名
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的更多相关文章

  1. sqlServer 2008修改字段类型和重命名字段名称的sql语句

    sqlServer 2008修改字段类型和重命名字段名称的sql语句 //修改字段的类型 alter table fdi_news alter column c_author nvarchar(50) ...

  2. JPA命名规则

    jpa中方法的命名规则必须按照严格的要求来写.不能随便的命名方法名字,具体的方法操作如下. 参照方法地址:https://blog.csdn.net/csdnchen666666/article/de ...

  3. sql 语句大小写的问题

    关键字不区分大小写 例如 select ,from, 大小写均可 标识符区分大小写 例如 表名,列名 标识符如果不加双引号,默认是按大写执行 标识符如果加双引号,则是按原始大小写执行 但是,当表名加上 ...

  4. 打开黑盒:从 MySQL架构设计出发,看它是如何执行一条 SQL语句的

    1.把MySQL当个黑盒子一样执行SQL语句 我们的系统采用数据库连接池的方式去并发访问数据库,然后数据库自己其实也会维护一个连接池,其中管理了各种系统跟这台数据库服务器建立的所有连接 当我们的系统只 ...

  5. 通过JPA注解映射视图的实体类 jpa 视图 无主键 @Query注解的用法(Spring Data JPA) jpa 使用sql语句

    参考: https://blog.csdn.net/qq465235530/article/details/68064074 https://www.cnblogs.com/zj0208/p/6008 ...

  6. jpa数据库表实体命名规则 Unknown column 'user0_.create_time' in 'field list'

    数据库,表字段命名是驼峰命名法(createTime),Spring data jpa 在操作表的时候,生成的sql语句中却是create_time, 表字段不对照, Spring data jpa基 ...

  7. 【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' ...

  8. spring data jpa 使用方法命名规则查询

    按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写.框架在进行方法名解析时,会先把方法名多余的前缀 ...

  9. jpa自定义sql语句

    /** * 查询还没生成索引的帖子 * @return */ @Query(value = "SELECT * FROM t_article WHERE index_state=0" ...

随机推荐

  1. 初探Runloop(一)

    iOS 的最大特点就是运行时. 保证运行时的就是RunLoop 1.什么是RunLoop呢? 从字面理解就是:运行循环 引用下官方文档的介绍: A run loop is an event proce ...

  2. Web Service(一):初识

    1. 前言 cxf 在项目中应用好久了,一直没有写总结,现在补上. 由于cxf 属于Web Service的一个实现,所以先学习和总结一下Web Service,作为学习cxf的基础. 2. WebS ...

  3. 数据包注入重放工具aireplay-ng

    数据包注入重放工具aireplay-ng   aireplay-ng是aircrack-ng组件包的一个工具.它可以注入和重放数据帧,用于后期的WEP.WPA-PSK破解.它提供九种攻击模式,包括死亡 ...

  4. android 多线程的实现方式

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha. 1,通过创建线程池,来创建多个线程. 2,通过异步任务,来创建线程池,从而创建多线程. ...

  5. HDU 3802Ipad,IPhone

    前两块可以看成是不是二次剩余,快速幂计算即可. 后半部分可以看成x1=a+b+2ab,x2=a+b-2ab为特征方程x^2-px-qx=0的两根 然后可以通过韦达定理求出p和q,因此递推式为A(n+2 ...

  6. Message Queue协议AMQP

    历史: Message Queue的需求由来已久,80年代最早在金融交易中,高盛等公司采用Teknekron公司的产品,当时的Message queuing软件叫做:the information b ...

  7. .net core下的dotnet全局工具

    .net core 2.1后支持了一个全新的部署和扩展命令,可以自己注册全局命令行. dotnet install tool -g dotnetsaydotnetsay 也可以自己构建自己的命令行,一 ...

  8. DELPHI - How to use opendialog1 for choosing a folder? TOpenDialog, TFileOpenDialog

    DELPHI - How to use opendialog1 for choosing a folder? On Vista and up you can show a more modern lo ...

  9. 爬虫IP被禁的简单解决方法

    爬虫以前听上去好厉害好神秘的样子,用好了可以成就像Google.百度这样的索索引擎,用不好可以凭借不恰当的高并发分分钟崩掉一个小型网站.写到这里想到12306每年扛住的并发请求量,觉得好牛逼. 爬虫和 ...

  10. SimpleUpdater.NET

    本类库+工具用于快速实现一个简单的自动更新程序,旨在快速简单地为现有的.Net应用程序添加上比较简单的自动更新功能. 本页包含以下内容 概述 整个自动升级工作的流程 更新包生成工具 发布更新包 为应用 ...