Java Code Examples for org.apache.commons.dbutils.GenerousBeanProcessor

The following examples show how to use org.apache.commons.dbutils.GenerousBeanProcessor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mcg-helper   Source File: DbConnect.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @Title:       querySql   
 * @Description: TODO(带可变参数查询,返回执行结果)   
 * @param:       @param clazz 转换的对象实例
 * @param:       @param sql 查询sql语句
 * @param:       @param para 查询参数
 * @param:       @return
 * @param:       @throws SQLException      
 * @return:      List      
 * @throws
 */
public <T> List querySql(T clazz, String sql, Object... para) throws SQLException {
	logger.debug("查询Sql: {}, 查询参数: {}", sql, ToStringBuilder.reflectionToString(para));
	QueryRunner runner = new QueryRunner();
	Connection conn = null;
	List<T> result = new ArrayList<T>();
	try {
		conn = getConnection();
		// 下划线分隔的表字段名转换为实体bean驼峰命名属性
		BeanProcessor bean = new GenerousBeanProcessor();
		RowProcessor processor = new BasicRowProcessor(bean);
		result = (List<T>) runner.query(conn, sql, new BeanListHandler((Class) clazz, processor), para);
	} catch (SQLException e) {
		logger.error("查询出错,异常信息: {}------", e.getMessage());
		throw e;
	} finally {
		if (conn != null && conn.getAutoCommit() == true) {
			freeConnection();
		}
	}
	return result;
}