Dbutils

custom encapsulation - return value type is a single object

String query = "select * from students where id = ?";

Student student = qr.query(query, new ResultSetHandler<Student>() {
    public Student handle(ResultSet rs) throws SQLException {
        Student s = new Student();
        while(rs.next()) {
            String id = rs.getString("id");
            String name = rs.getString("name");
            String clazz = rs.getString("clazz");
            s.setId(id);
            s.setName(name);
            s.setClazz(clazz);
        }
        return s;
    }
}, "helloworld");

System.out.println(student);

custom encapsulation - return value type is collection

String query = "select * from students";
        List<Student> list = qr.query(query, new ResultSetHandler<List<Student>>() {
            public List<Student> handle(ResultSet rs) throws SQLException {
                List<Student> l = new ArrayList<>();
                Student s = new Student();
                while(rs.next()) {
                    s.setId(rs.getString("id"));
                    s.setName(rs.getString("name"));
                    s.setClazz(rs.getString("clazz"));
                    l.add(s);
                }
                return l;
            }
        });
        System.out.println(list);

快包装 - return value type is a single object

String query = "select * from students where id = ?";
Student q = qr.query(query, new BeanHandler<>(Student.class), "helloworld");
System.out.println(q);
/**Console:
*   Student [id=helloworld, name=qwe, clazz=160341B]
*/

快包装 - return value Type is multiple objects

String query = "select * from students";
List<Student> l = qr.query(query, new BeanListHandler<>(Student.class));
System.out.println(l);
/**
 * [Student [id=160341240, name=qwe, clazz=160341B],
 * Student [id=160341244, name=qwe, clazz=160341B],
 * Student [id=aaa, name=Zhan Jinhao, clazz=160341B],
 * Student [id=helloworld, name=qwe, clazz=160341B]]
 */