DBUtils工具类的使用

看下面这段代码能否找出问题所在 : ( 其实没问题 )

@Test
    /**
     * 使用BeanListHandler
     */
    public void demo5(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        List<Student> list = null;
        try {
            list = queryRunner.query("select * from student", new BeanListHandler<Student>(Student.class));
            for (Student stu : list) {
                System.out.println(stu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

      乍一看 , 好像没什么不好理解的地方 , BeanListHandler<T>(class<T> type) ; 用于将查询到的结果集通过反射封装到泛型指定的 bean 中 , 很对呀 , 没什么不好理解的 , 但是仔细一看 , 泛型填的是 Student , 返回的应该也是使用 Student 来接收才对 , 但是返回的是用 List<Student> 接收的 , 不知道有没有人发现这一点 , 既然不报错 , 那肯定是我们还没有完全理解这个方法 , 让我们进文档看一下 :

BeanListHandler

      原来是因为他的干爹的原因 , 他将泛型对应的返回值使用一个 List 装起来了 . 这才有了 List<Student> .

原文地址:https://www.cnblogs.com/daimajun/p/6628805.html