繁体   English   中英

Spring Boot Java中如何在JPA存储库中编写处理自定义查询的函数?

[英]How to write a function to handle custom query in JPA repository in spring boot java?

基本上,我使用Spring Boot在服务器端托管数据库。 我想从客户端编写自定义查询,该查询以角度开发,并从服务器端调用一个函数以提供所需的结果。

服务器端的功能如下所示:

List<rows> func(String customQuery){
  //fetch rows from table using this custom query and return those rows
  //which I can use in client side.
}
Below are examples of customQuery which I need to send from client side:
select * from table;
select * from table where id>10;
select * from table where id>20 and id<30;

到目前为止,我在Internet上搜索时找不到任何解决方案。 请帮忙。

直接从客户端发送SQL会导致完全的安全失败,因为恶意用户可能会轻易弄清楚它的工作原理,并delete from table发送delete from table以杀死整个应用程序。 更糟糕的是,他们甚至可以运行create user ...来赋予自己对整个数据库的完全访问权限,获取敏感信息,安装恶意软件等。

相反,您将希望使用以下方法在应用程序中创建REST服务:

GET /table
GET /table?minId=10
GET /table?minId=20&maxId=30

application/json或类似的数据格式返回,仅返回您的角度应用真正需要的信息。 然后,Angular将负责使用您的数据有选择地更新显示。


编辑:这是我发现的基于Spring Boot和Angular创建基本Web应用程序的指南。 可能对您来说是一个很好的起点: https//www.baeldung.com/spring-boot-angular-web

最好的方法是仅从客户端发送参数 (在您的情况下为minIDmaxID ),然后使用Spring JPA Specification在服务器端动态构建查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM