DbUtils (1) result set overview and dbutils result Overview
Record your learning and understanding of DbUtils
I feel that the most common use of Dbutils is to process the query result set, so I will start to understand the Dbutils library.
The conversion of the source code discovery result set is mainly used for the query, insert, and insertBatch methods.
// Only list the relevant code of the QueryRunner class, others skipped // ----------- source code 1 query method ------------------ stmt = this. prepareStatement (conn, SQL); this. fillStatement (stmt, params); rs = this.wrap(stmt.exe cuteQuery (); result = rsh. handle (rs); // processing result set // ----------- source code 2 insert method ------------------ stmt = conn. prepareStatement (SQL, tatement. RETURN_GENERATED_KEYS );
This. fillStatement (stmt, params); stmt.exe cuteUpdate (); ResultSet resultSet = stmt. getGeneratedKeys (); generatedKeys = rsh. handle (resultSet); // processing result set // ------------- source code 3 insertBatch method ---------------- ResultSet rs = stmt. getGeneratedKeys (); generatedKeys = rsh. handle (rs); // process the result set
The conversion of ResultSet mainly involves two interfaces (ResultSetHandler <T>AndRowProcessor).
1,ResultSetHandler <T>,The diagram is as follows:
The ResultSetHandler interface has only one handle (ResultSet rs) method to be implemented. In my understanding, this method is an entry for processing the result set, each result set conversion class processes its own tasks in its own handle method. This can be seen from the previous source code 1, 2, and 3. It is used to call the handle method of the implementation class, then return the result.
The implementation class of ResultSetHandler (result set conversion class) implements processing of the result set in the handle method. Most of the operations involve another interface.RowProcessorBasicRowProcessor.
2,RowProcessor, the relationship diagram is as follows:
The RowProcessor interface has four methods to implement. The specific usage can be intuitively seen from the method name. These methods are called in the handle method of the result conversion class. Note that if you need to convert the result set to Bean, the use of BeanProcessor class will also be involved.
3,ResultSetHandler andRowProcessor relationship
Although the figure is a bit messy, you can see it clearly. Most result sets hold a reference to the RowProcessor interface. Note that ArrayHandler is a class, because references to RowProcessor of other classes are obtained from this class. This class creates the RowProcessor implementation class BasicRowProcessor object.
Note that the three abstract classes (AbstractKeyedHandler, AbstractListHandler, and BaseResultSetHandler) can be used as result conversion classes. According to the instructions in the document, if none of the officially provided conversion classes meet your requirements, you can implement your own result set conversion class by inheriting BaseResultSetHandler.