首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-data-jpa中标准的等价物

spring-data-jpa中标准的等价物
EN

Stack Overflow用户
提问于 2014-06-30 07:16:29
回答 3查看 16.3K关注 0票数 9

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

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

那么在spring-data-jpa中这个请求的等价物是什么呢?

代码语言:javascript
复制
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 ....
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-06-30 07:42:48

我是QueryDSL。在Here's上发表一篇博文,介绍如何在Spring Data中使用它。

票数 20
EN

Stack Overflow用户

发布于 2014-06-30 07:47:29

Spring Jpa数据中的等价物是Specification,您可以使用存储库SpecificationExecutor<T>和Jpa MetaModel来创建您的Jpa标准。

您可以找到关于Jpa JpaSpecificationExecutor的介绍。

一个简单的例子:

  1. Entity

@Entity

代码语言:javascript
复制
public class ClassRoom {
    // id and other properties

    @ManyToOne
    private School school;

    private Date creationDate;

    private String reference;
    // Getters and setters
}

2.存储库:

代码语言:javascript
复制
    @Repository
public interface ClassRoomRepository extends JpaSpecificationExecutor<ClassRoom>{
}

2.服务接口:

代码语言:javascript
复制
public interface ClassRoomService {

List<ClassRoom> list(String reference, String schoolName,
        Date creationDate)
}

3.服务实现:

代码语言:javascript
复制
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);
    }
}
票数 6
EN

Stack Overflow用户

发布于 2014-06-30 16:04:49

对于Spring data,您只需使用存储库。

代码语言:javascript
复制
   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
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24481095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档