HOW TO pass List of objects(a DTO) as single IN parameter to Stored Procedure

戏子无情 提交于 2021-02-07 07:20:31

问题


I want to pass Dto as in parameters and call stored procedure in spring jdbc.Is it possible,In doing so?

I want to call stored procedures with dto as in paratmeters instead of setting parameters?Because I have large number of parameters.


回答1:


At the moment, there is no way to pass (or return) objects in MySQL stored procedures and functions.

BUT, MySQL 5.7 have JSON functions, you can pass a varchar parameter and extract values using JSON_EXTRACT function.

See MySQL 5.7 manual: Functions That Search JSON Values




回答2:


I was able to do this in MySQL 5.7

If you are trying to variable which key to look for I would do this

SET TypeCat = CONCAT(CONVERT('$.', CHAR(50)), T)

The reason why is Mysql thinks '$.' is a double so I had to cast it as a Char

DROP PROCEDURE IF EXISTS web.newTransaction;

CREATE PROCEDURE web.newTransaction (DATA JSON)
  BEGIN


  ## Declare Vars ###
  DECLARE done INT DEFAULT FALSE;

  DECLARE TypeCat VARCHAR(255);

  DECLARE s JSON;

  # CURSOR 1
  DECLARE T VARCHAR(255);   ## Transaction
  DECLARE TVT VARCHAR(255); ## TransactionValueTable
  DECLARE cur1 CURSOR FOR SELECT  Transaction, TransactionValueTable FROM  web.TransactionType;




  ##### Error Handlers ####

  ### Error Handle for Loop ##

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;

  DECLARE exit handler for sqlexception
    BEGIN
      -- ERROR
      SHOW ERRORS;
    ROLLBACK;
  END;

  DECLARE exit handler for sqlwarning
   BEGIN
      -- WARNING
     SHOW WARNINGS;
   ROLLBACK;
  END;



  OPEN cur1;

  START TRANSACTION;
  -- Start our for loop
  forLoop: LOOP

      FETCH cur1 INTO T,TVT;
      IF done = TRUE THEN
        LEAVE forLoop;
      END IF;

      SET TypeCat = CONCAT(CONVERT('$.', CHAR(50)), T);


      IF (SELECT JSON_CONTAINS_PATH(DATA, 'one',  TypeCat) = 1)
      THEN SET s =JSON_EXTRACT(DATA, TypeCat);
      END IF;



    -- End our for loop
    END LOOP forLoop;


  COMMIT;

    SELECT s;
END;

call web.newTransaction('{"Owner": "user","Title": "Fix Function Foo" }');

https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html



来源:https://stackoverflow.com/questions/43796685/how-to-pass-list-of-objectsa-dto-as-single-in-parameter-to-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!