ibatis调用oracle存储过程返回结果集的问题
xml代码
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD iBatis Mapper 3.0 //EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="org.yhb.ibatis.dao.UserDAO">
<!-- 表结构
create table IBATIS_USER
(
ID NUMBER not null,
NAME VARCHAR2(20) not null,
BRITHDAY DATE not null
)
-->
<!-- 存储过程
create or replace procedure getAllUser(userList out sys_refcursor)
as
begin
open userList for select * from ibatis_user;
end;
-->
<!-- resultMap -->
<resultMap type="org.yhb.ibatis.model.User" id="userMap">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="birthday" property="birthday" />
</resultMap>
<!-- 调用存储过程 -->
<select id="getAllUser" statementType="CALLABLE" resultMap="userMap">
{call getAllUser(#{userList,mode=OUT,javaType=java.sql.ResultSet,jdbcType=CURSOR,resultMap=userMap})}
</select>
</mapper>
java调用代码
public void testProcedure() throws Exception {
Reader reader = null;
reader = Resources.getResourceAsReader("SqlMapper.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
reader.close();
SqlSession session = ssf.openSession();
Map map = new HashMap();
session.selectOne("org.yhb.ibatis.dao.UserDAO.getAllUser", map);
System.out.println(map);
//返回的集合被放入了map中
List<User> userList = (List<User>) map.get("userList");
System.out.println(userList);
session.close();
}
错误代码:
org.apache.ibatis.exceptions.IbatisException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Result Maps collection does not contain value for userMap
### The error may exist in user.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: {call getAllUser(?)} 存储 Oracle iBATIS 结果集
[解决办法]
我基本上直接用你的代码测试,但是我的没错。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="sex">
<!-- 用别名指向Java里属性 数据库字段正常写 -->
<select id="querySex" resultType="java.util.HashMap" parameterType="java.lang.String">
select * from sex
</select>
<resultMap id="sexxx" type="com..xml.Sex">
<id column="ct_id" property="ct_id" />
<result column="code" property="code" />
<result column="codename" property="codename" />
<result column="fathercode" property="fathercode" />
</resultMap>
<parameterMap id="sexxxmap" type="java.util.HashMap">
<parameter property="sexList" mode="OUT" jdbcType="CURSOR" javaType="java.sql.ResultSet" resultMap="sexxx"/>
</parameterMap>
<select id="querySexPro" parameterMap="sexxxmap" statementType="CALLABLE">
{call bql.pro_query_sex(?)}
</select>
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlMapFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlMapFactory.openSession();
//1 常规方法 找sqlKey
Map map = new HashMap();
session.selectOne("sex.querySexPro", map);
System.out.println(map);
//返回的集合被放入了map中
List<Sex> sexList = (List<Sex>) map.get("sexList");
System.out.println(sexList);
session.close();
[解决办法]
可能是你的版本问题
ibatis应该只有2,3就是mybatis了
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">