Home > Database > Mysql Tutorial > body text

使用DbUtils实现增删改查ResultSetHandler接口的实现类

WBOY
Release: 2016-06-07 16:01:15
Original
1440 people have browsed it

在上一篇文章中《使用DbUtils实现增删改查》,发现执行runner.query()这行代码时,需要自己去处理查询到的结果集,比较麻烦。这行代码的原型是: public Object query(Connection conn, String sql, ResultSetHandlerT rsh, Object... params) 其中ResultSet

在上一篇文章中《使用DbUtils实现增删改查》,发现执行runner.query()这行代码时,需要自己去处理查询到的结果集,比较麻烦。这行代码的原型是:

public Object query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params)
Copy after login

其中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);
	}
}
Copy after login
测试时,可以加断点调试,再执行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里,其key为指定的列。
⑨ScalarHandler:获取结果集中第一行数据指定列的值,常用来进行单值查询。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!