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

Spring Data JPA 之 JpaRepository的使用

 更新時(shí)間:2022年02月23日 10:43:12   作者:西瓜游俠  
這篇文章主要介紹了Spring Data JPA 之 JpaRepository的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
  • SpringBoot版本:2.3.2.RELEASE
  • SpringBoot Data JPA版本:2.3.2.RELEASE

JpaRepository是SpringBoot Data JPA提供的非常強(qiáng)大的基礎(chǔ)接口。

1 JpaRepository

1.1 JpaRepository接口定義

JpaRepository接口的官方定義如下:

@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T>

可以看出JpaRepository繼承了接口PagingAndSortingRepository和QueryByExampleExecutor。而PagingAndSortingRepository又繼承CrudRepository。因此,JpaRepository接口同時(shí)擁有了基本CRUD功能以及分頁(yè)功能。

當(dāng)我們需要定義自己的Repository接口的時(shí)候,我們可以直接繼承JpaRepository,從而獲得SpringBoot Data JPA為我們內(nèi)置的多種基本數(shù)據(jù)操作方法,例如:

public interface UserRepository extends JpaRepository<User, Integer> {
}

1.2 內(nèi)置方法

1.2.1 CrudRepository<T, ID>提供的方法

    /**
	 * 保存一個(gè)實(shí)體。
	 */
	<S extends T> S save(S entity);
	/**
	 * 保存提供的所有實(shí)體。
	 */
	<S extends T> Iterable<S> saveAll(Iterable<S> entities);
	/**
	 * 根據(jù)id查詢對(duì)應(yīng)的實(shí)體。
	 */
	Optional<T> findById(ID id);
	/**
	 * 根據(jù)id查詢對(duì)應(yīng)的實(shí)體是否存在。
	 */
	boolean existsById(ID id);
	/**
	 * 查詢所有的實(shí)體。
	 */
	Iterable<T> findAll();
	/**
	 * 根據(jù)給定的id集合查詢所有對(duì)應(yīng)的實(shí)體,返回實(shí)體集合。
	 */
	Iterable<T> findAllById(Iterable<ID> ids);
	/**
	 * 統(tǒng)計(jì)現(xiàn)存實(shí)體的個(gè)數(shù)。
	 */
	long count();
	/**
	 * 根據(jù)id刪除對(duì)應(yīng)的實(shí)體。
	 */
	void deleteById(ID id);
	/**
	 * 刪除給定的實(shí)體。
	 */
	void delete(T entity);
	/**
	 * 刪除給定的實(shí)體集合。
	 */
	void deleteAll(Iterable<? extends T> entities);
	/**
	 * 刪除所有的實(shí)體。
	 */
	void deleteAll();

1.2.2 PagingAndSortingRepository<T, ID>提供的方法

    /**
	 * 返回所有的實(shí)體,根據(jù)Sort參數(shù)提供的規(guī)則排序。
	 */
	Iterable<T> findAll(Sort sort);
	/**
	 * 返回一頁(yè)實(shí)體,根據(jù)Pageable參數(shù)提供的規(guī)則進(jìn)行過(guò)濾。
	 */
	Page<T> findAll(Pageable pageable);

1.2.3 JpaRepository<T, ID>提供的方法

    /**
	 * 將所有未決的更改刷新到數(shù)據(jù)庫(kù)。
	 */
	void flush();
	/**
	 * 保存一個(gè)實(shí)體并立即將更改刷新到數(shù)據(jù)庫(kù)。
	 */
	<S extends T> S saveAndFlush(S entity);
	/**
	 * 在一個(gè)批次中刪除給定的實(shí)體集合,這意味著將產(chǎn)生一條單獨(dú)的Query。
	 */
	void deleteInBatch(Iterable<T> entities);
	/**
	 * 在一個(gè)批次中刪除所有的實(shí)體。
	 */
	void deleteAllInBatch();
	/**
	 * 根據(jù)給定的id標(biāo)識(shí)符,返回對(duì)應(yīng)實(shí)體的引用。
	 */
	T getOne(ID id);

JpaRepository<T, ID>還繼承了一個(gè)QueryByExampleExecutor<T>,提供按“實(shí)例”查詢的功能。

2 方法測(cè)試

下面對(duì)以上提供的所有內(nèi)置方法進(jìn)行測(cè)試,給出各方法的用法。

首先定義實(shí)體類Customer:

package com.tao.springboot.hibernate.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Table(name = "tb_customer")
@Data
@NoArgsConstructor
@RequiredArgsConstructor
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false)
    private Long id;
    @Column(nullable = false)
    private String name;
    
    @Column(nullable = false)
    private Integer age;
}

然后定義接口CustomerRepository:

package com.tao.springboot.hibernate.repository;
import com.tao.springboot.hibernate.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
    
}

接下來(lái)對(duì)各個(gè)方法進(jìn)行測(cè)試~

2.1 save

    /**
	 * 保存一個(gè)實(shí)體。
	 */
	<S extends T> S save(S entity);

測(cè)試代碼:

    @GetMapping("/customer/save")
    public Customer crudRepository_save() {
        // 保存一個(gè)用戶michael
        Customer michael = new Customer("Michael", 26);
        Customer res = customerRepository.save(michael);
        return res;
    }

測(cè)試結(jié)果:

2.2 saveAll

    /**
	 * 保存提供的所有實(shí)體。
	 */
	<S extends T> Iterable<S> saveAll(Iterable<S> entities);

測(cè)試代碼:

    @GetMapping("/customer/saveAll")
    public List<Customer> crudRepository_saveAll() {
        // 保存指定集合的實(shí)體
        List<Customer> customerList = new ArrayList<>();
        customerList.add(new Customer("Tom", 21));
        customerList.add(new Customer("Jack", 21));
        List<Customer> res = customerRepository.saveAll(customerList);
        return res;
    }

測(cè)試結(jié)果:

2.3 findById

    /**
	 * 根據(jù)id查詢對(duì)應(yīng)的實(shí)體。
	 */
	Optional<T> findById(ID id);

測(cè)試代碼:

    @GetMapping("/customer/findById")
    public Customer crudRepository_findById() {
        // 根據(jù)id查詢對(duì)應(yīng)實(shí)體
        Optional<Customer> customer = customerRepository.findById(1L);
        if(customer.isPresent()) {
            return customer.get();
        }
        return null;
    }

測(cè)試結(jié)果:

2.4 existsById

    /**
	 * 根據(jù)id查詢對(duì)應(yīng)的實(shí)體是否存在。
	 */
	boolean existsById(ID id);

測(cè)試代碼:

    @GetMapping("/customer/existsById")
    public boolean crudRepository_existsById() {
        // 根據(jù)id查詢對(duì)應(yīng)的實(shí)體是否存在
        return customerRepository.existsById(1L);
    }

測(cè)試結(jié)果:

2.5 findAll

    /**
	 * 查詢所有的實(shí)體。
	 */
	Iterable<T> findAll();

測(cè)試代碼:

    @GetMapping("/customer/findAll")
    public List<Customer> crudRepository_findAll() {
        // 查詢所有的實(shí)體
        List<Customer> customerList = customerRepository.findAll();
        return customerList;
    }

測(cè)試結(jié)果:

2.6 findAllById

    /**
	 * 根據(jù)給定的id集合查詢所有對(duì)應(yīng)的實(shí)體,返回實(shí)體集合。
	 */
	Iterable<T> findAllById(Iterable<ID> ids);

測(cè)試代碼:

    @GetMapping("/customer/findAllById")
    public List<Customer> crudRepository_findAllById() {
        // 根據(jù)給定的id集合查詢所有對(duì)應(yīng)的實(shí)體,返回實(shí)體集合
        List<Long> ids = new ArrayList<>();
        ids.add(2L);
        ids.add(1L);
        List<Customer> customerList = customerRepository.findAllById(ids);
        return customerList;
    }

測(cè)試結(jié)果:

2.7 count

    /**
	 * 統(tǒng)計(jì)現(xiàn)存實(shí)體的個(gè)數(shù)。
	 */
	long count();

測(cè)試代碼:

    @GetMapping("/customer/count")
    public Long crudRepository_count() {
        // 統(tǒng)計(jì)現(xiàn)存實(shí)體的個(gè)數(shù)
        return customerRepository.count();
    }

測(cè)試結(jié)果:

2.8 deleteById

    /**
	 * 根據(jù)id刪除對(duì)應(yīng)的實(shí)體。
	 */
	void deleteById(ID id);

測(cè)試代碼:

    @GetMapping("/customer/deleteById")
    public void crudRepository_deleteById() {
        // 根據(jù)id刪除對(duì)應(yīng)的實(shí)體
         customerRepository.deleteById(1L);
    }

測(cè)試結(jié)果:

刪除前~~

刪除后~~

2.9 delete(T entity)

	/**
	 * 刪除給定的實(shí)體。
	 */
	void delete(T entity);

測(cè)試代碼:

    @GetMapping("/customer/delete")
    public void crudRepository_delete() {
        // 刪除給定的實(shí)體
        Customer customer = new Customer(2L, "Tom", 21);
        customerRepository.delete(customer);
    }

測(cè)試結(jié)果:

刪除前~~

刪除后~~

2.10 deleteAll(Iterable<? extends T> entities)

	/**
	 * 刪除給定的實(shí)體集合。
	 */
	void deleteAll(Iterable<? extends T> entities);

測(cè)試代碼:

    @GetMapping("/customer/deleteAll(entities)")
    public void crudRepository_deleteAll_entities() {
        // 刪除給定的實(shí)體集合
        Customer tom = new Customer(2L,"Tom", 21);
        Customer jack = new Customer(3L,"Jack", 21);
        List<Customer> entities = new ArrayList<>();
        entities.add(tom);
        entities.add(jack);
        customerRepository.deleteAll(entities);
    }

測(cè)試結(jié)果:

刪除前~~

刪除后~~

2.11 deleteAll

	/**
	 * 刪除所有的實(shí)體。
	 */
	void deleteAll();

測(cè)試代碼:

    @GetMapping("/customer/deleteAll")
    public void crudRepository_deleteAll() {
        // 刪除所有的實(shí)體
        customerRepository.deleteAll();
    }

測(cè)試結(jié)果:

刪除前~~

刪除后~~

2.12 findAll(Sort sort)

    /**
	 * 返回所有的實(shí)體,根據(jù)Sort參數(shù)提供的規(guī)則排序。
	 */
	Iterable<T> findAll(Sort sort);

測(cè)試代碼:

    @GetMapping("/customer/findAll(sort)")
    public List<Customer> pagingAndSortingRepository_findAll_sort() {
        // 返回所有的實(shí)體,根據(jù)Sort參數(shù)提供的規(guī)則排序
        // 按age值降序排序
        Sort sort = new Sort(Sort.Direction.DESC, "age");
        List<Customer> res = customerRepository.findAll(sort);
        return res;
    }

測(cè)試結(jié)果:

格式化之后發(fā)現(xiàn),確實(shí)是按照age的值降序輸出的!?。?/p>

2.13 findAll(Pageable pageable)

    /**
	 * 返回一頁(yè)實(shí)體,根據(jù)Pageable參數(shù)提供的規(guī)則進(jìn)行過(guò)濾。
	 */
	Page<T> findAll(Pageable pageable);

測(cè)試代碼:

    @GetMapping("/customer/findAll(pageable)")
    public void pagingAndSortingRepository_findAll_pageable() {
        // 分頁(yè)查詢
        // PageRequest.of 的第一個(gè)參數(shù)表示第幾頁(yè)(注意:第一頁(yè)從序號(hào)0開(kāi)始),第二個(gè)參數(shù)表示每頁(yè)的大小
        Pageable pageable = PageRequest.of(1, 5); //查第二頁(yè)
        Page<Customer> page = customerRepository.findAll(pageable);
        System.out.println("查詢總頁(yè)數(shù):" + page.getTotalPages());
        System.out.println("查詢總記錄數(shù):" + page.getTotalElements());
        System.out.println("查詢當(dāng)前第幾頁(yè):" + (page.getNumber() + 1));
        System.out.println("查詢當(dāng)前頁(yè)面的集合:" + page.getContent());
        System.out.println("查詢當(dāng)前頁(yè)面的記錄數(shù):" + page.getNumberOfElements());
    }

測(cè)試結(jié)果:

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

相關(guān)文章

  • Spring StopWatch使用實(shí)例詳解

    Spring StopWatch使用實(shí)例詳解

    這篇文章主要介紹了Spring StopWatch使用實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Java web開(kāi)發(fā)中加載圖片路徑的兩種方式

    Java web開(kāi)發(fā)中加載圖片路徑的兩種方式

    下文給大家介紹基于編譯器idea以及tomcat服務(wù)器開(kāi)發(fā)的,對(duì)Java web開(kāi)發(fā)加載圖片路徑的兩種方式感興趣的朋友一起看看吧
    2017-07-07
  • 分享Java多線程實(shí)現(xiàn)的四種方式

    分享Java多線程實(shí)現(xiàn)的四種方式

    這篇文章主要介紹了分享Java多線程實(shí)現(xiàn)的四種方式,文章基于?Java的相關(guān)資料展開(kāi)多線程的詳細(xì)介紹,具有一的的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • 解決myBatis返回integer值的問(wèn)題

    解決myBatis返回integer值的問(wèn)題

    這篇文章主要介紹了解決myBatis返回integer值的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • 最新評(píng)論

    海伦市| 囊谦县| 留坝县| 阜城县| 洪洞县| 罗山县| 武夷山市| 广南县| 延庆县| 靖边县| 日土县| 巴东县| 海伦市| 马鞍山市| 包头市| 合水县| 宝山区| 娱乐| 襄樊市| 丰镇市| 娄底市| 察隅县| 轮台县| 辽宁省| 金门县| 烟台市| 昭觉县| 隆化县| 连城县| 汶上县| 鹿邑县| 东阳市| 泾源县| 库尔勒市| 杭锦后旗| 安乡县| 安福县| 基隆市| 南雄市| 邵阳县| 日照市|