spring jdbctemplate调用存储过程,返回list对象

注:本文来源于《  spring jdbctemplate调用存储过程,返回list对象

spring jdbctemplate调用存储过程,返回list对象

方法:



/**
	 * 调用存储过程
	 * @param spName
	 */
	@SuppressWarnings("unchecked")
	public List<HashMap<String, Object>> executeSP(String procedure) {
		//procedure = "{call WCITY2_STATISTIC.sp_uservisit_stat(?)}";
		return (List<HashMap<String, Object>>) jdbcTemplate.execute(procedure,
				new CallableStatementCallback() {
					public Object doInCallableStatement(
							CallableStatement cs) throws SQLException,
							DataAccessException {

						List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

						cs.registerOutParameter(1, OracleTypes.CURSOR);
						cs.execute();
						ResultSet rs = (ResultSet) cs.getObject(1);

						while (rs.next()) {
							HashMap<String, Object> dataMap = new HashMap<String, Object>();
							ResultSetMetaData rsMataData = rs.getMetaData();
							for (int i = 1; i <= rsMataData.getColumnCount(); i++) {
								dataMap.put(rsMataData.getColumnName(i), rs
										.getString(rsMataData.getColumnName(i)));
							}
							list.add(dataMap);
						}

						return list;
					}
				});

	}

存储过程:



create or replace package WCITY2_STATISTIC is

  -- Author  : ADMINISTRATOR
  -- Created : 2012/10/24 9:48:34
  -- Purpose : 

  type Ref_Cursor is ref cursor;

  --
  procedure sp_pager_stats;

  --访问信息
  procedure sp_uservisit_stat(c_uservisit out Ref_Cursor);

end WCITY2_STATISTIC;

create or replace package body WCITY2_STATISTIC is

  --页面信息
  procedure sp_pager_stats is

  begin
    -- cur_page as select * from OMS_WIRELESS. TEMPLATE_FILE_WORKING;

    null;
  end sp_pager_stats;

  --访问信息
  procedure sp_uservisit_stat(c_uservisit out Ref_Cursor) as
    --定义游标
    /*
    cursor c_uservisit is
    select t.city,t.username,t.username as telphone,‘‘ as ip
    from INTERFACE_WIRELESS.USER_LOGIN_LOG t ;*/
  begin
    open c_uservisit For

      select t.city, t.username, t.username as telphone, ‘‘ as ip
        from INTERFACE_WIRELESS.USER_LOGIN_LOG t;

  end;

end WCITY2_STATISTIC;

调用方法:

	@SuppressWarnings("rawtypes")
	public String getUserVisitStat(){
		//List lst=jdbcService.executeSP("");

		String procedure = "{call WCITY2_STATISTIC.sp_uservisit_stat(?)}";
		List lst=spService.executeSP(procedure);
		if(lst!=null){
			System.out.println(lst.size());
		}

		return SUCCESS;
	}

自己编写的代码


import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;

import com.tt.pwp.framework.data.dao.DaoFactory;

import oracle.jdbc.OracleTypes;

public class FuncReportTaskService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DaoFactory daoFactory;

    /**
     * 调用存储过程
     * @param spName
     */
    @SuppressWarnings("unchecked")
    public List<HashMap<String, Object>> executeSP(String procedure) {
    	JdbcTemplate jdbcTemplate = daoFactory.getDao().getJdbcTemplate();
        //procedure = "{call WCITY2_STATISTIC.sp_uservisit_stat(?)}";
        return (List<HashMap<String, Object>>) jdbcTemplate.execute(procedure,
                new CallableStatementCallback() {
                    public Object doInCallableStatement(
                            CallableStatement cs) throws SQLException,
                            DataAccessException {

                        List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

                        cs.registerOutParameter(1, OracleTypes.CURSOR);
                        cs.execute();
                        ResultSet rs = (ResultSet) cs.getObject(1);

                        while (rs.next()) {
                            HashMap<String, Object> dataMap = new HashMap<String, Object>();
                            ResultSetMetaData rsMataData = rs.getMetaData();
                            for (int i = 1; i <= rsMataData.getColumnCount(); i++) {
                                dataMap.put(rsMataData.getColumnName(i), rs
                                        .getString(rsMataData.getColumnName(i)));
                            }
                            list.add(dataMap);
                        }

                        return list;
                    }
                });

    }
}

原文地址:https://www.cnblogs.com/ios9/p/9370825.html

时间: 2024-11-05 20:28:02

spring jdbctemplate调用存储过程,返回list对象的相关文章

Java调用存储过程返回数组

Java调用存储过程: 结合SQL操作与存储过程 create procedure set_death_age(poet VARCHAR2, poet_age NUMBER) poet_id NUMBER; begin SELECT id INTO poet_id FROM poets WHERE name = poet; INSERT INTO deaths (mort_id, age) VALUES (poet_id, poet_age); end set_death_age; 下面是调用上

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.cnam

Oracle调用存储过程返回游标结果

/** 调用Oracle存储过程 */ public ResultSet queryForAllJSD_NEWEST(final String hpCode, final String id) { System.out.println("hpCode:"+hpCode+",id:"+id); ResultSet rs = (ResultSet)this.jdbctemplate.execute("{call pro_querybcb_newest(? ,

hibernate 调用存储过程返回参数

Connection conn= getSession().connection(); CallableStatement cs=null; //指定调用的存储过程  cs = conn.prepareCall("{Call PRO_PROCESSING_COMMON.GET_STR(?,?)}");  //设置参数  cs.setString(1, "TESTSTR");  //这里需要配置OUT的参数新型  cs.registerOutParameter(2,

webservice接口调用存储过程返回失败

poka.cashman.timer.service.impl.PdaOperateServiceImpl - Method Name: cashBoxOutOrIn; cbInfo:JN00201503170;;0;;;;;;902000000|JN00201503170;;0;;;;;;902000000|JN00201503170;;0;;;;;;902000000|JN00201503170;;0;;;;;;9020000002015-03-18 10:37:19,863 [WebCon

Spring Data Jpa 查询返回自定义对象

转载请注明出处:http://www.wangyongkui.com/java-jpa-query. 今天使用Jpa遇到一个问题,发现查询多个字段时返回对象不能自动转换成自定义对象.代码如下: //User 实体对象 @Entity @Table(name="t_user") public class User { private Integer id; private String name; public Integer getId() { return id; } public

spring heibernate 调用存储过程

一:参考网址 http://sunbin123.iteye.com/blog/1007556 二:示例 @Autowired @Qualifier("jdbcTemplate") private JdbcTemplate jdbcTemplate; @SuppressWarnings("unchecked") public Integer GetSerNo() { Integer param2Value = jdbcTemplate.execute( new Cal

Sybase&#183;调用存储过程并返回结果

最近项目要用Sybase数据库实现分页,第一次使用Sybase数据库,也是第一次使用他的存储过程.2个多小时才调用成功,在此记录: 项目架构:SSM 1.Sybase本身不支持分页操作,需要写存储过程来调用,这是很坑的.存储过程的内容暂且放下, 存储过程名:query_xxx_record_detail 输入输出参数如下:9个输入,5个输出 --input @terminal_no varchar(16), @account_no varchar(16), @begin_time char(14

使用CompletionService结合ExecutorService批处理调用存储过程任务实例

此实例为java多线程并发调用存储过程实例,只做代码记载,不做详细描述 1.线程池构造初始化类CommonExecutorService.java package com.pupeiyuan.go; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadFactory; import java.u