首页 > 解决方案 > java - 如何在spring boot java中编写一个函数来处理JPA存储库中的自定义查询?

问题描述

基本上我使用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;

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

标签: javaangularspring-bootspring-data-jpajpa-2.1

解决方案


直接从客户端发送 SQL 将是一个完整的安全失败,因为恶意用户可以很容易地弄清楚它是如何工作的并发送一个delete from table来杀死你的整个应用程序。更糟糕的是,他们甚至可以运行create user ...以完全访问您的整个数据库、获取敏感信息、安装恶意软件等。

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

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

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


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


推荐阅读