spring数据jpa中准则的等价性

spring数据jpa中准则的等价性,spring,hibernate,jpa,criteria,spring-data,Spring,Hibernate,Jpa,Criteria,Spring Data,我使用的是hibernate,但我听说spring-data-jpa是最好的,所以我尝试了一下,我对它很满意,直到我面对这个问题 我在我的jsp中有一个带有许多条件的搜索表单,用户可以选择他想要的任何东西 那么在spring数据jpa if(startDate!=null){ criteria.add(Expression.ge("date",startDate)); } if(endDate!=null){ criteria.add(Expression.le("date",e

我使用的是
hibernate
,但我听说
spring-data-jpa
是最好的,所以我尝试了一下,我对它很满意,直到我面对这个问题

我在我的
jsp
中有一个带有许多条件的搜索表单,用户可以选择他想要的任何东西

那么在
spring数据jpa

if(startDate!=null){
    criteria.add(Expression.ge("date",startDate));
}
if(endDate!=null){
    criteria.add(Expression.le("date",endDate));
}
if(volume!=null){
    criteria.add(Expression.ge("volume",volume));
}
if ....

是的。一篇关于如何使用Spring数据的博客文章

Spring Jpa数据中的等价物是
规范
,您可以使用存储库
规范执行器
和Jpa元模型来创建Jpa标准

你可以找到关于Jpa的介绍

一个简单的例子:

  • 实体
  • @实体

    public class ClassRoom {
        // id and other properties
    
        @ManyToOne
        private School school;
    
        private Date creationDate;
    
        private String reference;
        // Getters and setters
    }
    
    2.储存库:

        @Repository
    public interface ClassRoomRepository extends JpaSpecificationExecutor<ClassRoom>{
    }
    
    @存储库
    公共接口ClassRoomRepository扩展了JpaSpecificationExecutor{
    }
    
    2.服务接口:

    public interface ClassRoomService {
    
    List<ClassRoom> list(String reference, String schoolName,
            Date creationDate)
    }
    
    公共接口ClassRoomService{
    列表(字符串引用、字符串学校名称、,
    日期(创建日期)
    }
    
    3.服务实施:

    import static yourpackage.ClassRoomSpecifications.*;
    import static org.springframework.data.jpa.domain.Specifications.*;
    @Service
    public class ClassRoomServiceImpl implements ClassRoomService {
    
        @Resource
        private ClassRoomRepository repository;
    
    
        @Override
        @Transactional(propagation = Propagation.SUPPORTS)
        public List<ClassRoom> list(String reference, String schoolName,
                Date creationDate) {
    
            Specifications<ClassRoom> spec = null;
            Specifications<ClassRoom> tempo = null;
    
            spec = where(findPerSchool(schoolName));
    
            if (reference != null) {
                tempo = where(findPerReference(reference));
            }
    
            if (creationDate!=null) {
                tempo = tempo == null ? where(findPerCreationDate(creationDate):tempo.and(findPerCreationDate(creationDate));
            }
            spec = tempo == null ? spec : spec.and(tempo);
            return repository.findAll(spec);
        }
    }
    
    导入静态yourpackage.ClassRoomSpecifications.*;
    导入静态org.springframework.data.jpa.domain.Specifications.*;
    @服务
    公共类ClassRoomServiceImpl实现ClassRoomService{
    @资源
    私有ClassRoomRepository;
    @凌驾
    @事务性(传播=传播.支持)
    公共列表(字符串引用、字符串学校名称、,
    日期(创建日期){
    规格规格=空;
    规格节拍=空;
    spec=其中(findPerSchool(学校名称));
    如果(引用!=null){
    tempo=其中(findPerReference(reference));
    }
    如果(creationDate!=null){
    tempo=tempo==null?其中(findPerCreationDate(creationDate):tempo.和(findPerCreationDate(creationDate));
    }
    spec=tempo==null?spec:spec.and(tempo);
    返回repository.findAll(spec);
    }
    }
    
    对于Spring数据,您只需使用存储库即可

       And  findByLastnameAndFirstname  … where x.lastname = ?1 and x.firstname = ?2
       Or   findByLastnameOrFirstname   … where x.lastname = ?1 or x.firstname = ?2
       Between  findByStartDateBetween  … where x.startDate between 1? and ?2
       LessThan findByAgeLessThan   … where x.age < ?1
       GreaterThan  findByAgeGreaterThan    … where x.age > ?1
       IsNull   findByAgeIsNull … where x.age is null
       IsNotNull,NotNull    findByAge(Is)NotNull    … where x.age not null
       Like findByFirstnameLike … where x.firstname like ?1
       NotLike  findByFirstnameNotLike  … where x.firstname not like ?1
       OrderBy  findByAgeOrderByLastnameDesc    … where x.age > ?1 order by x.lastname desc
       Not  findByLastnameNot   … where x.lastname <> ?1
    
    和findbylastname和firstname…其中x.lastname=?1和x.firstname=?2
    或FindBylastName或firstname…其中x.lastname=?1或x.firstname=?2
    FindBystartDateBeween…其中x.startDate介于1?和2之间
    LessThan findByAgeLessThan…其中x.age<?1
    GreaterThan FindBayagerathan…其中x.age>1
    IsNull findByAgeIsNull…其中x.age为null
    IsNotNull,NotNull findByAge(Is)NotNull…其中x.age不为null
    比如findByFirstnameLike…x.firstname在哪里?1
    不象FindByFirstName不象…x.firstname不象在哪里?1
    OrderBy FINDDBYAGEORDERBYLASTNAME desc…其中x.age>1 OrderBy x.lastname desc
    找不到BylastName不…x.lastname在哪里?1
    
    规范
    很难编写,而且非常冗长,就像你给我的链接中所说的那样。在查看示例时,我认为你应该在这里添加一个博客摘录,以防链接停止工作。顺便说一句,信息很棒。