java rowmapper 通用实现_将每行映射为MAP对象的RowMapper实现

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.LinkedHashMap;

import java.util.Map;

import com.huawei.widget.commons.dao.RowMapper;

import com.huawei.widget.commons.dao.util.JdbcUtils;

/**

* 将每行映射为MAP对象的RowMapper实现。

*

* @author g00106664

* @version C02 2009-4-27

* @since OpenEye WIDGET_SRV V100R001C02

*/

@SuppressWarnings("unchecked")

public class ColumnMapRowMapper implements RowMapper{

/**

* 此方法用于将每行数据映射为实体类。

*

* @param rs

*            ResultSet

* @param rowNum

*            the row num

* @return the t

* @throws java.sql.SQLException

*             the SQL exception

* @see com.huawei.widget.db.RowMapper#mapRow(ResultSet, int)

*/

public Map 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.toLowerCase(), obj);

}

return mapOfColValues;

}

/**

* Create a Map instance to be used as column map.

*

* 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 new LinkedHashMap(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.

*

* The default implementation uses the getObject 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

* @throws java.sql.SQLException

*             如有异常,抛出。

* @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue

*/

protected Object getColumnValue(ResultSet rs, int index)

throws SQLException

{

return JdbcUtils.getResultSetValue(rs, index);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值