org.apache.commons.dbutils.BasicRowProcessor Java Examples

The following examples show how to use org.apache.commons.dbutils.BasicRowProcessor. 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 File: M.java    From ThinkJD with Apache License 2.0 6 votes vote down vote up
/**
 * 查询一条数据,可搭配page,limit,order,group,having使用
 * 
 * @return T 返回javabean
 * @throws Exception if has error
 */
@SuppressWarnings("unchecked") 
public <T> T find() throws Exception{
	this.limit(1);
	try {
		if (buildSql_Select()) {
			BeanHandler<T> beanHandler= new BeanHandler<T>((Class<T>) this.beanClass,new BasicRowProcessor(new BeanProcessor(columnToPropertyOverrides)));
			T bean = new QueryRunner().query(conn, sql,beanHandler, param_where);
			D.autoCloseConn(conn);
			return bean;
		}
	} catch (Exception e) {
		D.closeConn(conn);
		throw e;
	}
	return null;
}
 
Example #2
Source File: DefaultDataAccessor.java    From PeonyFramwork with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T queryEntity(Class<T> entityClass, String sql, Object... params) {
    T result;
    try {
        Map<String, String> columnMap = EntityHelper.getColumnMap(entityClass);
        if (MapUtils.isNotEmpty(columnMap)) {
            result = queryRunner.query(sql, new BeanHandler<T>(entityClass, new BasicRowProcessor(new BeanProcessor(columnMap))), params);
        } else {
            result = queryRunner.query(sql, new BeanHandler<T>(entityClass), params);
        }
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return result;
}
 
Example #3
Source File: DefaultDataAccessor.java    From PeonyFramwork with Apache License 2.0 6 votes vote down vote up
@Override
public <T> List<T> queryEntityList(Class<T> entityClass, String sql, Object... params) {
    List<T> result;
    try {
        Map<String, String> columnMap = EntityHelper.getColumnMap(entityClass);
        if (MapUtils.isNotEmpty(columnMap)) {
            result = queryRunner.query(sql, new BeanListHandler<T>(entityClass, new BasicRowProcessor(new BeanProcessor(columnMap))), params);
        } else {
            result = queryRunner.query(sql, new BeanListHandler<T>(entityClass), params);
        }
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return result;
}
 
Example #4
Source File: DbConnect.java    From mcg-helper with 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;
}
 
Example #5
Source File: DbDeployer.java    From obevo with Apache License 2.0 6 votes vote down vote up
private void validateProperDbUtilsVersion() {
    Package dbutilsPackage = BasicRowProcessor.class.getPackage();
    String dbutilsVersion = dbutilsPackage.getSpecificationVersion();

    if (dbutilsVersion == null) {
        return;  // in case the jar is shaded, this information may not be available; hence, we are forced to return
    }
    String[] versionParts = dbutilsVersion.split("\\.");
    if (versionParts.length < 2) {
        throw new IllegalArgumentException("Improper dbutils version; must have at least two parts x.y; " + dbutilsVersion);
    }

    int majorVersion = Integer.valueOf(versionParts[0]).intValue();
    int minorVersion = Integer.valueOf(versionParts[1]).intValue();

    if (!(majorVersion >= 1 && minorVersion >= 6)) {
        throw new IllegalArgumentException("commons-dbutils:commons-dbutils version must be >= 1.6 to avoid bugs w/ JDBC getColumnName() usage in <= 1.5");
    }
}
 
Example #6
Source File: DefaultDataAccessor.java    From smart-framework with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T queryEntity(Class<T> entityClass, String sql, Object... params) {
    T result;
    try {
        Map<String, String> columnMap = EntityHelper.getColumnMap(entityClass);
        if (MapUtil.isNotEmpty(columnMap)) {
            result = queryRunner.query(sql, new BeanHandler<T>(entityClass, new BasicRowProcessor(new BeanProcessor(columnMap))), params);
        } else {
            result = queryRunner.query(sql, new BeanHandler<T>(entityClass), params);
        }
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return result;
}
 
Example #7
Source File: DefaultDataAccessor.java    From smart-framework with Apache License 2.0 6 votes vote down vote up
@Override
public <T> List<T> queryEntityList(Class<T> entityClass, String sql, Object... params) {
    List<T> result;
    try {
        Map<String, String> columnMap = EntityHelper.getColumnMap(entityClass);
        if (MapUtil.isNotEmpty(columnMap)) {
            result = queryRunner.query(sql, new BeanListHandler<T>(entityClass, new BasicRowProcessor(new BeanProcessor(columnMap))), params);
        } else {
            result = queryRunner.query(sql, new BeanListHandler<T>(entityClass), params);
        }
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return result;
}
 
Example #8
Source File: DefaultDataAccessor.java    From smart-framework with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T queryEntity(Class<T> entityClass, String sql, Object... params) {
    T result;
    try {
        Map<String, String> columnMap = EntityHelper.getColumnMap(entityClass);
        if (MapUtil.isNotEmpty(columnMap)) {
            result = queryRunner.query(sql, new BeanHandler<T>(entityClass, new BasicRowProcessor(new BeanProcessor(columnMap))), params);
        } else {
            result = queryRunner.query(sql, new BeanHandler<T>(entityClass), params);
        }
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return result;
}
 
Example #9
Source File: DefaultDataAccessor.java    From smart-framework with Apache License 2.0 6 votes vote down vote up
@Override
public <T> List<T> queryEntityList(Class<T> entityClass, String sql, Object... params) {
    List<T> result;
    try {
        Map<String, String> columnMap = EntityHelper.getColumnMap(entityClass);
        if (MapUtil.isNotEmpty(columnMap)) {
            result = queryRunner.query(sql, new BeanListHandler<T>(entityClass, new BasicRowProcessor(new BeanProcessor(columnMap))), params);
        } else {
            result = queryRunner.query(sql, new BeanListHandler<T>(entityClass), params);
        }
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return result;
}
 
Example #10
Source File: M.java    From ThinkJD with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> List<T> select() throws Exception{
	try {
		if (buildSql_Select()) {
			BeanListHandler<T> beanListHandler= new BeanListHandler<T>((Class<T>) this.beanClass,new BasicRowProcessor(new BeanProcessor(columnToPropertyOverrides)));
			List<T> beanList = new QueryRunner().query(conn, sql,beanListHandler,param_where);
			D.autoCloseConn(conn);
			return beanList;
		}
	} catch (Exception e) {
		D.closeConn(conn);
		throw e;
	}
	return null;
}
 
Example #11
Source File: TestUtils.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public static List<Map> resultSetToMaps(ResultSet rs) throws SQLException{

        List<Map> results =new ArrayList<>();
        BasicRowProcessor brp = new BasicRowProcessor();

        while(rs.next()){
            results.add(brp.toMap(rs));
        }

        return results;
    }
 
Example #12
Source File: TestUtils.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public static List<Object[]> resultSetToArrays(ResultSet rs) throws SQLException {
    List<Object[]> results =new ArrayList<>();
    BasicRowProcessor brp = new BasicRowProcessor();

    while (rs.next()){
        results.add(brp.toArray(rs));
    }

    return results;
}
 
Example #13
Source File: JdbcResultSetIterator.java    From Quicksql with MIT License 4 votes vote down vote up
public JdbcResultSetIterator(ResultSet resultSet) {
    this.resultSet = resultSet;
    this.convert = new BasicRowProcessor();
}
 
Example #14
Source File: RepositoryDao.java    From Summer with Apache License 2.0 4 votes vote down vote up
protected RepositoryDao() {
    super();
    beanHandler = new BeanHandler<>(getEntityClass(), new BasicRowProcessor(new RepositoryBeanProcessor()));
    beanListHandler = new BeanListHandler<>(getEntityClass(), new BasicRowProcessor(new RepositoryBeanProcessor()));
}
 
Example #15
Source File: EmployeeHandler.java    From tutorials with MIT License 4 votes vote down vote up
public EmployeeHandler(Connection con) {
    super(Employee.class, new BasicRowProcessor(new BeanProcessor(getColumnsToFieldsMap())));
    this.connection = con;
}