1. ResultSetHandler 的作用: QueryRunner 的 query 方法的返回值最终取决于 query 方法的 ResultHandler 参数的 hanlde 方法的返回值.
2. BeanListHandler: 把结果集转为一个 Bean 的 List, 并返回. Bean 的类型在 创建 BeanListHanlder 对象时以 Class 对象的方式传入. 可以适应列的别名来映射 JavaBean 的属性名: String sql = "SELECT id, name customerName, email, birth " + "FROM customers WHERE id = ?"; BeanListHandler(Class<T> type)
3. BeanHandler: 把结果集转为一个 Bean, 并返回. Bean 的类型在创建 BeanHandler 对象时以 Class 对象的方式传入 BeanHandler(Class<T> type)
4. MapHandler: 把结果集转为一个 Map 对象, 并返回. 若结果集中有多条记录, 仅返回 第一条记录对应的 Map 对象. Map 的键: 列名(而非列的别名), 值: 列的值
5. MapListHandler: 把结果集转为一个 Map 对象的集合, 并返回. Map 的键: 列名(而非列的别名), 值: 列的值 6. ScalarHandler: 可以返回指定列的一个值或返回一个统计函数的值.
2、通过QueryRunner实现增删改查
/*** 测试 QueryRunner 的 query 方法*/@SuppressWarnings({ "unchecked", "rawtypes" })@Testpublic void testResultSetHandler(){String sql = "SELECT id, name, email, birth " +"FROM customer";//1. 创建 QueryRunner 对象QueryRunner queryRunner = new QueryRunner();Connection conn = null;try {conn = JdbcTools.getConnection();/*** 2. 调用 query 方法:* ResultSetHandler 参数的作用: query 方法的返回值直接取决于 * ResultSetHandler 的 hanlde(ResultSet rs) 是如何实现的. 实际上, 在* QueryRunner 类的 query 方法中也是调用了 ResultSetHandler 的 handle()* 方法作为返回值的。*/Object object = queryRunner.query(conn, sql, new ResultSetHandler(){@Overridepublic Object handle(ResultSet rs) throws SQLException {List<Customer> customers = new ArrayList<>();while(rs.next()){int id = rs.getInt(1);String name = rs.getString(2);String email = rs.getString(3);Date birth = rs.getDate(4);System.out.println(id+","+name+","+email+","+birth);Customer customer = new Customer(id, name, email, birth);customers.add(customer);}return customers;}}); System.out.println(object); } catch (Exception e) {e.printStackTrace();} finally{JdbcTools.releaseDB(null, null, conn);}}
log输出日志如下
1,aaa,aaa,2016-10-05
2,bbb,bbb,2016-10-11
[Customer [id=1, name=aaa, email=aaa, birth=2016-10-05], Customer [id=2, name=bbb, email=bbb, birth=2016-10-11]]
删除如下:
/*** 测试 QueryRunner 类的 update 方法* 该方法可用于 INSERT, UPDATE 和 DELETE*/@Testpublic void testQueryRunnerUpdate() {//1. 创建 QueryRunner 的实现类QueryRunner queryRunner = new QueryRunner();String sql = "DELETE FROM customer " +"WHERE id IN (?,?)";Connection connection = null;try {connection = JdbcTools.getConnection();//2. 使用其 update 方法int update = queryRunner.update(connection, sql, 1, 2);System.out.println("update:"+update);} catch (Exception e) {e.printStackTrace();} finally{JdbcTools.releaseDB(null, null, connection);}}
3、通过BeanListHandler、BeanHandler实现查询操作
/*** 测试 ResultSetHandler 的 BeanListHandler 实现类* BeanListHandler: 把结果集转为一个 Bean 的 List. 该 Bean* 的类型在创建 BeanListHandler 对象时传入:* * new BeanListHandler<>(Customer.class)* */@Testpublic void testBeanListHandler(){String sql = "SELECT id, name, email, birth " +"FROM customer";//1. 创建 QueryRunner 对象QueryRunner queryRunner = new QueryRunner();Connection conn = null;try {conn = JdbcTools.getConnection();Object object = queryRunner.query(conn, sql, new BeanListHandler<>(Customer.class)); System.out.println(object);ArrayList<Customer> customers = (ArrayList<Customer>) object;for (int i = 0; i < customers.size(); i++) {Customer customer = customers.get(i);System.out.println(customer.getName());}} catch (Exception e) {e.printStackTrace();} finally{JdbcTools.releaseDB(null, null, conn);}}
输出如下:
[Customer [id=1, name=aaa, email=aaa, birth=2016-10-05], Customer [id=2, name=bbb, email=bbb, birth=2016-10-11]]
aaa
bbb
/*** BeanHandler: 把结果集的第一条记录转为创建 BeanHandler 对象时传入的 Class 参数对应的对象.*/@Testpublic void testBeanHanlder() {QueryRunner queryRunner = new QueryRunner();Connection connection = null;try {connection = JdbcTools.getConnection();String sql = "SELECT id, name, email, birth "+ "FROM customer WHERE name = ?";@SuppressWarnings("unchecked")Customer customer = queryRunner.query(connection, sql,new BeanHandler(Customer.class), "bbb");System.out.println(customer);} catch (Exception e) {e.printStackTrace();} finally {JdbcTools.releaseDB(null, null, null, connection);}}
输出如下:
Customer [id=2, name=bbb, email=bbb, birth=2016-10-26]