| « | November 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
| Blog信息 |
|
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9743006 建立时间:2004年12月20日 |

| |
|
[java语言]使用spring实现的占位存储过程的调用 原创空间, 软件技术, 电脑与网络
邢红瑞 发表于 2006/3/16 16:05:38 |
| import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.CallableStatementCallback;import org.springframework.jdbc.core.namedparam.NamedParameterUtils;import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;import org.springframework.core.CollectionFactory;
import java.util.*;import java.sql.*;
/** * Created by IntelliJ IDEA. * User: 邢红瑞 * Date: 2006-12-19 * Time: 16:18:34 * To change this template use File | Settings | File Templates. */public class CommonDBDao extends NamedParameterJdbcDaoSupport { private String procedure;
/** * @return */ public String getProcedure() { return procedure; }
public void setProcedure(String procedure) { procedure = "{ call " + procedure + " } "; this.procedure = procedure; }
public List<Map> queryMaps(Map condMapForQuery) { final ArrayList list = new ArrayList(); final Object[] sqlarray = NamedParameterUtils.buildValueArray(procedure, condMapForQuery); String sqlstr = NamedParameterUtils.substituteNamedParameters(procedure, new MapSqlParameterSource(condMapForQuery)); final String sqlstr0 = sqlstr.replace("{", "{ ?="); Object o = getJdbcTemplate().execute(sqlstr0, new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement arg0) throws SQLException, DataAccessException { arg0.registerOutParameter(1, Types.INTEGER); for (int i = 0; i < sqlarray.length; i++) { arg0.setObject(i + 2, sqlarray[i]); } ResultSet rs = arg0.executeQuery(); ListResultSetExtractor ls = new ListResultSetExtractor(); list.addAll((Collection) ls.extractData(rs)); return null; } }); return list; }
public static Map CreateMap(int result) { Map map = new HashMap(); map.put("result", new Integer(result)); return map; }
private static class ListResultSetExtractor {
public Object extractData(ResultSet rs) throws SQLException { List listOfRows = new ArrayList(); ResultSetMetaData rsmd = null; int numberOfColumns = -1; while (rs.next()) { if (rsmd == null) { rsmd = rs.getMetaData(); numberOfColumns = rsmd.getColumnCount(); } Map mapOfColValues = CollectionFactory.createLinkedMapIfPossible(numberOfColumns); for (int i = 1; i <= numberOfColumns; i++) { switch (rsmd.getColumnType(i)) { case java.sql.Types.TIMESTAMP: mapOfColValues.put(rsmd.getColumnName(i), rs.getTimestamp(i)); break; default: mapOfColValues.put(rsmd.getColumnName(i), rs.getObject(i)); } } listOfRows.add(mapOfColValues); } return listOfRows; } }}配置文件 <property name="procedure"> <value> <![CDATA[pr_get_log_list(:forward,:id) ]]></value></property> |
|
|