123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package com.yihu.ehr.util.db;
- /*
- * 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.
- */
- import org.apache.commons.dbutils.BasicRowProcessor;
- import org.apache.commons.dbutils.BeanProcessor;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.SQLXML;
- import java.sql.Timestamp;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * <p>
- * <code>BeanProcessor</code> matches column names to bean property names
- * and converts <code>ResultSet</code> columns into objects for those bean
- * properties. Subclasses should override the methods in the processing chain
- * to customize behavior.
- * </p>
- * <p>
- * <p>
- * This class is thread-safe.
- * </p>
- *
- * @see BasicRowProcessor
- * @since DbUtils 1.1
- */
- public class BeanProcessorEx extends BeanProcessor {
- /**
- * Constructor for BeanProcessor.
- */
- public BeanProcessorEx() {
- this(new HashMap<String, String>());
- }
- /**
- * Constructor for BeanProcessor configured with column to property name overrides.
- *
- * @param columnToPropertyOverrides ResultSet column to bean property name overrides
- * @since 1.5
- */
- public BeanProcessorEx(Map<String, String> columnToPropertyOverrides) {
- super(columnToPropertyOverrides);
- }
- /**
- * Convert a <code>ResultSet</code> column into an object. Simple
- * implementations could just call <code>rs.getObject(index)</code> while
- * more complex implementations could perform type manipulation to match
- * the column's type to the bean property type.
- * <p>
- * <p>
- * This implementation calls the appropriate <code>ResultSet</code> getter
- * method for the given property type to perform the type conversion. If
- * the property type doesn't match one of the supported
- * <code>ResultSet</code> types, <code>getObject</code> is called.
- * </p>
- *
- * @param rs The <code>ResultSet</code> currently being processed. It is
- * positioned on a valid row before being passed into this method.
- * @param index The current column index being processed.
- * @param propType The bean property type that this column needs to be
- * converted into.
- * @return The object from the <code>ResultSet</code> at the given column
- * index after optional type processing or <code>null</code> if the column
- * value was SQL NULL.
- * @throws SQLException if a database access error occurs
- */
- protected Object processColumn(ResultSet rs, int index, Class<?> propType)
- throws SQLException {
- if (!propType.isPrimitive() && rs.getObject(index) == null) {
- return null;
- }
- if (propType.equals(String.class)) {
- return rs.getString(index);
- } else if (
- propType.equals(Integer.TYPE) || propType.equals(Integer.class)) {
- return Integer.valueOf(rs.getInt(index));
- } else if (
- propType.equals(Boolean.TYPE) || propType.equals(Boolean.class)) {
- return Boolean.valueOf(rs.getBoolean(index));
- } else if (propType.equals(Long.TYPE) || propType.equals(Long.class)) {
- return Long.valueOf(rs.getLong(index));
- } else if (
- propType.equals(Double.TYPE) || propType.equals(Double.class)) {
- return Double.valueOf(rs.getDouble(index));
- } else if (
- propType.equals(Float.TYPE) || propType.equals(Float.class)) {
- return Float.valueOf(rs.getFloat(index));
- } else if (
- propType.equals(Short.TYPE) || propType.equals(Short.class)) {
- return Short.valueOf(rs.getShort(index));
- } else if (propType.equals(Byte.TYPE) || propType.equals(Byte.class)) {
- return Byte.valueOf(rs.getByte(index));
- } else if (propType.equals(Timestamp.class)) {
- return rs.getTimestamp(index);
- } else if (propType.equals(SQLXML.class)) {
- return rs.getSQLXML(index);
- } else if (propType.equals(Date.class)) {
- return new Timestamp(rs.getTimestamp(index).getTime());
- } else {
- return rs.getObject(index);
- }
- }
- }
|