将每行映射为MAP对象的RowMapper实现
2010-04-23 09:40
417 查看
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<Map>
{
/**
* 此方法用于将每行数据映射为实体类。
*
* @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.
* <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 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.
* <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
* @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);
}
}
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<Map>
{
/**
* 此方法用于将每行数据映射为实体类。
*
* @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.
* <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 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.
* <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
* @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);
}
}
相关文章推荐
- Spring2.5 BeanPropertyRowMapper ResultSet和实体类的字段进行实现自动映射
- Nhibernate实现类似 BeanPropertyRowMapper 的类型映射
- 用java反射将map映射成java对象的简易实现,模拟BeanUtils
- JS中的Map和Set实现映射对象
- 凹凸映射(Bump Map)实现原理
- ModelMapper Java对象自动映射
- Map在Java中构造缓存对象的原理及实现
- java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类HashMap Hashtable LinkedHashMap 和TreeMap.
- Mapper的XML映射文件---2.5: 对象工厂(ObjectFactory)
- bit_map映射实现高速排序查找
- 推荐几个对象映射(Object-Object Map)的库
- JS自定义对象实现Java中Map对象功能的方法
- 使用NamedParameterJdbcTemplate 通过BeanPropertyRowMapper 返回一个对象或List 集合
- MyBatis之XML文件配置实现映射器Mapper
- myBatis配置实现数据库字段下划线映射到java对象的驼峰式命名属性
- 凹凸映射(Bump Map)实现原理
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
- JavaScript中实现类似Java中的Map对象
- Hibernate高级实体关联映射之多值的实体关联(map实现三重关联,单向导航)
- java中两个对象间的属性值复制,比较,转为map方法实现