public Object query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params)
public Object query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params)
其中ResultSetHandler是一个接口,实际上,万能的Apache已经为我们提供了众多好用的实现类,现在举例如下:
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);
}
}
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
源码:
/*
* 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;
}
}
/*
* 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;
}
}
学好英文很重要,静下心来很重要。定能生慧。