如何从 MyBatis Java 调用存储过程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12257921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:12:09  来源:igfitidea点击:

How to call a stored procedure from MyBatis Java?

javaspringstored-proceduresmybatis

提问by Ruju

I am getting the error:

我收到错误:

org.mybatis.spring.MyBatisSystemException: nested exception is org. apache. ibatis. exceptions.PersistenceException:

org.mybatis.spring.MyBatisSystemException: 嵌套异常是 org. 阿帕奇。伊巴蒂斯。异常.PersistenceException:

and

java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for .."

java.lang.IllegalArgumentException: Mapped Statements 集合不包含 .. 的值”

when I call stored procedure from mybatis using Java.

当我使用 Java 从 mybatis 调用存储过程时。

I am using PostgreSQLas a database and a Spring MVCframework. For this, my DAO class calling the stored procedure is:

我使用PostgreSQL作为数据库和Spring MVC框架。为此,我调用存储过程的 DAO 类是:

Orders orders=new Orders();

Values are set in orders variable programatically.

值以编程方式按顺序变量设置。

Integer insert= getSqlSession().insert("records",orders);**

My mybatis file looks like this:

我的 mybatis 文件如下所示:

<insert id="records" parameterType="Orders" statementType="CALLABLE">
 {call fn_records_tbl(#{rId,javaType=Integer,jdbcType=INTEGER,mode=IN},#{state,javaType=String,jdbcType=CHAR,mode=IN},#{uId,javaType=Integer,jdbcType=INTEGER,mode=IN},#{status,javaType=String,jdbcType=CHAR,mode=IN})}
</insert>

My stored procedure syntax is:

我的存储过程语法是:

CREATE OR REPLACE FUNCTION fn_records_tbl(rId integer, state character,uId integer, status character)

RETURNS void AS

$BODY$
DECLARE

    -- my code
BEGIN

    -- my code
END

$BODY$

LANGUAGE plpgsql VOLATILE
COST 100;

ALTER FUNCTION fn_records_tbl(integer, character, integer, character)
OWNER TO mydba;

and my entity class for passing parameters is:

我用于传递参数的实体类是:

 public class Orders implements Serializable {

  private static final long serialVersionUID = 267216928694677437L;
  private Integer uId;
  private Integer rId;
  private String status;
  private String state;

     // here are my setter and getter
 }

采纳答案by Ruju

My syntax for calling stored procedure is correct both in MyBatis and in stored procedure.

我调用存储过程的语法在 MyBatis 和存储过程中都是正确的。

As I am using Spring framework so due to some transitive dependency my stored procedure is not calling from MyBatis in java.

由于我使用的是 Spring 框架,因此由于某些传递依赖关系,我的存储过程不会从 Java 中的 MyBatis 调用。

So I checked my pom.xml file for finding transitive dependency and then I exclude all those dependencies for MyBatis and uses the recent version of MyBatis for spring framework.

因此,我检查了我的 pom.xml 文件以查找传递依赖项,然后我排除了 MyBatis 的所有这些依赖项,并使用了 Spring 框架的最新版本的 MyBatis。

Now it is working correctly.

现在它工作正常。

回答by Iswanto San

Try to change your configuration from insertto select

尝试将您的配置从 更改insertselect

<select id="records" parameterType="Orders" statementType="CALLABLE">
 {call fn_records_tbl(#{rId,javaType=Integer,jdbcType=INTEGER,mode=IN},#{state,javaType=String,jdbcType=CHAR,mode=IN},#{uId,javaType=Integer,jdbcType=INTEGER,mode=IN},#{status,javaType=String,jdbcType=CHAR,mode=IN})}
</select>