BeanProcessorEx.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.yihu.ehr.util.db;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import org.apache.commons.dbutils.BasicRowProcessor;
  19. import org.apache.commons.dbutils.BeanProcessor;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.sql.SQLXML;
  23. import java.sql.Timestamp;
  24. import java.util.Date;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. /**
  28. * <p>
  29. * <code>BeanProcessor</code> matches column names to bean property names
  30. * and converts <code>ResultSet</code> columns into objects for those bean
  31. * properties. Subclasses should override the methods in the processing chain
  32. * to customize behavior.
  33. * </p>
  34. * <p>
  35. * <p>
  36. * This class is thread-safe.
  37. * </p>
  38. *
  39. * @see BasicRowProcessor
  40. * @since DbUtils 1.1
  41. */
  42. public class BeanProcessorEx extends BeanProcessor {
  43. /**
  44. * Constructor for BeanProcessor.
  45. */
  46. public BeanProcessorEx() {
  47. this(new HashMap<String, String>());
  48. }
  49. /**
  50. * Constructor for BeanProcessor configured with column to property name overrides.
  51. *
  52. * @param columnToPropertyOverrides ResultSet column to bean property name overrides
  53. * @since 1.5
  54. */
  55. public BeanProcessorEx(Map<String, String> columnToPropertyOverrides) {
  56. super(columnToPropertyOverrides);
  57. }
  58. /**
  59. * Convert a <code>ResultSet</code> column into an object. Simple
  60. * implementations could just call <code>rs.getObject(index)</code> while
  61. * more complex implementations could perform type manipulation to match
  62. * the column's type to the bean property type.
  63. * <p>
  64. * <p>
  65. * This implementation calls the appropriate <code>ResultSet</code> getter
  66. * method for the given property type to perform the type conversion. If
  67. * the property type doesn't match one of the supported
  68. * <code>ResultSet</code> types, <code>getObject</code> is called.
  69. * </p>
  70. *
  71. * @param rs The <code>ResultSet</code> currently being processed. It is
  72. * positioned on a valid row before being passed into this method.
  73. * @param index The current column index being processed.
  74. * @param propType The bean property type that this column needs to be
  75. * converted into.
  76. * @return The object from the <code>ResultSet</code> at the given column
  77. * index after optional type processing or <code>null</code> if the column
  78. * value was SQL NULL.
  79. * @throws SQLException if a database access error occurs
  80. */
  81. protected Object processColumn(ResultSet rs, int index, Class<?> propType)
  82. throws SQLException {
  83. if (!propType.isPrimitive() && rs.getObject(index) == null) {
  84. return null;
  85. }
  86. if (propType.equals(String.class)) {
  87. return rs.getString(index);
  88. } else if (
  89. propType.equals(Integer.TYPE) || propType.equals(Integer.class)) {
  90. return Integer.valueOf(rs.getInt(index));
  91. } else if (
  92. propType.equals(Boolean.TYPE) || propType.equals(Boolean.class)) {
  93. return Boolean.valueOf(rs.getBoolean(index));
  94. } else if (propType.equals(Long.TYPE) || propType.equals(Long.class)) {
  95. return Long.valueOf(rs.getLong(index));
  96. } else if (
  97. propType.equals(Double.TYPE) || propType.equals(Double.class)) {
  98. return Double.valueOf(rs.getDouble(index));
  99. } else if (
  100. propType.equals(Float.TYPE) || propType.equals(Float.class)) {
  101. return Float.valueOf(rs.getFloat(index));
  102. } else if (
  103. propType.equals(Short.TYPE) || propType.equals(Short.class)) {
  104. return Short.valueOf(rs.getShort(index));
  105. } else if (propType.equals(Byte.TYPE) || propType.equals(Byte.class)) {
  106. return Byte.valueOf(rs.getByte(index));
  107. } else if (propType.equals(Timestamp.class)) {
  108. return rs.getTimestamp(index);
  109. } else if (propType.equals(SQLXML.class)) {
  110. return rs.getSQLXML(index);
  111. } else if (propType.equals(Date.class)) {
  112. return new Timestamp(rs.getTimestamp(index).getTime());
  113. } else {
  114. return rs.getObject(index);
  115. }
  116. }
  117. }