QreryRunner是Dbutils的核心类之一,它显著的简化了SQL查询,并与ResultSetHandler协同工作将使编码量大为减少。
注意事项
1. 使用QreryRunner必须保证实体类的变量名,和sql语句中要查找的字段名必须相同,否则查询
不到数据,会出现NullPointerException异常
此时sql语句名和数据库名不一致
public Manager selectManagerByName(String name) throws SQLException {
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
String sql = "select mgr_id,login_name,login_pwd ,mgr_name " +
",mgr_gender ,mgr_tel ,mgr_email ,mgr_qq ,create_time " +
"from tb_managers where login_name=?";
Object[] args = {name};
return qr.query(sql,new BeanHandler<>(Manager.class),args);
}
异常
加上别名后可正常查询
public Manager selectManagerByName(String name) throws SQLException {
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
String sql = "select mgr_id mgrId ,login_name loginName,login_pwd loginPwd,mgr_name mgrName" +
",mgr_gender mgrGender,mgr_tel mgrTel,mgr_email mgrEmail,mgr_qq mgrQQ,create_time createTime " +
"from tb_managers where login_name=?";
Object[] args = {name};
return qr.query(sql,new BeanHandler<>(Manager.class),args);
}