java - 如何从参数输入中进行自定义查询
问题描述
您好我目前正在尝试进行自定义查询,该查询可以通过我的存储库层中的参数输入。
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);
}
我应该怎么做?
解决方案
You can not do this via Annotation, but you can do this by providing a custom repository. It is something around this lines:
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>{
}
Now the only thing you need to get this method is to extend your repositories with the interface MyCustomExecuteMethod. This technic is called spring data composable repositories it is covered in spring data documentation under chapter Custome Implementation of spring data repositories https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations
推荐阅读
- reactjs - bit-src 没有显示 react bootstrap 的风格
- google-apps-script - 如何使用公式从单元格中获取值?
- c# - 强制 nuget 忽略目标框架冲突的包
- python - python 3.7中的self和/是什么意思?
- typescript - 如何根据高阶函数的静态字符串值推断类型?
- python - 使用带有 SNS 和 EMR 客户端的 lambda 中的 boto3 来获取集群的详细信息并将其发送到邮件
- ios - 如何在 MKMapSnapshotter 上绘制 CLLocationCoordinate2Ds(在 mapView 打印图像上绘制)
- typescript - 接口结构相似的联合类型
- postgresql - 创建触发器来检查状态,
- python-3.x - Dask 客户端无法连接到 dask-scheduler