How to call a stored procedure from MyBatis Java?

I get an error message:

org.mybatis.spring.MyBatisSystemException: nested exception - org. Apache. Ibatis. exceptions.PersistenceException:

and

java.lang.IllegalArgumentException: The Mapped Statement collection does not contain a value for .. "

when I call a stored procedure from mybatis using Java.

I use PostgreSQL as a database and Spring MVC framework. To do this, my DAO class calling the stored procedure is as follows:

Orders orders=new Orders(); 

The values ​​are set in the order variable programmatically.

 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:

 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 transfer parameters:

  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 } 
+4
source share
2 answers

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

Since I use the Spring framework, so due to some transitive dependency, my stored procedure is not being called from MyBatis in java.

So, I checked my pom.xml file to look for a transitive dependency, and then excluded all these dependencies for MyBatis and used the latest version of MyBatis for Spring.

Now it works correctly.

+1
source

Try changing the 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> 
0
source

Source: https://habr.com/ru/post/1432204/


All Articles