日期:2014-05-17  浏览次数:20723 次

QueryRunner问题,在线等!
QueryRunner qr = new QueryRunner();
results =  qr.query(conn, sql, new BeanListHandler<T>(module),param);
代码如上,参数conn为数据库连接,sql为一个存储过程,param为sql所需的参数,请问下new BeanListHandler<T>(module)是什么,麻烦详细点。

------解决方案--------------------
去看DBUtils框架的API啊,之前用过,现在忘了。。
------解决方案--------------------
BeanListHandler应该是自己继承 ResultSetHandler 的类吧,ResultSetHandler 是用来将你查询的返回结果转换成一个对象。具体还是看API。

 query

public <T> T query(Connection conn,
                   String sql,
                   ResultSetHandler<T> rsh,
                   Object... params)
        throws SQLException

    Execute an SQL SELECT query with replacement parameters. The caller is responsible for closing the connection.

    Type Parameters:
        T - The type of object that the handler returns
    Parameters:
        conn - The connection to execute the query in.
        sql - The query to execute.
        rsh - The handler that converts the results into an object.
        params - The replacement parameters. 
    Returns:
        The object returned by the handler. 
    Throws:
        SQLException - if a database access error occurs


------解决方案--------------------
http://commons.apache.org/proper/commons-dbutils/apidocs/org/apache/commons/dbutils/QueryRunner.html#query(java.sql.Connection, java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...)
你调用的是这个方法。
queryRunner.query(sql, new BeanListHandler( UserInfo.class));

就是把你查询到的返回结果转化成一个 UserInfo 对象。
------解决方案--------------------
conn:创建的连接;
sql:查询语句;
new BeanListHandler<T>(module):返回值得类型,是个list,如查询的结果是List<Test>的话,可以写成,new BeanListHandler<T>(Test.class);
param:sql语句中的参数;
差不多是这个意思