Repository
父接口标记型接口,不包含任何方法.
源代码:
package org.springframework.data.repository;
import org.springframework.stereotype.Indexed;
@Indexed
public interface Repository<T, ID> {
}
<T>是持久层管理的主要实体类型
<ID>是实体类的主ID(也就是对应表的主键)
CrudRepository
提供了简单的增删改查(crud)方法,继承后可以直接使用
package org.springframework.data.repository;
import java.util.Optional;
@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity);
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
Optional<T> findById(ID id);
boolean existsById(ID id);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> ids);
long count();
void deleteById(ID id);
void delete(T entity);
void deleteAll(Iterable<? extends T> entities);
void deleteAll();
}
共提供了10个方法
save:新增一个实体类
saveAll:新增一组实体类
findById:通过主键Id查询
existsById:根据Id判断某个实体类是否存在
findAll:查询某个类的所有实例
findAllById:根据给定的一组Id查询对应的一组实体类
count:统计实体类的数量
deleteById:根据Id删除
delete:删除某个实体类
deleteAll:删除给定的一组实体类 / 删除所有实体类
PagingAndSortingRepository
提供了简单的分页和排序功能,查询出所有数据后可以进行分页或者排序,需要传入Sort或者Pageable参数,设置分页或排序的规则。
package org.springframework.data.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
JpaRepository
在父接口的基础上,提供了几个其他的方法
flush:立即生效(一般不用)
saveAndFlush:保存并立即生效(save方法可能会有延迟,save操作后不会立即提交到数据库,有可能只是修改内存。)
deleteInBatch:批量删除(delete是删除一个数据作为一个操作,batch是删除多个数据在同一个操作中。个人感觉可以对比JDBC中的批量执行sql,addBatch(sql)和executeBatch(),不知是否恰当)关于delete和deleteInBatch的区别可以参考:https://stackoverflow.com/questions/26142261/spring-jparepostory-delete-vs-deleteinbatch
deleteAllInBatch:批量删除所有
getOne:根据给定主ID查询,如果不存在抛EntityNotFoundException。返回的是实体类的一个引用(Returns a reference to the entity )关于getOne和findById区别可以参考:https://blog.csdn.net/zx110503/article/details/81356735
package org.springframework.data.jpa.repository;
import java.util.List;
import javax.persistence.EntityManager;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
List<T> findAll();
List<T> findAll(Sort sort);
List<T> findAllById(Iterable<ID> ids);
<S extends T> List<S> saveAll(Iterable<S> entities);
void flush();
<S extends T> S saveAndFlush(S entity);
void deleteInBatch(Iterable<T> entities);
void deleteAllInBatch();
T getOne(ID id);
@Override
<S extends T> List<S> findAll(Example<S> example);
@Override
<S extends T> List<S> findAll(Example<S> example, Sort sort);
}
最下面的两个findAll是重写的QueryByExampleExecutor
SimpleJpaRepository
接口的实现类,里面有接口中方法的具体实现逻辑,可以查看方法的源码。
QueryByExampleExecutor
JpaRepository继承的接口
package org.springframework.data.repository.query;
import java.util.Optional;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
public interface QueryByExampleExecutor<T> {
<S extends T> Optional<S> findOne(Example<S> example);
<S extends T> Iterable<S> findAll(Example<S> example);
<S extends T> Iterable<S> findAll(Example<S> example, Sort sort);
<S extends T> Page<S> findAll(Example<S> example, Pageable pageable);
<S extends T> long count(Example<S> example);
<S extends T> boolean exists(Example<S> example);
}
JpaSpecificationExecutor
SimpleJpaRepository实现的接口
package org.springframework.data.jpa.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
public interface JpaSpecificationExecutor<T> {
Optional<T> findOne(@Nullable Specification<T> spec);
List<T> findAll(@Nullable Specification<T> spec);
Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
List<T> findAll(@Nullable Specification<T> spec, Sort sort);
long count(@Nullable Specification<T> spec);
}