在网上找了很久的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;
分享到:
相关推荐
2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构...
2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构...
能不能写个动态的业务,只输入存储过程名称,自动获取存储过程参数,并且参数的数据从前台传递过来...只根据输入不同的存储过程名称、参数内容,自动调用不同的存储过程。 已经使用在多个项目中 全开源项目 请放心下载
OA系统-springMVC/Mybatis/Mysql/Bootstrap OA系统-springMVC/Mybatis/Mysql/Bootstrap OA系统-springMVC/Mybatis/Mysql/Bootstrap
源码|Bootstrap、jQuery、HTML5、SpringMVC、Hibernate、Mybatis admin管理平台模块,你值得拥有
解决mybatis头文件报错 下载好压缩包 解压将文件放到本地文件夹 例如 D盘的哪个文件夹 D:\mybatis\ ;然后打开eclipse ->Window->prefenrence->XML->XML Catalog->User Specifiled Entreis->Add->Location(此处是你...
mybatis调用orclae存储过程
springboot(4) 整合mybatis和hibernate 请删除项目中本地仓库的配置,或者改成国内的公共仓库,否则将无法启动
这是mybatis调用存储过程的代码
代码生成平台controller/service/dao/mybatis/model
项目使用Spring4+SpringMVC+mysql【同时支持mybatis+hibernate两种底层操作框架】+memcached
全注解零配置Spring/SpringMVC/Mybatis/Swagger,工程里有初始化脚本。
mybatis/ibatis自动生成sqlmapper的脚本
通过sql来映射实体,dao,service等等增删改查基础方法,节约重复代码的时间,提高代码品质效率,适用框架JPA/JdbcTemplate/Mybatis/MybatisPlus
赠送jar包:mybatis-3.5.9.jar; 赠送原API文档:mybatis-3.5.9-javadoc.jar; 赠送源代码:mybatis-3.5.9-sources.jar; 赠送Maven依赖信息文件:mybatis-3.5.9.pom; 包含翻译后的API文档:mybatis-3.5.9-javadoc-...
主要介绍了SpringMVC与Mybatis集合实现调用存储过程、事务控制实例,有需要的可以了解一下。
使用maven+springMVC+mybatis编写的项目例子,如果有需要的可以下载,里面还用到了mybatis调用储存过程。
MyBatis调用MYSQL存储过程 返回多行 引用的包: asm-3.1.jar cglib-2.2.jar commons-logging-1.1.1.jar log4j-1.2.13.jar mybatis-3.0.4.jar mysql-connector-java-5.1.13.jar slf4j-api-1.5.8.jar slf4j-log4j12-...
主要介绍了mybatis调用存储过程的实例,非常不错,具有参考借鉴价值,需要的朋友可以参考下
struts2介绍、和springmvc对比,mybatis介绍和hibernate对比