spring jdbctemplate调用存储过程,返回list对象
生活随笔
收集整理的這篇文章主要介紹了
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 isbegin-- 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 isselect t.city,t.username,t.username as telphone,'' as ipfrom INTERFACE_WIRELESS.USER_LOGIN_LOG t ;*/beginopen c_uservisit Forselect t.city, t.username, t.username as telphone, '' as ipfrom 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());@Autowiredprivate 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
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的spring jdbctemplate调用存储过程,返回list对象的全部內容,希望文章能夠幫你解決所遇到的問題。