【问题标题】:Creating and executing a stored procedure in spring boot在 Spring Boot 中创建和执行存储过程
【发布时间】:2019-12-04 16:02:00
【问题描述】:

我有一个用 sql 文件编写的存储过程,我想使用 jdbc 模板通过 Spring Boot 代码执行该存储过程。存储过程是这样的:

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 boot 项目的一个 sql 文件中添加了这个。我想创建一个调度程序来使用 spring jdbc 创建和执行这个存储过程。使用的数据库是 Postgres。有没有办法做到这一点。我有调用过程的参考资料,但我需要的是创建和执行过程。

【问题讨论】:

  • 将代码放在一个字符串中,然后使用Statement.execute(String)运行它
  • 这也可以使用 jdbc 模板来完成吗?

标签: postgresql spring-boot stored-procedures spring-jdbc


【解决方案1】:

对于运行该过程,Spring 的SimpleJdbcCall 可能是您正在寻找的。 它的使用应该相当简单:

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

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

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

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2022-06-14
    • 1970-01-01
    • 2022-01-13
    • 2010-09-15
    • 1970-01-01
    相关资源
    最近更新 更多