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);
自定义封装-返回值类型是集合
String query ="select * from students";List<Student>list= qr.query(query, new ResultSetHandler<List<Student>>() {publicList<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);
快速封装-返回值类型是单个对象
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] */