spring data 接口之 JpaRepository,JpaSpecificationExecutor

来源:互联网 发布:罗浮 荣威 知乎 编辑:程序博客网 时间:2024/05/21 08:57

JpaRepository 继承于 PagingAndSortingRepository 接口, 拥有PagingAndSortingRepository 的所有方法,而JpaSpecificationExecutor 不属于Repository 体系。由于JpaSpecificationExecutor 并不继承repository 接口,所以它不能单独使用,只能和jpa Repository 一起用。


 JpaRepository 接口特色: 1. 将一些查询方法的返回类型由Iterable 转换成了 List<T>; 2. 新增了保存或更新等方法。

          List<T> findAll();  List<T> findAll(Sort sort);  List<T> findAll(Iterable<ID> ids);  <S extends T> List<S> save(Iterable<S> entities);  void flush();  <S extends T> S saveAndFlush(S entity);   void deleteInBatch(Iterable<T> entities);  void deleteAllInBatch();  T getOne(ID id);

JpaSpecificationExecutor特色:实现了带条件的查询, 类似于Hibernate 的cretira

           T findOne(Specification<T>);   List<T> findAll(Specification<T>);   List<T> findAll(Specification<T>, Sort);   List<T> findAll(Specification<T>, Pageable);   long count(Specification<T>);

一 继承JpaRepository,JpaSpecificationExecutor 接口的接口:IStudentJpaRepository

package org.zgf.spring.data.dao;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.JpaSpecificationExecutor;import org.zgf.spring.data.entity.StudentPO;/** * JpaRepository 继承于 PagingAndSortingRepository 接口,  * 由于JpaSpecificationExecutor 并不继承repository 接口,所以它不能单独使用,只能和jpa Repository 一起用 * * @author: zonggf * @date: 2016年1月15日-下午12:58:38 */public interface IStudentJpaRepository extends JpaRepository<StudentPO, Integer>, JpaSpecificationExecutor<StudentPO> {/** JpaRepository 主要特点,将返回类型 Iterable 转换成了List, 并新增了写方法 * List<T> findAll(); * List<T> findAll(Sort sort); * List<T> findAll(Iterable<ID> ids); * <S extends T> List<S> save(Iterable<S> entities); * void flush(); * <S extends T> S saveAndFlush(S entity);  * void deleteInBatch(Iterable<T> entities); * void deleteAllInBatch(); * T getOne(ID id); *//** JpaSpecificationExecutor 实现了带条件的查询 *  T findOne(Specification<T>); *  List<T> findAll(Specification<T>); *  List<T> findAll(Specification<T>, Sort); *  List<T> findAll(Specification<T>, Pageable); *  long count(Specification<T>); */}

二 测试类

package org.zgf.spring.data.dao;import java.util.List;import javax.persistence.criteria.CriteriaBuilder;import javax.persistence.criteria.CriteriaQuery;import javax.persistence.criteria.Path;import javax.persistence.criteria.Predicate;import javax.persistence.criteria.Root;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.jpa.domain.Specification;import org.zgf.spring.data.base.BaseTest;import org.zgf.spring.data.dao.IStudentJpaRepository;import org.zgf.spring.data.entity.StudentPO;/** * @ClassName: Test_IStudentJpaRepository * @Description:  * @author: zonggf * @date: 2016年1月15日-下午1:01:54 */public class Test_IStudentJpaRepository extends BaseTest{@Autowiredprivate IStudentJpaRepository studentJpaSpecification;@Testpublic void test_findAll(){Specification<StudentPO> specification = new Specification<StudentPO>() {@Overridepublic Predicate toPredicate(Root<StudentPO> root, CriteriaQuery<?> query, CriteriaBuilder cb) {Path path = root.get("id");Predicate predicate = cb.lt(path, 5);return predicate;}};List<StudentPO> studentList = this.studentJpaSpecification.findAll(specification);for (StudentPO studentPO : studentList) {System.out.println(studentPO);}}}


0 1