本文主要是介绍dbutils各种类型处理器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.ArrayHandler把结果集的第一行数据转换成对象数组 @Testpublic void testArrayHandler() throws SQLException{QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());String sql="select * from user";Object result[]=(Object [])qr.query(sql, new ArrayHandler());System.out.println(Arrays.asList(result));}
2.ArrayListHandler把结果集的每一行数据转换成对象数组,再存放到List中
@Testpublic void testArrayListHandler() throws SQLException{QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());String sql="select * from user";List<Object []> list=(List) qr.query(sql, new ArrayListHandler());for(Object[] l:list){System.out.println(Arrays.asList(l));
3.BeanHandler将结果中的第一行数据封装到一个对应javaBean
@Testpublic void testBeanHandler() throws SQLException{QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());String sql="select * from user";User user=(User) qr.query(sql, new BeanHandler(User.class)); System.out.println(user.getBirthday());}
4.BeanListHandler把结果集的每一行数据转换成一个对应javaBean,在放到List中
@Testpublic void testBeanListHandler() throws SQLException{QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());String sql="select * from user";List<User> list=(List<User>) qr.query(sql, new BeanListHandler(User.class));for(User user:list){System.out.println(Arrays.asList(user.getBirthday()));}}
5.columnListHandler将结果集中的某一列存放到一个List中
<span style="font-size:12px;">@Testpublic void testColumnListHandler() throws SQLException{QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());String sql="select * from user";List list=(List)qr.query(sql, new ColumnListHandler("name")); for(Object obj:list){System.out.println(obj);}}</span>
6.KeyedHandler:将结果集中的数据都封装成一个map,在将这些map存放到一个map中,Key为指定的key
<span style="font-size:12px;">@Testpublic void testKeyedHandler() throws SQLException{QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());String sql="select * from user";Map<Integer,Map> map=(Map) qr.query(sql, new KeyedHandler("id"));Iterator<Entry<Integer, Map>> it=map.entrySet().iterator();while(it.hasNext()){Entry<Integer,Map> entry=it.next();int id=entry.getKey();//System.out.println(id);Map<String, Object> map2=entry.getValue();for(Map.Entry<String, Object> mapEntry:map2.entrySet()){String keyString=mapEntry.getKey();Object obj=mapEntry.getValue();System.out.println(keyString+" "+obj);}}}</span>
7.MapHandler:将结果集中的第一行数据都封装成一个map,key就是列名,value就是对应的值
@Testpublic void testMapHandler() throws SQLException{QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());String sql="select * from user";Map<String,Object> map=(Map<String, Object>)qr.query(sql, new MapHandler()); Iterator iterator=map.entrySet().iterator();while(iterator.hasNext()){Map.Entry<String, Object> map2=(Entry<String, Object>) iterator.next();String key=map2.getKey();Object value=map2.getValue();System.out.println(key+" "+value);}}
8.MapListHandler:将结果集中的每一行数据都封装成一个map,key就是列名,value就是对应的值。再放到一个List中
<span style="font-size:12px;">@Testpublic void testMapListHandler() throws SQLException{QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());String sql="select * from user";List<Map<String,Object>> list=(List<Map<String, Object>>)qr.query(sql, new MapListHandler()); for(Map<String, Object> map:list){Iterator iterator=map.entrySet().iterator();while(iterator.hasNext()){Map.Entry<String, Object> map2=(Entry<String, Object>) iterator.next();String key=map2.getKey();Object value=map2.getValue();System.out.println(key+" "+value);}}}</span>
9.ScalarHandler:返回结果集中的第一行的指定列的一个值。
public void testScalarHandler() throws SQLException{QueryRunner qr=new QueryRunner(JdbcUtils.getDataSource());String sql="select count(*) from user";long l=(Long)qr.query(sql, new ScalarHandler(1));int i=(int)l;System.out.println(i);}
这篇关于dbutils各种类型处理器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!