最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JPA-JpaRepository方法命名語法說明

 更新時間:2021年11月23日 15:10:00   作者:北亮bl  
這篇文章主要介紹了JPA-JpaRepository方法命名語法說明,具有很好的參考價值。如有錯誤或未考慮完全的地方,望不吝賜教

前言

梳理了一遍JPA的方法命名語法,記錄一下,以便后續(xù)備查。

注:本文不介紹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相關,返回值是數(shù)組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不變,返回的數(shù)組也只有一條數(shù)據(jù)

下面這種定義,沒有意義,知曉一下就好:

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,刪除行數(shù)

@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);

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • java如何刪除以逗號隔開的字符串中某一個值

    java如何刪除以逗號隔開的字符串中某一個值

    這篇文章主要介紹了java如何刪除以逗號隔開的字符串中某一個值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • IDEA 2020.3 更新了機器學習都整上了

    IDEA 2020.3 更新了機器學習都整上了

    IDEA 歡迎窗口全新升級,首頁增加三個選項卡,一個用于設置 IDE 界面的 Customize,一個用于插件安裝的 Plugins,一個于訪問幫助和學習資源的 Learn IntelliJ IDEA,另外包括之前用于管理項目的 Projects,需要的朋友可以參考下
    2020-12-12
  • Java?Spring?事件監(jiān)聽詳情解析

    Java?Spring?事件監(jiān)聽詳情解析

    這篇文章主要介紹了Java?Spring?事件監(jiān)聽詳情解析,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • Java遞歸求和1+2+3+...+n實例詳解

    Java遞歸求和1+2+3+...+n實例詳解

    在本篇文章里小編給大家?guī)砹岁P于Java遞歸求和1+2+3+...+n實例內(nèi)容,需要的朋友們可以學習參考下。
    2020-01-01
  • spring cloud Ribbon用法及原理解析

    spring cloud Ribbon用法及原理解析

    這篇文章主要介紹了spring cloud Ribbon用法及原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • Spring Retry 重試實例詳解

    Spring Retry 重試實例詳解

    這篇文章主要介紹了Spring Retry 重試,使用方式有兩種分別是命令式和聲明式,本文通過實例代碼給大家詳細講解,需要的朋友可以參考下
    2022-10-10
  • flyway實現(xiàn)java 自動升級SQL腳本的問題及解決方法

    flyway實現(xiàn)java 自動升級SQL腳本的問題及解決方法

    大家在平時開發(fā)自己寫SQL語句忘記在所有環(huán)境執(zhí)行,需要新增環(huán)境做數(shù)據(jù)遷移,那么遇到這樣的問題該如何解決呢?本文通過場景分析給大家介紹java 自動升級SQL腳本的策略,感興趣的朋友一起看看吧
    2021-07-07
  • 淺談StringEntity 和 UrlEncodedFormEntity之間的區(qū)別

    淺談StringEntity 和 UrlEncodedFormEntity之間的區(qū)別

    這篇文章主要介紹了StringEntity 和 UrlEncodedFormEntity之間的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java中日期時間的用法總結(jié)

    Java中日期時間的用法總結(jié)

    在日常開發(fā)中,我們經(jīng)常需要處理日期和時間,所以這篇文章小編為大家總結(jié)了下?Java?中日期與時間的基本概念與一些常用的用法,希望對大家有所幫助
    2023-09-09
  • Java線程池實現(xiàn)原理總結(jié)

    Java線程池實現(xiàn)原理總結(jié)

    這篇文章主要給大家分享的是Java線程池實現(xiàn)原理總結(jié),線程池參數(shù)、線程池執(zhí)行流程等內(nèi)容上總結(jié),具有一定參考戒指,需要的小伙伴可以參考一下,希望對你有所幫助
    2022-01-01

最新評論

丘北县| 天气| 南开区| 通城县| 永仁县| 定南县| 玛纳斯县| 邹平县| 昭通市| 临城县| 忻城县| 全南县| 峨眉山市| 林芝县| 达尔| 冀州市| 泗阳县| 阿鲁科尔沁旗| 威信县| 深州市| 孙吴县| 茌平县| 通江县| 宜春市| 辽阳县| 鹿泉市| 图木舒克市| 铁岭市| 锦州市| 丹东市| 青铜峡市| 凤阳县| 行唐县| 彰化市| 阿荣旗| 五常市| 芦溪县| 湖口县| 独山县| 顺昌县| 敦化市|