/** * Convert a <code>ResultSet</code> row into a JavaBean. This * implementation delegates to a BeanProcessor instance. * @see org.apache.commons.dbutils.RowProcessor#toBean(java.sql.ResultSet, java.lang.Class) * @see org.apache.commons.dbutils.BeanProcessor#toBean(java.sql.ResultSet, java.lang.Class) * @param <T> The type of bean to create * @param rs ResultSet that supplies the bean data * @param type Class from which to create the bean instance * @throws SQLException if a database access error occurs * @return the newly created bean */ @Override public <T> T toBean(ResultSet rs, Class<? extends T> type) throws SQLException { return this.convert.toBean(rs, type); }
value = this.processColumn(rs, i, propType); this.callSetter(bean, prop, value);
/** * Convert a <code>ResultSet</code> into a <code>List</code> of JavaBeans. * This implementation delegates to a BeanProcessor instance. * @see org.apache.commons.dbutils.RowProcessor#toBeanList(java.sql.ResultSet, java.lang.Class) * @see org.apache.commons.dbutils.BeanProcessor#toBeanList(java.sql.ResultSet, java.lang.Class) * @param <T> The type of bean to create * @param rs ResultSet that supplies the bean data * @param type Class from which to create the bean instance * @throws SQLException if a database access error occurs * @return A <code>List</code> of beans with the given type in the order * they were returned by the <code>ResultSet</code>. */ @Override public <T> List<T> toBeanList(ResultSet rs, Class<? extends T> type) throws SQLException { return this.convert.toBeanList(rs, type); }
/** * Creates a new object and initializes its fields from the ResultSet. * @param <T> The type of bean to create * @param rs The result set. * @param type The bean type (the return type of the object). * @param props The property descriptors. * @param columnToProperty The column indices in the result set. * @return An initialized object. * @throws SQLException if a database error occurs. */ private <T> T createBean(ResultSet rs, Class<T> type, PropertyDescriptor[] props, int[] columnToProperty) throws SQLException { T bean = this.newInstance(type); return populateBean(rs, bean, props, columnToProperty); }
/** * Initializes the fields of the provided bean from the ResultSet. * @param <T> The type of bean * @param rs The result set. * @param bean The bean to be populated. * @return An initialized object. * @throws SQLException if a database error occurs. */ public <T> T populateBean(ResultSet rs, T bean) throws SQLException { PropertyDescriptor[] props = this.propertyDescriptors(bean.getClass()); ResultSetMetaData rsmd = rs.getMetaData(); int[] columnToProperty = this.mapColumnsToProperties(rsmd, props); return populateBean(rs, bean, props, columnToProperty); }
/** * Convert current row of the ResultSet to a bean of a dynamically generated class. It requires CGLIB. * @param rs the ResultSet * @return a bean of a dynamically generated class * @throws SQLException */ public Object convertToDynamicBean(ResultSet rs) throws SQLException{ ResultSetMetaData rsmd = rs.getMetaData(); Map<String, ColumnMetaData> columnToPropertyMappings = createColumnToPropertyMappings(rsmd); Class<?> beanClass = reuseOrBuildBeanClass(rsmd, columnToPropertyMappings); BeanProcessor beanProcessor = new BeanProcessor(simpleColumnToPropertyMappings(columnToPropertyMappings)); return beanProcessor.toBean(rs, beanClass); }
/** * Convert all rows of the ResultSet to a list of beans of a dynamically generated class. It requires CGLIB. * @param rs the ResultSet * @return a list of beans * @throws SQLException */ public List<?> convertAllToDynamicBeans(ResultSet rs) throws SQLException{ ResultSetMetaData rsmd = rs.getMetaData(); Map<String, ColumnMetaData> columnToPropertyMappings = createColumnToPropertyMappings(rsmd); Class<?> beanClass = reuseOrBuildBeanClass(rsmd, columnToPropertyMappings); BeanProcessor beanProcessor = new BeanProcessor(simpleColumnToPropertyMappings(columnToPropertyMappings)); return beanProcessor.toBeanList(rs, beanClass); }
public void testMapColumnToPropertiesWithOverrides() throws Exception { Map<String, String> columnToPropertyOverrides = new HashMap<String, String>(); columnToPropertyOverrides.put("five", "four"); BeanProcessor beanProc = new BeanProcessor(columnToPropertyOverrides); String[] columnNames = { "test", "test", "three", "five" }; String[] columnLabels = { "one", "two", null, null }; ResultSetMetaData rsmd = ProxyFactory.instance().createResultSetMetaData( new MockResultSetMetaData(columnNames, columnLabels)); PropertyDescriptor[] props = Introspector.getBeanInfo(MapColumnToPropertiesBean.class).getPropertyDescriptors(); int[] columns = beanProc.mapColumnsToProperties(rsmd, props); for (int i = 1; i < columns.length; i++) { assertTrue(columns[i] != BeanProcessor.PROPERTY_NOT_FOUND); } } }
PropertyDescriptor[] props = this.propertyDescriptors(type); ResultSetMetaData rsmd = rs.getMetaData(); int[] columnToProperty = this.mapColumnsToProperties(rsmd, props); results.add(this.createBean(rs, type, props, columnToProperty)); } while (rs.next());
@Override protected Object processColumn(ResultSet rs, int index, Class<?> propType) throws SQLException { Object obj = super.processColumn(rs, index, propType); if (obj != null && propType.equals(LensConf.class) && obj instanceof String) { return deserializeConf((String) obj); } return obj; } }
@SuppressWarnings("unchecked") public <T> List<T> select() throws Exception{ try { if (buildSql_Select()) { BeanListHandler<T> beanListHandler= new BeanListHandler<T>((Class<T>) this.beanClass,new BasicRowProcessor(new BeanProcessor(columnToPropertyOverrides))); List<T> beanList = new QueryRunner().query(conn, sql,beanListHandler,param_where); D.autoCloseConn(conn); return beanList; } } catch (Exception e) { D.closeConn(conn); throw e; } return null; }
public void testMapColumnToProperties() throws Exception { String[] columnNames = { "test", "test", "three" }; String[] columnLabels = { "one", "two", null }; ResultSetMetaData rsmd = ProxyFactory.instance().createResultSetMetaData( new MockResultSetMetaData(columnNames, columnLabels)); PropertyDescriptor[] props = Introspector.getBeanInfo(MapColumnToPropertiesBean.class).getPropertyDescriptors(); int[] columns = beanProc.mapColumnsToProperties(rsmd, props); for (int i = 1; i < columns.length; i++) { assertTrue(columns[i] != BeanProcessor.PROPERTY_NOT_FOUND); } }
public void testProcessWithPopulateBean() throws SQLException { TestBean b = new TestBean(); assertTrue(this.rs.next()); b = beanProc.populateBean(this.rs, b); assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); assertEquals(b.getThree(), TestBean.Ordinal.THREE); assertTrue(this.rs.next()); b = beanProc.populateBean(this.rs, b); assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); assertEquals(b.getThree(), TestBean.Ordinal.SIX); assertFalse(this.rs.next()); }
@Override protected Object processColumn(ResultSet rs, int index, Class<?> propType) throws SQLException { if (resultSetValueConverter != null && resultSetValueConverter.supports(propType)) { return resultSetValueConverter.convert(rs.getObject(index), propType); } return super.processColumn(rs, index, propType); }
T bean = this.newInstance(type); return this.populateBean(rs, bean);
/** * 查询一条数据,可搭配page,limit,order,group,having使用 * * @return T 返回javabean * @throws Exception if has error */ @SuppressWarnings("unchecked") public <T> T find() throws Exception{ this.limit(1); try { if (buildSql_Select()) { BeanHandler<T> beanHandler= new BeanHandler<T>((Class<T>) this.beanClass,new BasicRowProcessor(new BeanProcessor(columnToPropertyOverrides))); T bean = new QueryRunner().query(conn, sql,beanHandler, param_where); D.autoCloseConn(conn); return bean; } } catch (Exception e) { D.closeConn(conn); throw e; } return null; }
@Override public <T> T toBean(ResultSet rs, Class<T> type) throws SQLException { return super.toBean(StringTrimmedResultSet.wrap(rs), type); }
@Override public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException { return super.toBeanList(StringTrimmedResultSet.wrap(rs), type); }
public void testProcessWithToBean() throws SQLException { TestBean b = null; assertTrue(this.rs.next()); b = beanProc.toBean(this.rs, TestBean.class); assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); assertEquals(b.getThree(), TestBean.Ordinal.THREE); assertTrue(this.rs.next()); b = beanProc.toBean(this.rs, TestBean.class); assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); assertEquals(b.getThree(), TestBean.Ordinal.SIX); assertFalse(this.rs.next()); }