MongoRepository interface
MongoRepository的继承层次:
(parent: spring boot 2.3.2)
Repository
\___ CrudRepository - save(),saveAll(),findById(),existsById(),findAll(),findAllById(),count(),deleteById(),delete(),deleteAll(),deleteAll(无参数)
\____ PagingAndSortingRepository - findAll(Sort), findAll(Pageable)
\______
\ ___ MongoRepository - *saveAll(),*findAll(),*findAll(Sort),insert(),insert(),*findAll()
_____________________________/
/
QueryByExampleExecutor - findOne(), findAll(), .., count(),exists()
1 @Indexed 2 public interface Repository<T, ID> { 3 4 }
1 @NoRepositoryBean 2 public interface CrudRepository<T, ID> extends Repository<T, ID> { 3 4 /** 5 * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the 6 * entity instance completely. 7 * 8 * @param entity must not be {@literal null}. 9 * @return the saved entity; will never be {@literal null}. 10 * @throws IllegalArgumentException in case the given {@literal entity} is {@literal null}. 11 */ 12 <S extends T> S save(S entity); 13 14 /** 15 * Saves all given entities. 16 * 17 * @param entities must not be {@literal null} nor must it contain {@literal null}. 18 * @return the saved entities; will never be {@literal null}. The returned {@literal Iterable} will have the same size 19 * as the {@literal Iterable} passed as an argument. 20 * @throws IllegalArgumentException in case the given {@link Iterable entities} or one of its entities is 21 * {@literal null}. 22 */ 23 <S extends T> Iterable<S> saveAll(Iterable<S> entities); 24 25 /** 26 * Retrieves an entity by its id. 27 * 28 * @param id must not be {@literal null}. 29 * @return the entity with the given id or {@literal Optional#empty()} if none found. 30 * @throws IllegalArgumentException if {@literal id} is {@literal null}. 31 */ 32 Optional<T> findById(ID id); 33 34 /** 35 * Returns whether an entity with the given id exists. 36 * 37 * @param id must not be {@literal null}. 38 * @return {@literal true} if an entity with the given id exists, {@literal false} otherwise. 39 * @throws IllegalArgumentException if {@literal id} is {@literal null}. 40 */ 41 boolean existsById(ID id); 42 43 /** 44 * Returns all instances of the type. 45 * 46 * @return all entities 47 */ 48 Iterable<T> findAll(); 49 50 /** 51 * Returns all instances of the type {@code T} with the given IDs. 52 * <p> 53 * If some or all ids are not found, no entities are returned for these IDs. 54 * <p> 55 * Note that the order of elements in the result is not guaranteed. 56 * 57 * @param ids must not be {@literal null} nor contain any {@literal null} values. 58 * @return guaranteed to be not {@literal null}. The size can be equal or less than the number of given 59 * {@literal ids}. 60 * @throws IllegalArgumentException in case the given {@link Iterable ids} or one of its items is {@literal null}. 61 */ 62 Iterable<T> findAllById(Iterable<ID> ids); 63 64 /** 65 * Returns the number of entities available. 66 * 67 * @return the number of entities. 68 */ 69 long count(); 70 71 /** 72 * Deletes the entity with the given id. 73 * 74 * @param id must not be {@literal null}. 75 * @throws IllegalArgumentException in case the given {@literal id} is {@literal null} 76 */ 77 void deleteById(ID id); 78 79 /** 80 * Deletes a given entity. 81 * 82 * @param entity must not be {@literal null}. 83 * @throws IllegalArgumentException in case the given entity is {@literal null}. 84 */ 85 void delete(T entity); 86 87 /** 88 * Deletes the given entities. 89 * 90 * @param entities must not be {@literal null}. Must not contain {@literal null} elements. 91 * @throws IllegalArgumentException in case the given {@literal entities} or one of its entities is {@literal null}. 92 */ 93 void deleteAll(Iterable<? extends T> entities); 94 95 /** 96 * Deletes all entities managed by the repository. 97 */ 98 void deleteAll(); 99 }
1 @NoRepositoryBean 2 public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> { 3 4 /** 5 * Returns all entities sorted by the given options. 6 * 7 * @param sort 8 * @return all entities sorted by the given options 9 */ 10 Iterable<T> findAll(Sort sort); 11 12 /** 13 * Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object. 14 * 15 * @param pageable 16 * @return a page of entities 17 */ 18 Page<T> findAll(Pageable pageable); 19 }
1 public interface QueryByExampleExecutor<T> { 2 3 /** 4 * Returns a single entity matching the given {@link Example} or {@literal null} if none was found. 5 * 6 * @param example must not be {@literal null}. 7 * @return a single entity matching the given {@link Example} or {@link Optional#empty()} if none was found. 8 * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if the Example yields more than one result. 9 */ 10 <S extends T> Optional<S> findOne(Example<S> example); 11 12 /** 13 * Returns all entities matching the given {@link Example}. In case no match could be found an empty {@link Iterable} 14 * is returned. 15 * 16 * @param example must not be {@literal null}. 17 * @return all entities matching the given {@link Example}. 18 */ 19 <S extends T> Iterable<S> findAll(Example<S> example); 20 21 /** 22 * Returns all entities matching the given {@link Example} applying the given {@link Sort}. In case no match could be 23 * found an empty {@link Iterable} is returned. 24 * 25 * @param example must not be {@literal null}. 26 * @param sort the {@link Sort} specification to sort the results by, must not be {@literal null}. 27 * @return all entities matching the given {@link Example}. 28 * @since 1.10 29 */ 30 <S extends T> Iterable<S> findAll(Example<S> example, Sort sort); 31 32 /** 33 * Returns a {@link Page} of entities matching the given {@link Example}. In case no match could be found, an empty 34 * {@link Page} is returned. 35 * 36 * @param example must not be {@literal null}. 37 * @param pageable can be {@literal null}. 38 * @return a {@link Page} of entities matching the given {@link Example}. 39 */ 40 <S extends T> Page<S> findAll(Example<S> example, Pageable pageable); 41 42 /** 43 * Returns the number of instances matching the given {@link Example}. 44 * 45 * @param example the {@link Example} to count instances for. Must not be {@literal null}. 46 * @return the number of instances matching the {@link Example}. 47 */ 48 <S extends T> long count(Example<S> example); 49 50 /** 51 * Checks whether the data store contains elements that match the given {@link Example}. 52 * 53 * @param example the {@link Example} to use for the existence check. Must not be {@literal null}. 54 * @return {@literal true} if the data store contains elements that match the given {@link Example}. 55 */ 56 <S extends T> boolean exists(Example<S> example); 57 }
1 @NoRepositoryBean 2 public interface MongoRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> { 3 4 /* 5 * (non-Javadoc) 6 * @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable) 7 */ 8 @Override 9 <S extends T> List<S> saveAll(Iterable<S> entities); 10 11 /* 12 * (non-Javadoc) 13 * @see org.springframework.data.repository.CrudRepository#findAll() 14 */ 15 @Override 16 List<T> findAll(); 17 18 /* 19 * (non-Javadoc) 20 * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort) 21 */ 22 @Override 23 List<T> findAll(Sort sort); 24 25 /** 26 * Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use the 27 * returned instance for further operations as the save operation might have changed the entity instance completely. 28 * Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API. 29 * 30 * @param entity must not be {@literal null}. 31 * @return the saved entity 32 * @since 1.7 33 */ 34 <S extends T> S insert(S entity); 35 36 /** 37 * Inserts the given entities. Assumes the given entities to have not been persisted yet and thus will optimize the 38 * insert over a call to {@link #saveAll(Iterable)}. Prefer using {@link #saveAll(Iterable)} to avoid the usage of store 39 * specific API. 40 * 41 * @param entities must not be {@literal null}. 42 * @return the saved entities 43 * @since 1.7 44 */ 45 <S extends T> List<S> insert(Iterable<S> entities); 46 47 /* 48 * (non-Javadoc) 49 * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example) 50 */ 51 @Override 52 <S extends T> List<S> findAll(Example<S> example); 53 54 /* 55 * (non-Javadoc) 56 * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort) 57 */ 58 @Override 59 <S extends T> List<S> findAll(Example<S> example, Sort sort); 60 }