ColumnMapRowMapper, Commpons collections


/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.jdbc.core;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;

import org.springframework.core.CollectionFactory;
import org.springframework.jdbc.support.JdbcUtils;

/**
* RowMapper implementation that creates a <code>java.util.Map</code>
* for each row, representing all columns as key-value pairs: one
* entry for each column, with the column name as key.
*
* <p>The Map implementation to use and the key to use for each column
* in the column Map can be customized through overriding
* <code>createColumnMap</code> and <code>getColumnKey</code>, respectively.
*
* @author Juergen Hoeller
* @since 1.2
* @see #createColumnMap
* @see #getColumnKey
* @see JdbcTemplate#queryForList(String)
* @see JdbcTemplate#queryForMap(String)
*/
public class ColumnMapRowMapper implements RowMapper {

public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Map mapOfColValues = createColumnMap(columnCount);
for (int i = 1; i <= columnCount; i++) {
String key = getColumnKey(rsmd.getColumnName(i));
Object obj = getColumnValue(rs, i);
mapOfColValues.put(key, obj);
}
return mapOfColValues;
}

/**
* Create a Map instance to be used as column map.
* <p>By default, a linked case-insensitive Map will be created if possible,
* else a plain HashMap (see Spring's CollectionFactory).
* @param columnCount the column count, to be used as initial
* capacity for the Map
* @return the new Map instance
* @see org.springframework.core.CollectionFactory#createLinkedCaseInsensitiveMapIfPossible
*/
protected Map createColumnMap(int columnCount) {
return CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(columnCount);
}

/**
* Determine the key to use for the given column in the column Map.
* @param columnName the column name as returned by the ResultSet
* @return the column key to use
* @see java.sql.ResultSetMetaData#getColumnName
*/
protected String getColumnKey(String columnName) {
return columnName;
}

/**
* Retrieve a JDBC object value for the specified column.
* <p>The default implementation uses the <code>getObject</code> method.
* Additionally, this implementation includes a "hack" to get around Oracle
* returning a non standard object for their TIMESTAMP datatype.
* @param rs is the ResultSet holding the data
* @param index is the column index
* @return the Object returned
* @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue
*/
protected Object getColumnValue(ResultSet rs, int index) throws SQLException {
return JdbcUtils.getResultSetValue(rs, index);
}

}




/**
* Create a linked case-insensitive map if possible: if Commons Collections
* 3.x is available, a CaseInsensitiveMap with ListOrderedMap decorator will
* be created. Else, a JDK 1.4+ LinkedHashMap or plain HashMap will be used.
* @param initialCapacity the initial capacity of the map
* @return the new map instance
* @see org.apache.commons.collections.map.CaseInsensitiveMap
* @see org.apache.commons.collections.map.ListOrderedMap
*/
public static Map createLinkedCaseInsensitiveMapIfPossible(int initialCapacity) {
if (commonsCollections3xAvailable) {
logger.debug("Creating [org.apache.commons.collections.map.ListOrderedMap/CaseInsensitiveMap]");
return CommonsCollectionFactory.createListOrderedCaseInsensitiveMap(initialCapacity);
}
else if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_14) {
logger.debug("Falling back to [java.util.LinkedHashMap] for linked case-insensitive map");
return Jdk14CollectionFactory.createLinkedHashMap(initialCapacity);
}
else {
logger.debug("Falling back to [java.util.HashMap] for linked case-insensitive map");
return new HashMap(initialCapacity);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值