简体   繁体   中英

How can I populate parameters map with result set in MyBatis

I'm reading a book "MyBatis in Practice" and can not figure why the code at page 67-68 doesn't work. Here is the problem. It's about calling stored procedures and it works but the real problem is that I can not figure out how to populate input map with result set. So here is the code from the main class:

public void callReadAllPets() throws Exception {
    HashMap<String, List<PetDVO>> inputMap = new HashMap<String, List<PetDVO>>();
    List<PetDVO> petList = new ArrayList<PetDVO>();
    inputMap.put("petData", petList);
    //
    getSqlSession().selectList("callReadAllPets", inputMap);
    List<PetDVO> outputData = inputMap.get("petData");
    printResultList(outputData, "read_all_pets");
}

private void printResultList(List<PetDVO> list) {
    for (PetDVO item : list) {
        System.out.println("     owner: " + item.getOwner());
        System.out.println("   species: " + item.getSpecies());
        System.out.println("       sex: " + item.getSex());
    }
}

PetDVO is just a Java POJO. Here is the code from mapper.xml

<select id="callReadAllPets" resultType="PetDVO" statementType="CALLABLE">
 CALL read_all_pets('SELECT name, owner, species, sex, birth, death FROM pet')

After running previous code, the outputData list is empty, ie unpopulated with results. This is the way suggested in the book but it doesn't work for me? How can I solve this? ps I'm using MyBatis 3.2.3

Ok, if you want to populate an input parameter with the OUT parameters of a stored procedure, you would do it like this...

<mapper namespace="com.example.SomeMapper">    
    <select id="doSomething" statementType="CALLABLE" parameterType="com.example.SomeRequest">
    { call someProcedure (
        #{someParameter,javaType=Long,jdbcType=NUMERIC,mode=IN},
        #{out,javaType=Long,jdbcType=NUMERIC,mode=OUT})
    }
    </select >
</mapper>

Your com.example.SomeRequest class would look like this...

class SomeRequest {

    private Long someParameter;
    private Long out;

    // add Getters & Setters

}

This should allow you to retrieve the (Long) result from the SomeRequest.out variable. To map more complex result, you would need to define them as ResultSet and supply the ResultMap, for example...

#{out,javaType=java.sql.ResultSet,jdbcType=CURSOR,resultMap=someResultMap,mode=OUT}

At least, that's how we do it with an Oracle database and package functions. Hopefully, it also works with MySql stored procedures the same way...

Thanks for your answer, but I'm using H2. There are no stored procedures in H2, only stored functions which are in fact ordinary java methods. In this case, stored function returns ResultSet and calling this function directly with the query 'SELECT name, owner, species, sex, birth, death FROM pet' produces the correct result. So, I believe that MyBatis is aware of a generated ResultSet but is not capable to put it back in the inputMap. To clarify this problem, I'll do two things. First, I'm gonna check if resultMap="petData" will work instead of resultType="PetDVO" , as petData is already a parameter of the inputMap. If that doesn't work, I'll assure myself that MyBatis is receiving the result set by using afore mentioned approach List<PetDVO> outputData = getSqlSession().selectList("callReadAllPets", inputMap) . Unfortunately, I'll be able to try it a few hours later. Thanks again @Florian Schaetz.

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