具有动态where子句的Spring数据JPA [英] Spring data JPA with dynamic where clause

查看:380
本文介绍了具有动态where子句的Spring数据JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本存储库,例如IBaseRepository,即

I have a base Repository, say IBaseRepository which is

public interface IBaseRepository<T extends BaseEntity<PK>, PK extends Serializable>
      extends JpaRepository<T, PK>, JpaSpecificationExecutor<T>  {
}

现在,每个存储库类(例如UserRepository)都从该基本存储库扩展.我如何添加像

now every repository class, for example UserRepository extends from this base repository. How can I add a general method like

T findOne(String filter, Map<String, Object> params);

用于所有继承的类,以便调用

for all inherited classes so that calling

Map<String,Object> params = new HashMap<String,Object>();
params.put("username","Lord");
params.put("locked",Status.LOCKED);
userRepo.findeOne("username = :username AND status = :locked",params);

使用动态where子句向我返回一条记录.

return me a single record with dynamic where clause.

推荐答案

您可以执行以下

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;

import java.io.Serializable;
import java.util.Map;

/**
 * Created by shazi on 1/11/2017.
 */
@NoRepositoryBean
public interface IBaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

    T findOne(String filter, Map<String, Object> params);

}

并按以下步骤实施.

import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.Map;

/**
 * Created by shazi on 1/11/2017.
 */
public class BaseRepositoryImpl<T, ID extends Serializable>
        extends SimpleJpaRepository<T, ID> implements IBaseRepository<T, ID> {

    private final EntityManager entityManager;

    private final JpaEntityInformation entityInformation;

    public BaseRepositoryImpl(JpaEntityInformation entityInformation,
                            EntityManager entityManager) {
        super(entityInformation, entityManager);

        // Keep the EntityManager around to used from the newly introduced methods.
        this.entityManager = entityManager;
        this.entityInformation = entityInformation;
    }

    @Override
    public T findOne(String filter, Map<String, Object> params) {
        final String jpql = "FROM " + entityInformation.getEntityName() + " WHERE " + filter;
        Query query = entityManager.createQuery(jpql);
        for (Map.Entry<String, Object> value:params.entrySet()) {
            query.setParameter(value.getKey(), value.getValue());
        }
        return (T) query.getSingleResult();
    }
}

并按如下所示进行配置

@Configuration
@EnableJpaRepositories(repositoryBaseClass = BaseRepositoryImpl.class)
@EnableTransactionManagement
public class RepoConfig {

或XML

<repositories base-class="….BaseRepositoryImpl" />

最后,您可以按以下方式使用它;

Finally you can use it as follows;

User found = userRepository.findOne("name = :name", Collections.singletonMap("name", "name"));

但是您必须确保查询WHERE使得查询将始终仅返回1个结果.参见此帖子

But you have to make sure that your query WHERE is such that the Query will always return 1 result only. See this post

这篇关于具有动态where子句的Spring数据JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆