JPA-JpaRepository方法命名语法说明
前言
梳理了一遍jpa的方法命名语法,记录一下,以便后续备查。
注:本文不介绍jpl语法,版本为spring-data-jpa-2.3.0.release。
假设实体类名为 aaa,且定义如下:
import lombok.data; import javax.persistence.entity; import javax.persistence.id; @entity @data public class aaa { @id private long id; private long restid; private int dishhour; private int num; }
对应的仓储层接口定义:
import org.springframework.data.jpa.repository.jparepository; import org.springframework.stereotype.repository; import javax.transaction.transactional; import java.util.list; @repository public interface aaarepository extends jparepository<aaa, long> { int countbydishhourandrestid(int hour, long restid); boolean existsbydishhourandrestid(int hour, long restid); list<aaa> findbydishhourandrestid(int hour, long restid); aaa findtopbydishhourandrestid(int hour, long restid); @transactional int deletebydishhourandrestid(int hour, long restid); }
jpa的语法分为如下5种:
1、count相关,返回值为int 或 long
int countbydishhourandrestid(int hour, long restid); int countaaabydishhourandrestid(int hour, long restid); int countaaasbydishhourandrestid(int hour, long restid); int countallbydishhourandrestid(int hour, long restid);
上面这4个方法是一样的,对应的sql如下:
select count(id) from aaa where dishhour=? and restid=?
下面这种定义,没有意义,知晓一下就好:
int countdistinctbydishhourandrestid(int hour, long restid);
对应sql如下,如果表中有主键,功能跟countby是一致的,浪费性能:
select distinct count(distinct id) from aaa where dishhour=? and restid=?
2、exists相关,返回值只能是 boolean
boolean existsbydishhourandrestid(int hour, long restid); boolean existsaaabydishhourandrestid(int hour, long restid); boolean existsaaasbydishhourandrestid(int hour, long restid); boolean existsallbydishhourandrestid(int hour, long restid);
上面这4个方法是一样的,对应的sql如下:
select id from aaa where dishhour=? and restid=? limit 1
下面这种定义,没有意义,知晓一下就好:
boolean existsdistinctbydishhourandrestid(int hour, long restid);
对应sql如下,功能跟existsby是一致的,多余:
select distinct id from aaa where dishhour=? and restid=? limit 1
3、find相关,返回值是数组list<aaa>
list<aaa> findbydishhourandrestid(int hour, long restid); list<aaa> findaaabydishhourandrestid(int hour, long restid); list<aaa> findaaasbydishhourandrestid(int hour, long restid); list<aaa> findallbydishhourandrestid(int hour, long restid); list<aaa> getbydishhourandrestid(int hour, long restid); list<aaa> getaaabydishhourandrestid(int hour, long restid); list<aaa> getaaasbydishhourandrestid(int hour, long restid); list<aaa> getallbydishhourandrestid(int hour, long restid); list<aaa> querybydishhourandrestid(int hour, long restid); list<aaa> queryaaabydishhourandrestid(int hour, long restid); list<aaa> queryaaasbydishhourandrestid(int hour, long restid); list<aaa> queryallbydishhourandrestid(int hour, long restid); list<aaa> readbydishhourandrestid(int hour, long restid); list<aaa> readaaabydishhourandrestid(int hour, long restid); list<aaa> readaaasbydishhourandrestid(int hour, long restid); list<aaa> readallbydishhourandrestid(int hour, long restid); list<aaa> streambydishhourandrestid(int hour, long restid); list<aaa> streamaaabydishhourandrestid(int hour, long restid); list<aaa> streamaaasbydishhourandrestid(int hour, long restid); list<aaa> streamallbydishhourandrestid(int hour, long restid);
上面这20个方法是一样的,对应的sql如下:
select id,dishhour,num,restid from aaa where dishhour=? and restid=?
下面这种定义,没有意义,知晓一下就好:
list<aaa> finddistinctbydishhourandrestid(int hour, long restid);
对应sql如下,如果表中有主键,功能跟findby是一致的,多余:
select distinct id,dishhour,num,restid from aaa where dishhour=? and restid=?
4、findfirst相关,返回值是aaa
aaa findfirstbydishhourandrestid(int hour, long restid); aaa findtopbydishhourandrestid(int a, long b); aaa getfirstbydishhourandrestid(int hour, long restid); aaa gettopbydishhourandrestid(int a, long b); aaa queryfirstbydishhourandrestid(int hour, long restid); aaa querytopbydishhourandrestid(int a, long b); aaa readfirstbydishhourandrestid(int hour, long restid); aaa readtopbydishhourandrestid(int a, long b); aaa streamfirstbydishhourandrestid(int hour, long restid); aaa streamtopbydishhourandrestid(int a, long b);
上面这10个方法是一样的,对应的sql如下:
select id,dishhour,num,restid from aaa where dishhour=? and restid=? limit 1
注:返回值也可以改成list<aaa>,但是sql不变,返回的数组也只有一条数据
下面这种定义,没有意义,知晓一下就好:
list<aaa> finddistinctfirstbydishhourandrestid(int hour, long restid);
对应sql如下,如果表中有主键,功能跟countby是一致的,多余:
select distinct id,dishhour,num,restid from aaa where dishhour=? and restid=? limit 1
5、delete相关,返回值是int,删除行数
@transactional int deleteaaabydishhourandrestid(int a, long b); @transactional int deleteaaasbydishhourandrestid(int a, long b); @transactional int deleteallbydishhourandrestid(int a, long b); @transactional int deletebydishhourandrestid(int a, long b); @transactional int removeaaabydishhourandrestid(int a, long b); @transactional int removeaaasbydishhourandrestid(int a, long b); @transactional int removeallbydishhourandrestid(int a, long b); @transactional int removebydishhourandrestid(int a, long b);
上面这8个方法是一样的,对应有2条sql,如下:
select id,dishhour,num,restid from aaa where dishhour=? and restid=? delete from aaa where id=?
注:先select查找主键,再进行删除,所以必须在方法前加注解transactional,提供事务,否则会抛异常。
下面这种定义,没有意义,知晓一下就好:
int deletedistinctbydishhourandrestid(int hour, long restid);
对应sql如下,如果表中有主键,功能跟deleteby是一致的,多余:
select distinct id,dishhour,num,restid from aaa where dishhour=? and restid=?
注1:方法by后面的语法,可以参考下图,或官方文档:
注2:jpa query注解问题:
sql里可以用 #{#entityname} 占位符,替代手写表名,如:
@query(value = "select * from #{#entityname} where 1=2", nativequery = true) aaa selectxxx();
insert、update、delete这3种dml操作,返回值只能是void、int、long,且必须增加2个注解,例如:
// 返回值不是void、int、long,报错: // modifying queries can only use void or int/integer as return type! // 不加 transactional 报错: // javax.persistence.transactionrequiredexception: executing an update/delete query @transactional // 不加modifing 报错: // can not issue data manipulation statements with executequery(). @modifying @query(value = "update #{#entityname} set num=num+1 where id=6", nativequery = true) int doupdate();
注3:jpa原生方法列表:
list<t> findall(); list<t> findall(sort var1); list<t> findallbyid(iterable<id> var1); <s extends t> list<s> saveall(iterable<s> var1); void flush(); <s extends t> s saveandflush(s var1); void deleteinbatch(iterable<t> var1); void deleteallinbatch(); t getone(id var1); <s extends t> list<s> findall(example<s> var1); <s extends t> list<s> findall(example<s> var1, sort var2);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
上一篇: LRU算法在Vue内置组件keep-alive中的使用
下一篇: 人工智能对阅读场景的重塑