单行数据处理:
ScalarHandler ArrayHandler MapHandler BeanHandler
多行数据处理:BeanListHandler AbstractListHandler(ArrayListHandler MapListHandler ColumnListHandler)
AbstractKeyedHandler(KeyedHandler BeanMapHandler)
可供扩展的类:
BaseResultSetHandler
一、ScalarHandler:获取结果集第一行某列;泛型决定返回值类型,默认返回Object;
String sql="select * from book where id=?";
1.Float name = runner.query(sql, new ScalarHandler<Float>(3),1);//执行sql语句,通配符的值为1;并且获取结果集的第一行第三列(从1开始),并将其封装为Float;
2.Float name = runner.query(sql, new ScalarHandler<Float>(“price”),1);//获取结果集的第一行列名为price的数据项,并将其封装为Float;
3.Float name = runner.query(sql, new ScalarHandler<Float>(),1);//获取结果集的第一行第一列(从1开始),并将其封装为Float;
二、ArrayHandler:获取结果集第一行;返回值默认为Object[];
String sql="select * from book where id=?";
1.Object[] o = runner.query(sql, new ArrayHandler(),1);//获取结果集第一行,并且将每列封装成Object[]的元素;
for (Object object : o) {
System.out.println(String.valueOf(object));
}
三、MapHandler:获取结果集第一行,将其封装成LinkHashMap,返回值Map<String ,Object>;
String sql = “select * form book where id=?”;
Map<String, Object> map = runner.query(sql, new MapHandler(),1);
Set set = map.keySet();
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println(map.get(object));
}
System.out.println(map.get("ID"));//注意 map的key是不区分大小写的
四、BeanHandler<T>:获取结果集的第一行,将其封装进T,整个转换过程最终会在 BeanProcessor 类中完成。
注意:要保证数据库表的字段和javabean(User)的属性一致,并且不区分大小写
String sql = "select * from users";
User user = runner.query(sql,new BeanHandler<User>(User.class));
System.out.println(user);
五、BeanListHandler:将结果集的每一行数据转换为Javabean,再将这个Javabean添加到ArrayList中。
String sql = "select * from book where id<?";
List<Book> list = runner.query(sql, new BeanListHandler<Book>(Book.class),5);
for (Iterator<Book> iterator = list.iterator(); iterator.hasNext();) {
Book book = (Book) iterator.next();
System.out.println(book);
六、AbstractListHandler。。。
七、ArrayListHandler (extends AbstractListHandler<Object[]>) //获取结果集,将每行数据添加到Object[]
,然后再将Object[]添加到ArrayList 里面;
String sql = "select id,name,price from book where id
List<Object[]> list =(List<Object[]>)runner.query(sql, new ArrayListHandler(),5);
for (Iterator<Object[]> iterator = list.iterator(); iterator.hasNext();) {
Object[] objects = (Object[]) iterator.next();
for(Objectobject:objects){
System.out.print(object);
}
System.out.println();
}
八、MapListHandler (extends AbstractListHandler<Map<String, Object>>)//把结果集的每行封装成Map 然后存到ArrayList
String sql = "select id,name,price from book where id<?";
List list = runner.query(sql, new MapListHandler(),5);
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Map map = (Map) iterator.next();
System.out.println(map.toString());
}
九、ColumnListHandler<T> (extends AbstractListHandler<T>)//根据结果集,利用列索引(从1开始)或列名获取整列的内容,添加到ArrayList里面(ScalarHandler的加强版);
1.List list = runner.query(sql, new ColumnListHandler<String>(2),5);
2.List list = runner.query(sql, new ColumnListHandler<String>("name"),5);
十、KeyedHandler<K> (extends AbstractKeyedHandler<K, Map<String, Object>>)//将获取的结果集每行封装到Map,然后将Map封装到Map(双层Map).
String sql = "select * from book where id<?";
Map<Integer,Map<String,Object>> map = runner.query(sql, new KeyedHandler<Integer>(1),5);//将第一列列名作为主键;
System.out.println(map);
十一、BeanMapHandler<K, V> (extends AbstractKeyedHandler<K, V>)// 用于获取所有结果集,将每行结果集转换为Javabean作为value,并指定某列为key,封装到HashMap中。
String sql = "select * from book where id<5";
Map<Integer, Book> map = runner.query(sql, new BeanMapHandler<Integer,Book>(Book.class,1),5);//将表索引第一个作为Map的主键,
Set set = map.keySet();
for(Objectobject:set){
System.out.println(map.get(object));
}