KeyedHandler类源码

本文介绍了一个用于Apache Commons DbUtils库的KeyedHandler类,该类实现了ResultSetHandler接口,可以将数据库查询结果转换为Map集合。KeyedHandler允许指定键值来源,支持通过列名或索引获取,并使用RowProcessor来处理行数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.commons.dbutils.handlers;

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

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;

/**
 * <p>
 * <code>ResultSetHandler</code> implementation that returns a Map of Maps.
 * <code>ResultSet</code> rows are converted into Maps which are then stored
 * in a Map under the given key.  Although this implementation uses Maps to 
 * store row contents, subclasses are encouraged to override the 
 * <code>createRow()</code> method to convert the rows into any kind of object.
 * </p>
 * <p>
 * If you had a Person table with a primary key column called ID, you could 
 * retrieve rows from the table like this:
 * <pre>
 * ResultSetHandler h = new KeyedHandler("id");
 * Map found = (Map) queryRunner.query("select id, name, age from person", h);
 * Map jane = (Map) found.get(new Long(1)); // jane's id is 1
 * String janesName = (String) jane.get("name");
 * Integer janesAge = (Integer) jane.get("age");
 * </pre>
 * Note that the "id" passed to KeyedHandler and "name" and "age" passed to the
 * returned Map's get() method can be in any case.  The data types returned for
 * name and age are dependent upon how your JDBC driver converts SQL column 
 * types from the Person table into Java types.  
 * </p>
 * <p>
 * To avoid these type issues you could subclass KeyedHandler and override 
 * <code>createRow()</code> to store rows in Java bean instances (ie. a
 * Person class).
 * </p>
 * <p>This class is thread safe.</p>
 * 
 * @see org.apache.commons.dbutils.ResultSetHandler
 * @since DbUtils 1.1
 */
public class KeyedHandler implements ResultSetHandler {

    /**
     * The RowProcessor implementation to use when converting rows
     * into Objects.
     */
    protected RowProcessor convert = ArrayHandler.ROW_PROCESSOR;

    /**
     * The column index to retrieve key values from.  Defaults to 1.
     */
    protected int columnIndex = 1;

    /**
     * The column name to retrieve key values from.  Either columnName or 
     * columnIndex will be used but never both.
     */
    protected String columnName = null;

    /** 
     * Creates a new instance of KeyedHandler.  The value of the first column 
     * of each row will be a key in the Map.
     */
    public KeyedHandler() {
        super();
    }

    /**
     * Creates a new instance of KeyedHandler.  The value of the first column 
     * of each row will be a key in the Map.
     *
     * @param convert The <code>RowProcessor</code> implementation
     * to use when converting rows into Maps
     */
    public KeyedHandler(RowProcessor convert) {
        super();
        this.convert = convert;
    }

    /** 
     * Creates a new instance of KeyedHandler.
     * 
     * @param columnIndex The values to use as keys in the Map are 
     * retrieved from the column at this index.
     */
    public KeyedHandler(int columnIndex) {
        super();
        this.columnIndex = columnIndex;
    }

    /** 
     * Creates a new instance of KeyedHandler.
     * 
     * @param columnName The values to use as keys in the Map are 
     * retrieved from the column with this name.
     */
    public KeyedHandler(String columnName) {
        super();
        this.columnName = columnName;
    }

    /**
     * Convert each row's columns into a Map and store then 
     * in a <code>Map</code> under <code>ResultSet.getObject(key)</code> key.
     * 
     * @return A <code>Map</code> of Maps, never <code>null</code>. 
     * @throws SQLException if a database access error occurs
     * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
     */
    public Object handle(ResultSet rs) throws SQLException {
        Map result = createMap();
        while (rs.next()) {
            result.put(createKey(rs), createRow(rs));
        }
        return result;
    }

    /**
     * This factory method is called by <code>handle()</code> to create the Map
     * to store records in.  This implementation returns a <code>HashMap</code>
     * instance.
     *
     * @return Map to store records in
     */
    protected Map createMap() {
        return new HashMap();
    }

    /**
     * This factory method is called by <code>handle()</code> to retrieve the
     * key value from the current <code>ResultSet</code> row.  This 
     * implementation returns <code>ResultSet.getObject()</code> for the 
     * configured key column name or index. 
     * @param rs ResultSet to create a key from
     * @return Object from the configured key column name/index
     * @throws SQLException if a database access error occurs
     */
    protected Object createKey(ResultSet rs) throws SQLException {
        return (columnName == null) ? rs.getObject(columnIndex) : rs
                .getObject(columnName);
    }

    /**
     * This factory method is called by <code>handle()</code> to store the
     * current <code>ResultSet</code> row in some object. This 
     * implementation returns a <code>Map</code> with case insensitive column
     * names as keys.  Calls to <code>map.get("COL")</code> and 
     * <code>map.get("col")</code> return the same value.
     * @param rs ResultSet to create a row from
     * @return Object typed Map containing column names to values
     * @throws SQLException if a database access error occurs
     */
    protected Object createRow(ResultSet rs) throws SQLException {
        return this.convert.toMap(rs);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值