spring data 接口之 JpaRepository,JpaSpecificationExecutor
2016-01-18 15:52
786 查看
JpaRepository 继承于 PagingAndSortingRepository 接口, 拥有PagingAndSortingRepository 的所有方法,而JpaSpecificationExecutor 不属于Repository 体系。由于JpaSpecificationExecutor 并不继承repository 接口,所以它不能单独使用,只能和jpa Repository 一起用。
JpaRepository 接口特色: 1. 将一些查询方法的返回类型由Iterable 转换成了 List<T>; 2. 新增了保存或更新等方法。
JpaSpecificationExecutor特色:实现了带条件的查询, 类似于Hibernate 的cretira
一 继承JpaRepository,JpaSpecificationExecutor 接口的接口:IStudentJpaRepository
二 测试类
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{ @Autowired private IStudentJpaRepository studentJpaSpecification; @Test public void test_findAll(){ Specification<StudentPO> specification = new Specification<StudentPO>() { @Override public 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); } } }
相关文章推荐
- spring data 之JpaRepository,JpaSpecificationExecutor
- ASP.NET全球化与本地化 c#多国语言的支持 (项目支持多国语言的开发)
- ASP.NET MVC 中将FormCollection与实体间转换方法
- 【深入ASP.NET原理系列】--ASP.NET页面生命周期
- ASP.Net MVC 之FileResult
- ASP.NET Web API中实现版本的几种方式
- spring中AspectJ(16)
- ASP.NET MVC 3 常用
- repeater中CheckBoxList/RadioButtonList取值赋值
- ASP.NET WebAPI Get使用复杂型别
- ASP.Net MVC的ViewBag一个坑,不要跳进去
- ASP.Net MVC的ViewBag一个坑,不要跳进去
- asp gridview 统计总条数
- 在ASP.NET中基于Owin OAuth使用Client Credentials Grant授权发放Token
- asp.net cache 缓存
- ASP.NET Core 1.0基础之应用启动
- [转]Asp.Net MVC使用HtmlHelper渲染,并传递FormCollection参数的陷阱
- Razor:从aspx到cshtml常见错误及正确书写方法
- javaEE之-------Spring中的aspectJ的应用
- iReport与JasperReport PDF中文解决