Apache Commons DbUtils

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,属性,对应字段值都有就可以创建对象赋值了

你可能感兴趣的:(apache commons,DbUtils)