设为首页 收藏本站
查看: 1472|回复: 0

[经验分享] mybatis/hibernate 调用存储过程 返回游标 实例

[复制链接]

尚未签到

发表于 2016-11-27 08:13:56 | 显示全部楼层 |阅读模式
在网上找了很久的mybatis调用存储过程,并返回游标示例,例子很多,但是都描述不怎么清楚,通过多次探索,终于得出了得到完美的代码。希望对大家有一些帮助
存储过程示例:
create or replace procedure Fsp_Plan_CheckPrj(v_grantno  varchar2, v_deptcode number,  v_cursor   out sys_refcursor) is
………………
    ---返回统计结果
    open v_Cursor for
      select s.plan_code,
             s.plan_dept,
             s.plan_amount,
             s.exec_amount,
             p.cname       as plan_name,
             d.cname       as dept_name
        from Snap_plan_checkprj s
        left join v_plan p
          on s.plan_code = p.plan_code
        left join org_office d
          on s.plan_dept = d.off_org_code
       group by s.plan_code,
                s.plan_dept,
                s.plan_amount,
                s.exec_amount,
                p.cname,
                d.cname;
  end;
end Fsp_Plan_CheckPrj;

mybatis:(mybatis doc api:  http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html#Result_Maps)
java层代码
Map<String, Object> params = new HashMap<String, Object>();
GrantSetting gs = this. grantSettingDao.get(grantCode);
params.put( "grantNo", StringUtils. substring(gs.getGrantNo(), 0, 2));
params.put( "offOrgCode", SecurityUtils.getPersonOffOrgCode());
params.put("v_cursor", new ArrayList<Map<String, Object>>());//传入一个jdbc游标,用于接收返回参数
this. batisDao. getSearchList("call_Fsp_Plan_CheckPrj", params);
return params;
mybatis xml配置
<resultMap type ="java.util.HashMap" id= "cursorMap"><!--配置返回游标中别名对应的resultMap -->
<result column ="plan_code" property="plan_code" />
<result column ="plan_dept" property="plan_dept"  />
<result column ="plan_amount" property="plan_amount" />
<result column ="exec_amount" property="exec_amount"  />
<result column ="plan_name" property="plan_name" />
<result column ="dept_name" property="dept_name" />
</resultMap >
<select id ="call_Fsp_Plan_CheckPrj" parameterType= "map" statementType="CALLABLE" >
<!--注明statementType="CALLABLE"表示调用存储过程-->
{call Fsp_Plan_CheckPrj(#{grantNo, jdbcType=VARCHAR, mode=IN},
#{offOrgCode, jdbcType=INTEGER, mode=IN},
#{v_cursor, mode=OUT, jdbcType=CURSOR, resultMap=cursorMap})}
<!--传入传出参数要注明mode=IN/OUT 并要注明jdbcType(在网上可以查询mybatis支持哪些jdbcType类型),返回参数要注明对应的resultMap-->
</select >


最后,在jsp页面只需遍历
params.put( "v_cursor", OracleTypes. CURSOR);中的v_cursor。本身就是一个可遍历的list结果集

hibernate
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Connection con = this.getSession().connection();
CallableStatement sp;
try {
sp = con.prepareCall("{call Fsp_Plan_CheckPrj(?,?,?)}");
sp.setString(1, ObjectUtils.toString(params.get("grantNo")));
sp.setLong(2, NumberUtils.toLong(ObjectUtils.toString(params.get("offOrgCode"))));
sp.registerOutParameter(3, OracleTypes.CURSOR);
sp.execute(); // 执行存储过程
ResultSet rs = (ResultSet) sp.getObject(3); // 获取返回的对象,再将对象转为记录集  3代表哪个参数
while (rs.next()) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("plan_code", ObjectUtils.toString(rs.getObject(1)));
resultMap.put("plan_dept", ObjectUtils.toString(rs.getObject(2)));
resultMap.put("plan_amount", IrisStringUtils.FormatMoney(ObjectUtils.toString(rs.getObject(3))));
resultMap.put("exec_amount", IrisStringUtils.FormatMoney(ObjectUtils.toString(rs.getObject(4))));
resultMap.put("plan_name", ObjectUtils.toString(rs.getObject(5)));
resultMap.put("dept_name", ObjectUtils.toString(rs.getObject(6)));
list.add(resultMap);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305960-1-1.html 上篇帖子: spring+mybatis多数据源及事物配置 下篇帖子: spring mvc + mybatis配置多个数据源问题。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表