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
- Spring data jpa/Spring boot jpa方法命名规则
- PHP 面向对象编程和设计模式 (5/5) - PHP 命名空间的使用及名称解析规则
- spring JPA相关命名规则
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- Python 中特殊变量/方法命名规则说明(特别是私有变量)及使用实例
- Java学习笔记摘录(标识符/变量是什么/如何命名变量/数据类型/变量的使用规则)
- 变量的命名和使用规则
- python 学习记录(5)-变量、模块名的命名规则及random模块使用
- 类方法命名规则-要减少使用时思考
- 网页制作中规范使用DIV+CSS命名规则,可以改善优化功效特别是团队合作时候可以提供合作制作效率,具体DIV CSS命名规则CSS命名大全内容如下:
- Directive的命名和使用规则(js中驼峰,html中变-)
- Python 中特殊变量/方法命名规则说明(特别是私有变量)及使用实例
- 我使用的android 资源文件命名规则
- Yii: URL管理规则中命名参数使用注意点
- Name Convension -- Grub脚本中使用的命名规则
- 我的命名规则在使用NHibernate.Tool.hbm2ddl遇到麻烦
- Spring data jpa 方法命名规则
- 学会如何使用LESS(二)----嵌套规则、运算、命名空间和作用域
- [Tool] 使用StyleCop验证命名规则
- 黑马程序员_C#基础知识学习笔记:变量的使用和命名规则