在 spring 引导中创建和执行存储过程

Creating and executing a stored procedure in spring boot

我在 sql 文件中编写了一个存储过程,我想使用 jdbc 模板通过 spring 引导代码执行该过程。存储过程如下:

CREATE OR REPLACE FUNCTION DELETE_REDUNDANT_RECORDS()
RETURNS void AS
$func$
DECLARE
    interval_time            BIGINT DEFAULT 0;
    min_time                 BIGINT DEFAULT 0;
    max_time                 BIGINT DEFAULT 0;
    rec_old                  RECORD;
    rec_new                  RECORD;
    rec_start                RECORD;
    v_filename               VARCHAR(250);
    v_systemuid              VARCHAR(50);
    is_continous_record      BOOLEAN default false;

    cursor_file CURSOR FOR
        select distinct filename,systemuid from ASP.MONITORING_BOOKMARK_ORIGINAL;
    cursor_data CURSOR FOR
        select * from ASP.MONITORING_BOOKMARK_ORIGINAL where filename = v_filename and systemuid=v_systemuid order by mindatetime, maxdatetime;

BEGIN
    --open the file cursor to fetch the filename and systemuid
    OPEN cursor_file;
    -- logic for procedure
    CLOSE cursor_file;
END;
$func$
LANGUAGE plpgsql;

我已将其添加到我的 spring 引导项目的 sql 文件中。我想创建一个调度程序来使用 spring jdbc 创建和执行此存储过程。使用的数据库是 Postgres。有没有办法做到这一点。我有调用过程的参考,但我需要的是创建和执行过程。

对于 运行 程序,Spring 的 SimpleJdbcCall 可能就是您要查找的内容。 它的使用应该相当简单:

  1. 创建一个提供 DataSource
  2. 的实例
  3. 根据需要调用 withCatalogNamewithProcedureName
  4. 根据需要致电declareParameters
  5. 调用 execute 到 运行 过程

要创建它,JdbcTemplate 和它自己的 execute 方法就足够了。

请注意,您可能只想创建一次该函数,然后再 运行 它。创建和 运行 将过程同时作为某些计划操作的一部分是一个非常不常见的要求。如果可能,您可能希望每次都重新评估创建过程的必要性。