ResultSetHandler接口的实现类
1 | public Object query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) |
其中ResultSetHandler是一个接口,实际上,万能的Apache已经为我们提供了众多好用的实现类,现在举例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | public class RSHanlderDemo { //ScalarHandler:获取结果集中第一行数据指定列的值,常用来进行单值查询 @Test public void tes9() throws SQLException{ QueryRunner runner = new QueryRunner( new ComboPooledDataSource()); Long count = (Long)runner.query( "select count(*) from account" , new ScalarHandler()); System.out.println(count); } //KeyedHandler(name):将结果集中的每一行数据都封装到一个Map里(List<Map>),再把这些map再存到一个map里,其key为指定的列。 @Test public void tes8() throws SQLException{ QueryRunner runner = new QueryRunner( new ComboPooledDataSource()); Map<Object, Map<String, Object>> map = runner.query( "select * from account where money>?" , new KeyedHandler( "id" ), 500 ); System.out.println(map); } //ColumnListHandler:将结果集中某一列的数据存放到List中。 @Test public void tes7() throws SQLException{ QueryRunner runner = new QueryRunner( new ComboPooledDataSource()); List<Object>list = runner.query( "select * from account where money>?" , new ColumnListHandler( 3 ), 500 ); System.out.println(list); } //MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List @Test public void tes6() throws SQLException{ QueryRunner runner = new QueryRunner( new ComboPooledDataSource()); List<Map<String, Object>> list = runner.query( "select * from account where money>?" , new MapListHandler(), 500 ); System.out.println(list); } //MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。 @Test public void tes5() throws SQLException{ QueryRunner runner = new QueryRunner( new ComboPooledDataSource()); Map<String, Object> map = runner.query( "select * from account where money>?" , new MapHandler(), 500 ); System.out.println(map); } //BeanListHandler:将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。 @Test public void tes4() throws SQLException{ QueryRunner runner = new QueryRunner( new ComboPooledDataSource()); List<Account>list = runner.query( "select * from account where money>?" , new BeanListHandler<Account>(Account. class ), 500 ); System.out.println(list); } //BeanHandler:将结果集中的第一行数据封装到一个对应的JavaBean实例中。 @Test public void tes3() throws SQLException{ QueryRunner runner = new QueryRunner( new ComboPooledDataSource()); Account acc = runner.query( "select * from account where money>?" , new BeanHandler<Account>(Account. class ), 500 ); System.out.println(acc); } //ArrayListHandler:把结果集中的每一行数据都转成一个对象数组,再存放到List中。 @Test public void tes2() throws SQLException{ QueryRunner runner = new QueryRunner( new ComboPooledDataSource()); List<Object[]> list = runner.query( "select * from account where money>?" , new ArrayListHandler(), 500 ); System.out.println(list); } //ArrayHandler:把结果集中的第一行数据转成对象数组。 @Test public void test1() throws SQLException{ QueryRunner runner = new QueryRunner( new ComboPooledDataSource()); Object[] objs = runner.query( "select * from account where money>?" , new ArrayHandler(), 500 ); System.out.println(objs); } } |
测试时,可以加断点调试,再执行Debug as JUnit Test。
总结如下:
①ArrayHandler:把结果集中的第一行数据转成对象数组。
②ArrayListHandler:把结果集中的每一行数据都转成一个对象数组,再存放到List中。
③BeanHandler:将结果集中的第一行数据封装到一个对应的JavaBean实例中。
④BeanListHandler:将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。
⑤MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。
⑥MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List
⑦ColumnListHandler:将结果集中某一列的数据存放到List中。
⑧KeyedHandler(name):将结果集中的每一行数据都封装到一个Map里(List<Map>),再把这些map再存到一个map里,其key为指定的列。
⑨ScalarHandler:获取结果集中第一行数据指定列的值,常用来进行单值查询。
返回的类型是这样,取值就方便了,实际这些可以通过阅读源码获得:
例如 ArrayHandler
源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /* * 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 org.apache.commons.dbutils.BasicRowProcessor; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.RowProcessor; /** * <code>ResultSetHandler</code> implementation that converts a * <code>ResultSet</code> into an <code>Object[]</code>. This class is * thread safe. * * @see org.apache.commons.dbutils.ResultSetHandler */ public class ArrayHandler implements ResultSetHandler<Object[]> { /** * Singleton processor instance that handlers share to save memory. Notice * the default scoping to allow only classes in this package to use this * instance. */ static final RowProcessor ROW_PROCESSOR = new BasicRowProcessor(); /** * An empty array to return when no more rows are available in the ResultSet. */ private static final Object[] EMPTY_ARRAY = new Object[ 0 ]; /** * The RowProcessor implementation to use when converting rows * into arrays. */ private final RowProcessor convert; /** * Creates a new instance of ArrayHandler using a * <code>BasicRowProcessor</code> for conversion. */ public ArrayHandler() { this (ROW_PROCESSOR); } /** * Creates a new instance of ArrayHandler. * * @param convert The <code>RowProcessor</code> implementation * to use when converting rows into arrays. */ public ArrayHandler(RowProcessor convert) { super (); this .convert = convert; } /** * Places the column values from the first row in an <code>Object[]</code>. * 将第一行的所有列值放进一个 Object【】 数组中 * @param rs <code>ResultSet</code> to process. * @return An Object[]. If there are no rows in the <code>ResultSet</code> * an empty array will be returned. * 如果没有返回行,就返回一个空数组 * @throws SQLException if a database access error occurs * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) */ @Override public Object[] handle(ResultSet rs) throws SQLException { return rs.next() ? this .convert.toArray(rs) : EMPTY_ARRAY; } } |
学好英文很重要,静下心来很重要。定能生慧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!