sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

JPA-JpaRepository方法命名语法说明 和对应执行的SQL 表名字占位符 #{#entityName}
http://news.558idc.com/148964.html

目录 前言 JPA的语法分为如下5种: 1、count相关,返回值为int 或 long 2、exists相关,返回值只能是 boolean 3、find相关,返回值是数组Listaaa 4、findFirst相关,返回值是aaa 5、delete相关,返回值
目录
  • 前言
  • JPA的语法分为如下5种:
    • 1、count相关,返回值为int 或 long
    • 2、exists相关,返回值只能是 boolean
    • 3、find相关,返回值是数组List<aaa>
    • 4、findFirst相关,返回值是aaa
    • 5、delete相关,返回值是int,删除行数

前言

梳理了一遍JPA的方法命名语法,记录一下,以便后续备查。

注:本文不介绍JPL语法,版本为spring-data-jpa-2.3.0.RELEASE。

假设实体类名为 aaa,且定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
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;
}

对应的仓储层接口定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
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

1
2
3
4
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如下:

1
select count(id) from aaa where dishHour=? and restId=?

下面这种定义,没有意义,知晓一下就好:

1
int countDistinctByDishHourAndRestId(int hour, long restId);

对应SQL如下,如果表中有主键,功能跟countBy是一致的,浪费性能:

1
select distinct count(distinct id) from aaa where dishHour=? and restId=?

2、exists相关,返回值只能是 boolean

1
2
3
4
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如下:

1
select id from aaa where dishHour=? and restId=? limit 1

下面这种定义,没有意义,知晓一下就好:

1
boolean existsDistinctByDishHourAndRestId(int hour, long restId);

对应SQL如下,功能跟existsBy是一致的,多余:

1
select distinct id from aaa where dishHour=? and restId=? limit 1

3、find相关,返回值是数组List<aaa>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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如下:

1
select id,dishHour,num,restId from aaa where dishHour=? and restId=?

下面这种定义,没有意义,知晓一下就好:

1
List<aaa> findDistinctByDishHourAndRestId(int hour, long restId);

对应SQL如下,如果表中有主键,功能跟findBy是一致的,多余:

1
select distinct id,dishHour,num,restId from aaa where dishHour=? and restId=?

4、findFirst相关,返回值是aaa

1
2
3
4
5
6
7
8
9
10
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如下:

1
select id,dishHour,num,restId from aaa where dishHour=? and restId=? limit 1

注:返回值也可以改成List<aaa>,但是SQL不变,返回的数组也只有一条数据

下面这种定义,没有意义,知晓一下就好:

1
List<aaa> findDistinctFirstByDishHourAndRestId(int hour, long restId);

对应SQL如下,如果表中有主键,功能跟countBy是一致的,多余:

1
select distinct id,dishHour,num,restId from aaa where dishHour=? and restId=? limit 1

5、delete相关,返回值是int,删除行数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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,如下:

1
2
select id,dishHour,num,restId from aaa where dishHour=? and restId=?
delete from aaa where id=?

注:先SELECT查找主键,再进行删除,所以必须在方法前加注解Transactional,提供事务,否则会抛异常。

下面这种定义,没有意义,知晓一下就好:

1
int deleteDistinctByDishHourAndRestId(int hour, long restId);

对应SQL如下,如果表中有主键,功能跟deleteBy是一致的,多余:

1
select distinct id,dishHour,num,restId from aaa where dishHour=? and restId=?

注1:方法By后面的语法,可以参考下图,或官方文档

在这里插入图片描述

注2:JPA Query注解问题:

SQL里可以用 #{#entityName} 占位符,替代手写表名,如:

1
2
@Query(value = "select * from #{#entityName} where 1=2", nativeQuery = true)
aaa selectXXX();

INSERT、UPDATE、DELETE这3种DML操作,返回值只能是void、int、long,且必须增加2个注解,例如:

1
2
3
4
5
6
7
8
9
10
// 返回值不是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原生方法列表:

1
2
3
4
5
6
7
8
9
10
11
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);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

posted on   sunny123456  阅读(765)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-01-11 encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。
2021-01-11 SQL 语句
2021-01-11 $("p").fadeOut("fast")设置淡出效果
2021-01-11 神奇的 toLocaleString
2021-01-11 获得当前时间的毫秒级别datetime.now.tostring(yyyyMMddHHmmssfff)
2021-01-11 浏览器 显示一个对话框,对话框中包含一条文字信息,用来提示用户输入文字。window.prompt()
2021-01-11 JS字符串转换为JSON对象的四种方法
点击右上角即可分享
微信分享提示