概览

  JpaRepository 继承 PagingAndSortingRepository 继承 CrudRepository 继承 Repository

1 Repository

  这是一个空接口,主要是用来指定它的子接口是一个持久层接口

  实现了Repository的接口默认就是一个持久层接口,会被容器管理起来;这就是为什么我们自己写的接口继承了JpaRepository后不用添加@Repository的原因

  1. public interface Repository<T, ID extends Serializable> {
  2.  
  3. }

Repository

2 CrudRepository

  CrudRepository继承自Repository接口,该接口定义了一些简单的增删改查方法

  1. /*
  2. * Copyright 2008-2011 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.data.repository;
  17.  
  18. import java.io.Serializable;
  19.  
  20. /**
  21. * Interface for generic CRUD operations on a repository for a specific type.
  22. *
  23. * @author Oliver Gierke
  24. * @author Eberhard Wolff
  25. */
  26. @NoRepositoryBean
  27. public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
  28.  
  29. /**
  30. * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
  31. * entity instance completely.
  32. *
  33. * @param entity
  34. * @return the saved entity
  35. */
  36. <S extends T> S save(S entity);
  37.  
  38. /**
  39. * Saves all given entities.
  40. *
  41. * @param entities
  42. * @return the saved entities
  43. * @throws IllegalArgumentException in case the given entity is {@literal null}.
  44. */
  45. <S extends T> Iterable<S> save(Iterable<S> entities);
  46.  
  47. /**
  48. * Retrieves an entity by its id.
  49. *
  50. * @param id must not be {@literal null}.
  51. * @return the entity with the given id or {@literal null} if none found
  52. * @throws IllegalArgumentException if {@code id} is {@literal null}
  53. */
  54. T findOne(ID id);
  55.  
  56. /**
  57. * Returns whether an entity with the given id exists.
  58. *
  59. * @param id must not be {@literal null}.
  60. * @return true if an entity with the given id exists, {@literal false} otherwise
  61. * @throws IllegalArgumentException if {@code id} is {@literal null}
  62. */
  63. boolean exists(ID id);
  64.  
  65. /**
  66. * Returns all instances of the type.
  67. *
  68. * @return all entities
  69. */
  70. Iterable<T> findAll();
  71.  
  72. /**
  73. * Returns all instances of the type with the given IDs.
  74. *
  75. * @param ids
  76. * @return
  77. */
  78. Iterable<T> findAll(Iterable<ID> ids);
  79.  
  80. /**
  81. * Returns the number of entities available.
  82. *
  83. * @return the number of entities
  84. */
  85. long count();
  86.  
  87. /**
  88. * Deletes the entity with the given id.
  89. *
  90. * @param id must not be {@literal null}.
  91. * @throws IllegalArgumentException in case the given {@code id} is {@literal null}
  92. */
  93. void delete(ID id);
  94.  
  95. /**
  96. * Deletes a given entity.
  97. *
  98. * @param entity
  99. * @throws IllegalArgumentException in case the given entity is {@literal null}.
  100. */
  101. void delete(T entity);
  102.  
  103. /**
  104. * Deletes the given entities.
  105. *
  106. * @param entities
  107. * @throws IllegalArgumentException in case the given {@link Iterable} is {@literal null}.
  108. */
  109. void delete(Iterable<? extends T> entities);
  110.  
  111. /**
  112. * Deletes all entities managed by the repository.
  113. */
  114. void deleteAll();
  115. }

CrudRepository

3 PagingAndSortingRepository

  PagingAndSortingRepository继承自CrudRepository接口,该接口不仅有CrudRepository接口中的增删改查方法,还定义了分页查询方法和排序方法

  1. /*
  2. * Copyright 2008-2010 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.data.repository;
  17.  
  18. import java.io.Serializable;
  19.  
  20. import org.springframework.data.domain.Page;
  21. import org.springframework.data.domain.Pageable;
  22. import org.springframework.data.domain.Sort;
  23.  
  24. /**
  25. * Extension of {@link CrudRepository} to provide additional methods to retrieve entities using the pagination and
  26. * sorting abstraction.
  27. *
  28. * @author Oliver Gierke
  29. * @see Sort
  30. * @see Pageable
  31. * @see Page
  32. */
  33. @NoRepositoryBean
  34. public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
  35.  
  36. /**
  37. * Returns all entities sorted by the given options.
  38. *
  39. * @param sort
  40. * @return all entities sorted by the given options
  41. */
  42. Iterable<T> findAll(Sort sort);
  43.  
  44. /**
  45. * Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object.
  46. *
  47. * @param pageable
  48. * @return a page of entities
  49. */
  50. Page<T> findAll(Pageable pageable);
  51. }

PagingAndSortingRepository

4 JpaRepository

  JpaRepository继承自PagingAndSortingRepository接口和QueryByExampleExecutor接口,该接口不仅有PagingAndSortingRepository中的方法还有QueryByExampleExecutor中定义的方法

  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5.  
  6. package org.springframework.data.jpa.repository;
  7.  
  8. import java.io.Serializable;
  9. import java.util.List;
  10. import org.springframework.data.domain.Example;
  11. import org.springframework.data.domain.Sort;
  12. import org.springframework.data.repository.NoRepositoryBean;
  13. import org.springframework.data.repository.PagingAndSortingRepository;
  14. import org.springframework.data.repository.query.QueryByExampleExecutor;
  15.  
  16. @NoRepositoryBean
  17. public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
  18. List<T> findAll();
  19.  
  20. List<T> findAll(Sort var1);
  21.  
  22. List<T> findAll(Iterable<ID> var1);
  23.  
  24. <S extends T> List<S> save(Iterable<S> var1);
  25.  
  26. void flush();
  27.  
  28. <S extends T> S saveAndFlush(S var1);
  29.  
  30. void deleteInBatch(Iterable<T> var1);
  31.  
  32. void deleteAllInBatch();
  33.  
  34. T getOne(ID var1);
  35.  
  36. <S extends T> List<S> findAll(Example<S> var1);
  37.  
  38. <S extends T> List<S> findAll(Example<S> var1, Sort var2);
  39. }

JpaRepository

1. 定义

  JpaRepository接口中实现了一些简单的增删改查功能

  1. @NoRepositoryBean
  2. public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
  3. List<T> findAll();
  4.  
  5. List<T> findAll(Sort var1);
  6.  
  7. List<T> findAll(Iterable<ID> var1);
  8.  
  9. <S extends T> List<S> save(Iterable<S> var1);
  10.  
  11. void flush();
  12.  
  13. <S extends T> S saveAndFlush(S var1);
  14.  
  15. void deleteInBatch(Iterable<T> var1);
  16.  
  17. void deleteAllInBatch();
  18.  
  19. T getOne(ID var1);
  20.  
  21. <S extends T> List<S> findAll(Example<S> var1);
  22.  
  23. <S extends T> List<S> findAll(Example<S> var1, Sort var2);
  24. }

JpaRepository

2 技巧

  实现了JpaRepository后的接口会被容器自动进行管理

SpringBoot05 数据操作02 -> JPA接口详解的更多相关文章

  1. SpringBoot05 数据操作01 -> JPA的基本使用、基本使用02

    前提: 创建一个springboot项目 创建一个名为springboottest的MySQL数据库 1 jar包准备 jpa的jar包 mysql驱动的jar包 druid数据库连接池的jar包 l ...

  2. SpringBoot05 数据操作03 -> JPA查询方法的规则定义

    请参见<springboot详解>springjpa部分知识 1 按照方法命名来进行查询 待更新... package cn.xiangxu.springboot.repository; ...

  3. jQuery 源码分析(十三) 数据操作模块 DOM属性 详解

    jQuery的属性操作模块总共有4个部分,本篇说一下第2个部分:DOM属性部分,用于修改DOM元素的属性的(属性和特性是不一样的,一般将property翻译为属性,attribute翻译为特性) DO ...

  4. jQuery 源码分析(十二) 数据操作模块 html特性 详解

    jQuery的属性操作模块总共有4个部分,本篇说一下第1个部分:HTML特性部分,html特性部分是对原生方法getAttribute()和setAttribute()的封装,用于修改DOM元素的特性 ...

  5. JPA EntityManager详解(一)

    JPA EntityManager详解(一) 持久化上下文(Persistence Contexts)的相关知识,内容包括如何从Java EE容器中创建EntityManager对象.如何从Java ...

  6. socket接口详解

    1. socket概述 socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信. socket起源于UNIX,在Unix一切 ...

  7. JPA EntityManager详解

    EntityManager是JPA中用于增删改查的接口,它的作用相当于一座桥梁,连接内存中的java对象和数据库的数据存储.其接口如下: public interface EntityManager ...

  8. [转载]MII/MDIO接口详解

    原文地址:MII/MDIO接口详解作者:心田麦浪 本文主要分析MII/RMII/SMII,以及GMII/RGMII/SGMII接口的信号定义,及相关知识,同时本文也对RJ-45接口进行了总结,分析了在 ...

  9. ReadWriteLock 接口详解

    ReadWriteLock 接口详解 这是本人阅读ReadWriteLock接口源码的注释后,写出的一篇知识分享博客 读写锁的成分是什么? 读锁 Lock readLock(); 只要没有写锁,读锁可 ...

随机推荐

  1. 转 postfix邮件服务下mailq、postmap、postqueue 、 postsuper等用法

    1.Mailq 功能说明:显示待寄邮件的清单. 语 法:mailq [-q] 补充说明:mailq可列出待寄邮件的清单,包括邮件ID,邮件大小,邮件保存时间,寄信人,收信人,以及邮件无法寄出的原因,提 ...

  2. Linux下tar命令的各种参数选项和他们的作用整理

    1.建立TAR包(打包)命令格式:tar cvf TAR包文件名.tar 所备份的文件或目录功能描述:tar cvf命令用于把指定的目录或文件打包到指定的文件中.“c”指定建立(或压缩)TAR包,“v ...

  3. Github-jcjohnson/torch-rnn代码详解

    Github-jcjohnson/torch-rnn代码详解 zoerywzhou@gmail.com http://www.cnblogs.com/swje/ 作者:Zhouwan  2016-3- ...

  4. bzoj5457 城市

    一棵树,每个点有一个民族,和一个人数,求每个子树里最多的民族及其人数,如果一样,输出编号最小的 $n \leq 500000$ sol: 卡莫队的毒瘤题,需要 dsu on tree 大概就是 dfs ...

  5. Redis底层探秘(一):简单动态字符串(SDS)

    redis是我们使用非常多的一种缓存技术,他的性能极高,读的速度是110000次/s,写的速度是81000次/s.这么高的性能背后,到底是怎么样的实现在支撑,这个系列的文章,我们一起去看看. redi ...

  6. 《Javascript高级程序设计》阅读记录(三):第五章 上

    这个系列以往文字地址: <Javascript高级程序设计>阅读记录(一):第二.三章 <Javascript高级程序设计>阅读记录(二):第四章 这个系列,我会把阅读< ...

  7. COGS1516. 棋盘上的车

    [题目描述] 在n*n(n≤20)的方格棋盘上放置n 个车,求使它们不能互相攻击的方案总数. [输入格式] 一行一个正整数n. [输出格式] 一行一个正整数,即方案总数. [样例输入] 3 [样例输出 ...

  8. Python函数-map()

    map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回.如下: def ...

  9. Windows命令查看文件的MD5/SHA1/SHA256

    certutil -hashfile "D:\Tools\Microsoft\SqlServer\2016\ct_sql_server_2016_enterprise_x64_dvd_869 ...

  10. 自己封装的AJAX (带JSON)

    最简单的封装的AJAX: function myajax(url,onsuccess,fail){ //确定是否支持xhr var xhr = new XMLHttpRequest ? new XML ...