类图
源码分析
package com.wnl.utils;
public class StudyUtils {
/**
* 结果处理器:
*/
/*
* (1) 父接口: Interface ResultSetHandler<T>
* 这个接口只有一个抽象方法,用来把结果集加入到JavaBean中
* 具体实现看源代码:
* public abstract interface ResultSetHandler<T>
* {
* public abstract T handle(ResultSet paramResultSet)hrows SQLException; }
*
*
* 来看看ResultSetHandler<T> 的抽象子类AbstractListHandler<T>
* 看看器源码实现:
* public abstract class AbstractListHandler<T>
implements ResultSetHandler<List<T>>
{
public List<T> handle(ResultSet rs)
throws SQLException
{
List rows = new ArrayList();
while (rs.next())
rows.add(handleRow(rs));
return rows;
}
protected abstract T handleRow(ResultSet paramResultSet)
throws SQLException;
*
* 分析可以知道,AbstractListHandler<T>把每一行结果集转成T数组,再把T数组放在List中
*
* (2) 子类 BeanHandler<T> 把结果集数据加到JavaBean中
* 1 里面有两个构造方法:
* public BeanHandler(Class<T> type){this(type, ArrayHandler.ROW_PROCESSOR);}
* public BeanHandler(Class<T> type, RowProcessor convert){
* this.type = type;
* this.convert = convert; }
*
* 分析此构造方法前,我们先看看RowProcessor的实现
* RowProcessor是一个接口类,可以使得子类可以把多行结果集 加到不同的Object对象中
* 看源码:
* public abstract interface RowProcessor
{
public abstract Object[] toArray(ResultSet paramResultSet)
throws SQLException;
public abstract <T> T toBean(ResultSet paramResultSet, Class<T> paramClass)
throws SQLException;
public abstract <T> List<T> toBeanList(ResultSet paramResultSet, Class<T> paramClass)
throws SQLException;
public abstract Map<String, Object> toMap(ResultSet paramResultSet)
throws SQLException;
}
* 由RowProcessor源码知道,可以把结果集数据加到Array、Bean、Map类型数据集合中
*
* 通过上面源码分析,可知第二个构造方法可以实现处理多行结果集数据
*
* 2 handle方法的实现
* public T handle(ResultSet rs)
throws SQLException
{
return ((rs.next()) ? this.convert.toBean(rs, this.type) : null);
}
*
* 可知,游标rs只是移动一次,即只能处理一行结果集数据
*
*
* (3)子类 BeanListHandler<T> 把所有结果集数据加到Bean中,是一个bean List集合
*
* (4) 子类 ArrayHandler 只能处理第一行数据, handle方法返回Object[]数组
* public Object[] handle(ResultSet rs) throws SQLException
{
return ((rs.next()) ? this.convert.toArray(rs) : null);
}
* (5 )子类 ArrayListHandler 返回所有结果集数据,每一行就是一个Object[]数组,再把Object[]数组放在List中
* handle方法:
*
* protected Object[] handleRow(ResultSet rs)
throws SQLException
{
return this.convert.toArray(rs);
}
*
*
* (6) 子类ColumnListHandler
* 看源码:
* public class ColumnListHandler extends AbstractListHandler<Object>
{
private final int columnIndex;
private final String columnName;
public ColumnListHandler()
{
this(1, null);
}
public ColumnListHandler(int columnIndex)
{
this(columnIndex, null);
}
public ColumnListHandler(String columnName)
{
this(1, columnName);
}
private ColumnListHandler(int columnIndex, String columnName)
{
this.columnIndex = columnIndex;
this.columnName = columnName;
}
protected Object handleRow(ResultSet rs)
throws SQLException
{
if (this.columnName == null)
return rs.getObject(this.columnIndex);
return rs.getObject(this.columnName);
}
}
* (7) 子类 KeyedHandler
*
* 看源码:
*
* public class KeyedHandler extends AbstractKeyedHandler<Object, Map<String, Object>>
{
protected final RowProcessor convert;
protected final int columnIndex;
protected final String columnName;
public KeyedHandler()
{
this(ArrayHandler.ROW_PROCESSOR, 1, null);
}
public KeyedHandler(RowProcessor convert)
{
this(convert, 1, null);
}
public KeyedHandler(int columnIndex)
{
this(ArrayHandler.ROW_PROCESSOR, columnIndex, null);
}
public KeyedHandler(String columnName)
{
this(ArrayHandler.ROW_PROCESSOR, 1, columnName);
}
private KeyedHandler(RowProcessor convert, int columnIndex, String columnName)
{
this.convert = convert;
this.columnIndex = columnIndex;
this.columnName = columnName;
}
protected Object createKey(ResultSet rs)
throws SQLException
{
return ((this.columnName == null) ? rs.getObject(this.columnIndex) : rs.getObject(this.columnName));
}
protected Map<String, Object> createRow(ResultSet rs)
throws SQLException
{
return this.convert.toMap(rs);
}
}
*
* 由源码分析可以知道,此类把结果集的每一行转成Map<String, Object>,再把每一个Map<String, Object>放进Map里面
*
* 首先,判断有没有传入指定列名,如果没有,则判断有没有传入列的索引值,如果也没用,就是要默认值1作为列的索引值来处理数据
*
*
* 其他子类就直接帖代码:
* public class ScalarHandler
implements ResultSetHandler<Object>
{
private final int columnIndex;
private final String columnName;
public ScalarHandler()
{
this(1, null);
}
public ScalarHandler(int columnIndex)
{
this(columnIndex, null);
}
public ScalarHandler(String columnName)
{
this(1, columnName);
}
private ScalarHandler(int columnIndex, String columnName)
{
this.columnIndex = columnIndex;
this.columnName = columnName;
}
public Object handle(ResultSet rs)
throws SQLException
{
if (rs.next()) {
if (this.columnName == null)
return rs.getObject(this.columnIndex);
return rs.getObject(this.columnName);
}
return null;
}
}
*
*
* package org.apache.commons.dbutils.handlers;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;
public class MapHandler
implements ResultSetHandler<Map<String, Object>>
{
private final RowProcessor convert;
public MapHandler()
{
this(ArrayHandler.ROW_PROCESSOR);
}
public MapHandler(RowProcessor convert)
{
this.convert = convert;
}
public Map<String, Object> handle(ResultSet rs)
throws SQLException
{
return ((rs.next()) ? this.convert.toMap(rs) : null);
}
}
*
*
* package org.apache.commons.dbutils.handlers;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import org.apache.commons.dbutils.RowProcessor;
public class MapListHandler extends AbstractListHandler<Map<String, Object>>
{
private final RowProcessor convert;
public MapListHandler()
{
this(ArrayHandler.ROW_PROCESSOR);
}
public MapListHandler(RowProcessor convert)
{
this.convert = convert;
}
protected Map<String, Object> handleRow(ResultSet rs)
throws SQLException
{
return this.convert.toMap(rs);
}
}
*/
/**
* QueryRunner类是此开源库的核心类,JDBC的增删改查操作都由此类完成
*
package org.apache.commons.dbutils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
public class QueryRunner extends AbstractQueryRunner
{
public QueryRunner()
{
}
public QueryRunner(boolean pmdKnownBroken)
{
super(pmdKnownBroken);
}
public QueryRunner(DataSource ds)
{
super(ds);
}
public QueryRunner(DataSource ds, boolean pmdKnownBroken)
{
super(ds, pmdKnownBroken);
}
public int[] batch(Connection conn, String sql, Object[][] params)
throws SQLException
{
return batch(conn, false, sql, params);
}
public int[] batch(String sql, Object[][] params)
throws SQLException
{
Connection conn = prepareConnection();
return batch(conn, true, sql, params);
}
private int[] batch(Connection conn, boolean closeConn, String sql, Object[][] params)
throws SQLException
{
if (conn == null) {
throw new SQLException("Null connection");
}
if (sql == null) {
if (closeConn)
close(conn);
throw new SQLException("Null SQL statement");
}
if (params == null) {
if (closeConn)
close(conn);
throw new SQLException("Null parameters. If parameters aren't need, pass an empty array.");
}
PreparedStatement stmt = null;
int[] rows = null;
try {
stmt = prepareStatement(conn, sql);
for (int i = 0; i < params.length; ++i) {
fillStatement(stmt, params[i]);
stmt.addBatch();
}
rows = stmt.executeBatch();
}
catch (SQLException e) {
rethrow(e, sql, (Object[])params);
} finally {
close(stmt);
if (closeConn)
close(conn);
}
return rows;
}
@Deprecated
public <T> T query(Connection conn, String sql, Object param, ResultSetHandler<T> rsh)
throws SQLException
{
return query(conn, false, sql, rsh, new Object[] { param });
}
@Deprecated
public <T> T query(Connection conn, String sql, Object[] params, ResultSetHandler<T> rsh)
throws SQLException
{
return query(conn, false, sql, rsh, params);
}
public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object[] params)
throws SQLException
{
return query(conn, false, sql, rsh, params);
}
public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh)
throws SQLException
{
return query(conn, false, sql, rsh, (Object[])null);
}
@Deprecated
public <T> T query(String sql, Object param, ResultSetHandler<T> rsh)
throws SQLException
{
Connection conn = prepareConnection();
return query(conn, true, sql, rsh, new Object[] { param });
}
@Deprecated
public <T> T query(String sql, Object[] params, ResultSetHandler<T> rsh)
throws SQLException
{
Connection conn = prepareConnection();
return query(conn, true, sql, rsh, params);
}
public <T> T query(String sql, ResultSetHandler<T> rsh, Object[] params)
throws SQLException
{
Connection conn = prepareConnection();
return query(conn, true, sql, rsh, params);
}
public <T> T query(String sql, ResultSetHandler<T> rsh)
throws SQLException
{
Connection conn = prepareConnection();
return query(conn, true, sql, rsh, (Object[])null);
}
private <T> T query(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object[] params)
throws SQLException
{
if (conn == null) {
throw new SQLException("Null connection");
}
if (sql == null) {
if (closeConn)
close(conn);
throw new SQLException("Null SQL statement");
}
if (rsh == null) {
if (closeConn)
close(conn);
throw new SQLException("Null ResultSetHandler");
}
PreparedStatement stmt = null;
ResultSet rs = null;
Object result = null;
try
{
stmt = prepareStatement(conn, sql);
fillStatement(stmt, params);
rs = wrap(stmt.executeQuery());
result = rsh.handle(rs);
}
catch (SQLException e) {
rethrow(e, sql, params);
}
finally {
try {
close(rs);
} finally {
close(stmt);
if (closeConn)
close(conn);
}
}
return result;
}
public int update(Connection conn, String sql)
throws SQLException
{
return update(conn, false, sql, (Object[])null);
}
public int update(Connection conn, String sql, Object param)
throws SQLException
{
return update(conn, false, sql, new Object[] { param });
}
public int update(Connection conn, String sql, Object[] params)
throws SQLException
{
return update(conn, false, sql, params);
}
public int update(String sql)
throws SQLException
{
Connection conn = prepareConnection();
return update(conn, true, sql, (Object[])null);
}
public int update(String sql, Object param)
throws SQLException
{
Connection conn = prepareConnection();
return update(conn, true, sql, new Object[] { param });
}
public int update(String sql, Object[] params)
throws SQLException
{
Connection conn = prepareConnection();
return update(conn, true, sql, params);
}
private int update(Connection conn, boolean closeConn, String sql, Object[] params)
throws SQLException
{
if (conn == null) {
throw new SQLException("Null connection");
}
if (sql == null) {
if (closeConn)
close(conn);
throw new SQLException("Null SQL statement");
}
PreparedStatement stmt = null;
int rows = 0;
try
{
stmt = prepareStatement(conn, sql);
fillStatement(stmt, params);
rows = stmt.executeUpdate();
}
catch (SQLException e) {
rethrow(e, sql, params);
}
finally {
close(stmt);
if (closeConn)
close(conn);
}
return rows;
}
}
*/
}