简体   繁体   中英

How to call a stored procedure from MyBatis Java?

I am getting the error:

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

and

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

when I call stored procedure from mybatis using Java.

I am using PostgreSQL as a database and a Spring MVC framework. For this, my DAO class calling the stored procedure is:

Orders orders=new Orders();

Values are set in orders variable programatically.

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

My mybatis file looks like this:

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

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

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.

Now it is working correctly.

Try to change your configuration from insert to 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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