关键字
方法命名
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. QT5:使用QFile类读写文本乱码

    我用的是QT5的QFile类实现的

  2. scrollIntoView将指定元素定位到浏览器顶部,底部,中间

    var element = document.getElementById("box"); element.scrollIntoView();//顶部 element.scroll ...

  3. kotlin下载地址收藏

    由于android studio 不同版本对应的kotlin版本是不同的,现在收藏 kotlin下载地址     https://plugins.jetbrains.com/plugin/6954-k ...

  4. 028.Zabbix常见故障

    一 中文乱码处理 1.1 现象 1.2 解决方法 将任意一中文字体上传至/usr/share/zabbix/fonts,如微软雅黑. vi /usr/share/zabbix/include/defi ...

  5. 两种思想实现基于jquery的延时导航菜单,可做延时触发器!

    1. 函数式 html如下: <div class="box"> <ul class="clear-fix"> <li class ...

  6. ubuntu 配置mycat

    https://blog.csdn.net/leisure_life/article/details/78611594 这篇博主写的非常好,我找了很久 都解决不了,最后按照他的步骤解决了问题. 其中有 ...

  7. 谷歌浏览器测试工具应用Advanced REST client

    1.2 http post 在URL栏输入http://httpbin.org/post,选在”POST”类型,在”Payload”栏输入data,最后设置”Content-Type”

  8. CentOS下使用autoenv实现进入特定目录后运行特定环境变量

    安装 # Mac git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv # 安装默认shell echo 'source ~/. ...

  9. android.os.handler(转)

    android.os.handler相关知识整理 Handler在android里负责发送和处理消息.它的主要用途有: 1)按计划发送消息或执行某个Runnanble(使用POST方法): 2)从其他 ...

  10. 用Docker下搭建GitLab

    最近试了一下Docker,发现用它搭建服务十分方便,就用它搭建了一个gitlab练练手. 首先下载gitlab镜像:         docker image pull gitlab/gitlab-c ...