`

Apache Commons DbUtils

    博客分类:
  • java
阅读更多
dbutils jar下载地址http://labs.renren.com/apache-mirror//commons/dbutils/binaries/commons-dbutils-1.4-bin.zip

配置获取一个数据库连接(添加数据库驱动)
		String url = "xxx";
		String driver = "oracle.jdbc.driver.OracleDriver";
		Class.forName(driver);
		Connection con = DriverManager.getConnection(url, "user","password");

将查询结果转换成对应的bean
		BeanHandler<Score> bh = new BeanHandler<Score>(Score.class);
		QueryRunner run = new QueryRunner();
		try {
			Score score =run.query(con, "select ss.* from score_score ss where ss.id=?",bh,299);
			//上面使用的query方法最后一个参数是可变参数,是sql中需要传递填充的值
			System.out.println(score.getScore());
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				DbUtils.close(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}


将查询结果转换成对应的bean list
		BeanListHandler<Score> blh = new BeanListHandler<Score>(Score.class);
		QueryRunner run = new QueryRunner();
		try {
			List<Score> list =run.query(con, "select * from score_score",blh);
			for(Score s:list){
				System.out.println(s.getScore());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				DbUtils.close(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

上面的bean和bean list转换不需要显示的调用PreparedStatement和ResultSet。直接通过QueryRunner工具类。

-------------个----------------
将ResultSet转换成bean
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select ss.* from score_score ss where ss.id=299";
		try {
			pstat =con.prepareStatement(sql);
			rs = pstat.executeQuery();
			while(rs.next()){
				RowProcessor rp = new BasicRowProcessor();
				Score score = rp.toBean(rs, Score.class);
				System.out.println(score.getScore());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DbUtils.closeQuietly(con, pstat, rs);
		}

将ResultSet转换成bean list
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select * from score_score";
		try {
			pstat =con.prepareStatement(sql);
			rs = pstat.executeQuery();
			while(rs.next()){
				RowProcessor rp = new BasicRowProcessor();
				List<Score> list= rp.toBeanList(rs,Score.class);
				for(Score s:list){
					System.out.println(s.getScore());
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DbUtils.closeQuietly(con, pstat, rs);
		}




* 类调用转换过程
* QueryRunner类的run方法
* 通过PreparedStatement获取ResultSet,然后对获取的ResultSet进行转换
* BeanHandler,BeanListHandler的默认RowProcessor都是
* ArrayHandler.ROW_PROCESSOR=BasicRowProcessor
* 而BasicRowProcessor的默认covert是BeanProcessor
* 所以最终转换都是调用的BeanProcessor中的toBean方法和toBeanList方法
*
* 所以要想控制转换 可以自定义BeanProcessor
* BeanProcessor转换过程:
* A.首先从传递过来的bean的class文件中提取所有的属性
* 即PropertyDescriptor[] props;
* PropertyDescriptor 对应存储属性名和属性类型
* B.检查ResultSet中的row colums和PropertyDescriptor[]的对应关系;哪个属性对应哪个字段
*   默认的匹配规则就是列名和属性名相等(忽略大小写)
*   对应方法:mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props)
*   可以从写该方法指定特定的属性和特定的列对应
* C.class,属性,对应字段值都有就可以创建对象赋值了
0
2
分享到:
评论

相关推荐

    apache commons dbutils api_zh

    apache commons dbutils api_zh

    Apache Commons DbUtils 1.6 API

    Apache Commons DbUtils 1.6 API,网页形式的api文档

    Apache commons dbutils 1.4jar是JDBC的开源数据库工具包

    Apache commons dbutils 1.4jar是JDBC的开源数据库工具包

    apache commons dbutils实现增删改查功能

    apache commons dbutils实现增删改查功能

    Apache Commons DbUtils 包是一组用于简化 JDBC 开发的 Java 实用程序类

    Apache Commons DbUtils 包是一组用于简化 JDBC 开发的 Java 实用程序类。原则上这不能说是一个持久层框架,它提供了一些Jdbc的操作封装来简化数据查询和记录读取操作

    commons-dbutils-1.7-API文档-中文版.zip

    赠送jar包:commons-dbutils-1.7.jar; 赠送原API文档:commons-dbutils-1.7-javadoc.jar; 赠送源代码:commons-dbutils-1.7-sources.jar; 赠送Maven依赖信息文件:commons-dbutils-1.7.pom; 包含翻译后的API文档...

    Apache Commons DbUtils 1.5 API CHM

    数据库查询工具包 Commons-DbUtils 1.5 api 文档,官方下载,原创制作

    commons-dbutils的再封装jar包

    commons-dbutils的再封装jar包,Blog文件的示例代码

    apache commons jar(commons所有的jar包,从官网下载提供.zip

    apache commons jar(commons所有的jar包,从官网下载提供给大家) 因为涉及jar太多,包括有src源代码,只需要3分,希望大家理解,我也是从官网花了很长时间才一个一个下完,需要的请自取。全部是zip文件,每个对应的...

    Commons-dbutils1.7 jar包.rar

    commons-dbutils包是Apache开源组织提供的用于操作数据库的工具包。简单来讲,这个工具包就是用来更加方便我们操作数据库的,最近工作中使用了一下,感觉确实方便很多,基本告别自己封装JDBC代码对数据库进行增删改...

    Apache Commons DbUtils工具包使用介绍

    主要介绍了Apache Commons DbUtils工具包使用介绍,本文介绍了DBUtils是什么东西、熟悉DBUtils的一些问题、API介绍等内容,需要的朋友可以参考下

    Dbutils项目实例

    org apache commons dbutils org apache commons dbutils handlers org apache commons dbutils wrappers DBUtils封装了对JDBC的操作 简化了JDBC操作 可以少写代码 org apache commons dbutils DbUtils 关闭链接等...

    commons-dbutils-1.4.jar

    commons-dbutils-1.4jar包,让你在java内对数据库的操作更加简单

    Commons DbUtils 使用说明.rar

    commons-dbutils 是 Apache 组织提供的一个...主要讲解两个类(org.apache.commons.dbutils.DbUtils 和org.apache.commons.dbutils.QueryRunner)和一个接口(org.apache.commons.dbutils.ResultSethandler)。 ......

    commons-dbutils

    commons-dbutils

    commons-dbutils-1.6.jar包

    包org.apache.commons.dbutils DbUtils是一个为简化JDBC操作的小类库. 接口摘要 ResultSetHandler 将ResultSet转换为别的对象的工具. RowProcessor 将ResultSet行转换为别的对象的工具. 类摘要 BasicRowProcessor ...

    uncommons-dbutils:Apache Commons DBUtils 的现代化分支

    uncommons-dbutils 现代化分支。 目的是制作更灵活的 API 并修复以前的设计错误,同时保持原始的小尺寸和紧密关注。 完整文档位于特征交易支持通过异步查询轻量级:执行查询并将ResultSet映射到对象。 就这样。 使用...

    commons-dbutils.jarv1.6官方免费版

    commons-dbutils.jar是在java架构开发时十分重要的一款.jar包,正确的使用commons dbutils可以让你的开发事半功倍,如果您在开发过程中缺少这款jar包,马上来下载commonsdbutils jar包吧! 软件功能: commons-...

    commons-dbutils-1.7

    Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能

    apache-commons下源码大放送

    apache-commons下全部官方源码和官方API文档,其中有: commons-beanutils-1.8.0 commons-codec commons-collections commons-dbcp commons-dbutils commons-fileupload commons-io commons-lang commons-lang3 ...

Global site tag (gtag.js) - Google Analytics