您好,我目前正在尝试执行自定义查询,可以通过我的存储库层中的参数输入该查询。
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface LinuxServerTaniumReportRepository extends MongoRepository<LinuxServerTaniumReport, String> {
@Query(:query)
public List<LinuxServerTaniumReport> performQuery(String query);
}
我该怎么办?
最佳答案
您无法通过注释来执行此操作,但可以通过提供自定义存储库来执行此操作。大约是这样的:
public interface MyCustomExecuteMethod<T> extends Repository {
public List<T> performQuery(String query);
}
public class MyCustomExecuteMethodProvider<T> implements MyCustomExecuteMethod {
@Autowire
EntityManager em;
public List<T> performQuery(String query) {
// put your logic here where you create the query via the entity manager
}
}
Repository
public interface LinuxServerTaniumReportRepository extends MongoRepository<LinuxServerTaniumReport, String> ,MyCustomExecuteMethod<LinuxServerTaniumReport>{
}
现在,要获取此方法,您唯一需要做的就是使用 MyCustomExecuteMethod 接口(interface)扩展您的存储库。 这项技术称为 Spring Data 可组合存储库,它在 Spring Data 文档中的 Spring Data Repositories 的 Custome Implementing https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations 章节中进行了介绍。
关于java - 如何根据参数输入执行自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56675520/