您的位置:首页 > 其它

MyBatis调用存储过程返回结果集

2016-06-09 02:53 393 查看
存储过程:

create or replace procedure get_result_by_sql(p_sql in varchar2,p_result out sys_refcursor,p_msg out varchar2) is
begin
open p_result for p_sql;
p_msg := '执行SQL成功了';
EXCEPTION
WHEN OTHERS THEN
p_msg := '执行SQL出错了';
end get_result_by_sql;


Mbatis配置:

<resultMap id="getResultBySQLMap" type="com.hotesion.pojo.testVO">
<result column="id" jdbcType="DECIMAL" property="id" />
<result column="key" jdbcType="VARCHAR" property="key" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<select id="getResultBySQL1" statementType="CALLABLE">
{call get_result_by_sql(
#{sql,jdbcType=VARCHAR,mode=IN},
#{result,mode=OUT,jdbcType=CURSOR,javaType=ResultSet,resultMap=getResultBySQLMap},
#{msg,mode=OUT,jdbcType=VARCHAR,javaType=String})}
</select>

<select id="getResultBySQL2" statementType="CALLABLE">
{call get_result_by_sql(
#{sql,jdbcType=VARCHAR,mode=IN},
#{result,mode=OUT,jdbcType=CURSOR,javaType=ResultSet,resultType=java.util.LinkedHashMap},
#{msg,mode=OUT,jdbcType=VARCHAR,javaType=String})}
</select>


Java调用:

public List getResultBySQL(String sql){
Map<String,Object> map=new HashMap<String, Object>();
map.put("sql", sql);
//
testDao.getResultBySQL1(map);
System.err.println("1====="+map.get("msg"));
List list1 = (List)map.get("result");
//或者
testDao.getResultBySQL2(map);
System.err.println("2====="+map.get("msg"));
List list2 = (List)map.get("result");;
return list1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MyBatis