Spring Data JPA:使用@Query 中的属性作为参数 [英] Spring Data JPA: using property in @Query as parameter

查看:29
本文介绍了Spring Data JPA:使用@Query 中的属性作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 application-x.properties 文件用于不同的配置文件,每个文件都包含一个属性,每个配置文件都有一个特定的值.我想在对数据库的查询中使用此属性作为参数.

I have several application-x.properties files which are used for different profiles and each file contains a property with a specific value for each profile. I want to use this property in queries to database as a parameter.

是否可以使用 SpEL 或其他方式添加它?

Is it possible to add it using SpEL or something else?

例如application.properties:

query.parameters.role: ADMIN

可能的用法:

@Query(value = "select u from User u where u.role = :#{query.parameters.role}")
Set<User> getAllUsers();

推荐答案

您可以通过以下方式进行:

You might do it by following way:

1.- 使用 仓库查询关键字

@Repository
public interface UserRepository extends JpaRepository<User, UUID> {

    Set<User> findByRole(String role);
}

2.- 在UserService 中创建一个名为getAllUsers 的方法,并使用@Value:

2.- Create a method called getAllUsers in UserService and get role value by using @Value:

@Service
public class UserService {

    @Autowired
    private UserRepository repository;

    @Value("${query.parameters.role}")
    private String role;

    public Set<User> getAllUsers() {
        return repository.findByRole(role);
    }
}  

回答这个问题的其他方法是实现一个由 @Query 支持的自定义 SpEL 你可以看看这个 Spring Data JPA 中的 SpEL 支持

Other way to answer to this question is implement a custom SpEL that is supported by @Query you can take a look this SpEL support in Spring Data JPA

那么您应该针对您的案例执行以下步骤:

Then you should follow these steps for your case:

1.- 创建一个 ConfigProperties 类,以便您可以读取和获取 application.properties.

1.- Create a ConfigProperties class so that you can read and get the application.properties.

@Configuration
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix = "query")
public class ConfigProperties {

    private Parameters parameters;

    // standard getters and setters
}

public class Parameters {

    private String role;

    // standard getters and setters
}

2.- 实现自定义EvaluationContextExtensionSupport 并引用ConfigProperties

2.- Implement a custom EvaluationContextExtensionSupport and reference to ConfigProperties

public class PropertyEvaluationContextExtension extends EvaluationContextExtensionSupport {

    private final ConfigProperties configProperties;

    public PropertyEvaluationContextExtension(final ConfigProperties configProperties) {
        this.configProperties= configProperties;
    }
    @Override
    public String getExtensionId() {
        return "properties";
    }

    @Override
    public ConfigProperties getRootObject() {
        return this.configProperties;
    }
}

3.- 创建一个 bean 以调用您的自定义 PropertyEvaluationContextExtension

3.- Create a bean in order to be called your custom PropertyEvaluationContextExtension

@Configuration
public class CustomConfig {

    private final ConfigProperties configProperties;

    public CustomConfig(final ConfigProperties configProperties) {
        this.configProperties= configProperties;
    }

    @Bean
    EvaluationContextExtensionSupport propertyExtension() {
        return new PropertyEvaluationContextExtension(configProperties);
    }
} 

4.- 按以下格式调用 query.parameters.role:?#{query.parameters.role}

4.- Call the query.parameters.role by following format: ?#{query.parameters.role}

@Query(value = "SELECT u FROM User u WHERE u.role = ?#{query.parameters.role}")
Set<User> getAllUsers();

这篇关于Spring Data JPA:使用@Query 中的属性作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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