Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyBatis mapper to call a PROC with multiple IN parameters

I'm trying to design a small CRUD tool, and so far every facet (the Rich Faces UI and Managed Beans, validation, the mySQL database, etc.) is going fine, but not the myBatis piece.

I'm relatively new to myBatis and am keeping the users guide and API close at hand, but there are still some things that just won't come together for me, and one is any call to a procedure involving multiple IN parameters. Here is an example:

This from the DB set up scripts:

create procedure MY_FOO_PROC (IN valA VARCHAR(15), IN valB CHAR(1))
    begin
        select blah from blah where blah = valA and blah = valB etc.;
    end 

This from MyMapper.java:

public interface MyMapper {
List<MyFooClass> getProgress (
        @Param("valA") String valueA, @Param("valB") String valueB);
}

This from MyMapper.xml:

<select id="getProgress" parameterType="map" 
    resultMap="MyFooMap" statementType="CALLABLE">
    { call MY_FOO_PROC (
        #{valA, mode=IN, jdbcType=VARCHAR}
        #{valB, mode=IN, jdbcType=CHAR}
    )}
</select>

And finally this from my DAO class:

public static List<MyFooClass>
        doGetProgress (String valueA, String valueB) {
    SqlSession session = MyBatisConnectionFactory.getInstance().getSqlSessionFactory().openSession();
    EsparMapper mapper = session.getMapper(MyMapper.class);
    List<MyFooClass> listFoo = mapper.getProgress(valueA, valueB);  // line which originates exception below
    session.close();
    return listFoo;
}

The result:

### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Incorrect number of arguments for PROCEDURE dbname.MY_FOO_PROC; expected 2, got 1
### The error may involve my.package.names.getProgress-Inline
### The error occurred while setting parameters

I will note that I have also tried:

  1. creating a POJO with variables valA and valB and getters/setters for each,
  2. making parameterType="PojoClass" in the XML,
  3. skipping the session.getMapper() and creating an instance of PojoClass,
  4. and calling session.selectList("getProgress", pojoInstance);

with the nearly identical result (i.e. wrong number of arguments).

Very little help on net search, most telling me to do what I think I have already done.

like image 337
cobaltduck Avatar asked Dec 24 '11 20:12

cobaltduck


People also ask

How do I call a stored procedure in MyBatis?

Unlike IBATIS, there is no <procedure> tag in MyBatis. To map the results of the procedures, we have created a resultmap named Student and to call the stored procedure named read_recordById. We have defined a select tag with id callById, and we use the same id in the application to call the procedure.

Is MyBatis faster than JDBC?

For an easiest query. Not considering the first query, for the following queries, JDBC always takes about 6ms~10ms, and MyBatis takes about 31~35ms, which is about 3 times. For a more complex query (6 inner joins and an order by in it), JDBC only takes 25~30ms, while MyBatis needs 80~100ms.

Can I pass a list as a parameter to a MyBatis Mapper?

NOTE You can pass any Iterable object (for example List, Set, etc.), as well as any Map or Array object to foreach as collection parameter.


1 Answers

I think you're missing a comma in the procedure call.

<select id="getProgress" parameterType="map" 
    resultMap="MyFooMap" statementType="CALLABLE">
    { call MY_FOO_PROC (
        #{valA, mode=IN, jdbcType=VARCHAR} , --<--- this
        #{valB, mode=IN, jdbcType=CHAR}
)} 
</select>
like image 70
pablochan Avatar answered Sep 19 '22 02:09

pablochan