简体   繁体   English

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

[英]How to call a stored procedure from MyBatis Java?

I am getting the error: 我收到错误:

org.mybatis.spring.MyBatisSystemException: nested exception is org. org.mybatis.spring.MyBatisSystemException:嵌套异常是org。 apache. 阿帕奇。 ibatis. ibatis的。 exceptions.PersistenceException: exceptions.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 PostgreSQL as a database and a Spring MVC framework. 我使用PostgreSQL作为数据库和Spring MVC框架。 For this, my DAO class calling the stored procedure is: 为此,我的DAO类调用存储过程是:

Orders orders=new Orders();

Values are set in orders variable programatically. 值以编程方式设置为order变量。

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
 }

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的所有依赖项,并使用最新版本的MyBatis for spring framework。

Now it is working correctly. 现在它正常工作。

Try to change your configuration from insert to select 尝试将配置从insert更改为select

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM