当前位置: 代码迷 >> 综合 >> Dbutils - 查询(11)
  详细解决方案

Dbutils - 查询(11)

热度:89   发布时间:2023-12-13 06:01:43.0

自定义封装-返回值类型是单个对象

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>>() {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);

快速封装-返回值类型是单个对象

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] */

快速封装-返回值类型是多个对象

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=詹金浩, clazz=160341B], * Student [id=helloworld, name=qwe, clazz=160341B]]*/