|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface ResultHandler<RES>
Processes the entire query result of an SQL statement and returns the contents in an object of type RES
.
For annotated and inline methods that execute SQL queries, pureQuery generally uses the return type of the method to determine how to return the query result. pureQuery provides a wide variety of formats in which a query result can be returned. When a format is required that is not one of the standard formats provided by pureQuery, an implementation of ResultHandler<RES>
can be specified to create the object that represents the query result.
ResultHandler<RES>
implementation can be specified by using the method Data.query(String, ResultHandler, Object...)
.ResultHandler<RES>
implementation can be specified in one of two ways.
@Handler(resultHandler=...)
annotation. In this approach, pureQuery creates a single instance of the implementation, and uses that instance every time the annotated method is invoked.@Handler(resultHandler=...)
annotation. When one or more handlers are provided as parameters to an annotated method, the handlers must be the last parameters to the method.Specifying a ResultHandler<RES>
implementation causes its handle(ResultSet)
method to be used to process the results of the SQL execution and to create the object that describes the results. The created object is returned from the associated annotated or inline method.
ResultHandler<RES>
implementationsFor simple examples of how ResultHandler<RES>
s can be implemented, see JSONResultHandler
and XMLResultHandler
.
ResultHandler<RES>
implementation for an inline methodThe following example demonstrates the basic syntax for specifying a ResultHandler<RES>
implementation for an inline method. It uses the IteratorPagingResultHandler
to return rows 11 through 20 of the query result.
Connection connection = DriverManager.getConnection (...);
Data data = DataFactory.getData (connection);
IteratorPagingResultHandler<EmployeeBean> handler = new IteratorPagingResultHandler<EmployeeBean> (EmployeeBean.class, 11, 20);
DepartmentBean department = ...;
Iterator<EmployeeBean> employees = data.query ("select * from employee where workdept = ?1.departmentNumber", handler, department);
ResultHandler<RES>
implementations for annotated methodsThe following two examples demonstrate the basic syntax for specifying a ResultHandler<RES>
implementation for an annotated method. The two examples assume that the annotated methods are declared in an interface named SampleInterfaceData
. The first example uses the JSONResultHandler
. Because the JSONResultHanlder
is instantiated with a no-argument constructor, the handler is specified in the @Handler(resultHandler=...)
annotation. The annotated method could be declared in an interface like this:
@Select(sql = "select * from employee where workdept = ?1.departmentNumber")
@Handler(resultHandler = JSONResultHandler.class)
String selectEmployeesInDepartmentAsJSONString (DepartmentBean department);
Then, after the pureQuery Generator is used to generate the implementation class for the interface, the method could be invoked like this:
Connection connection = DriverManager.getConnection (...);
SampleInterfaceData sampleInterfaceData = DataFactory.getData (SampleInterfaceData.class, connection);
DepartmentBean department = ...;
String employees = sampleInterfaceData.selectEmployeesInDepartmentAsJSONString (department);
The next example uses the IteratorPagingResultHandler
. Because the IteratorPagingResultHandler
does not have a no-argument constructor, the handler is specified as a parameter to the annotated method. The annotated method could be declared in an interface like this:
@Select(sql = "select * from employee where workdept = ?1.departmentNumber")
Iterator<EmployeeBean> selectEmployeesInDepartment (DepartmentBean department, IteratorPagingResultHandler resultHandler);
Then, after the pureQuery Generator is used to generate the implementation class for the interface, the method could be invoked to return rows 11 through 20 of the query result like this:
Connection connection = DriverManager.getConnection (...);
SampleInterfaceData sampleInterfaceData = DataFactory.getData (SampleInterfaceData.class, connection);
IteratorPagingResultHandler<EmployeeBean> handler = new IteratorPagingResultHandler<EmployeeBean> (EmployeeBean.class, 11, 20);
DepartmentBean department = ...;
Iterator<EmployeeBean> employees = sampleInterfaceData.selectEmployeesInDepartment (department, handler);
Data.query(String, ResultHandler, Object...)
, IteratorPagingResultHandler
, JSONResultHandler
, XMLResultHandler
Modifier and Type | Method and Description |
---|---|
RES |
handle(ResultSet resultSet) Processes an entire ResultSet for an SQL statement and returns the contents in an object of type RES . |
RES handle(ResultSet resultSet)
ResultSet
for an SQL statement and returns the contents in an object of type RES
.resultSet
- a ResultSet
that represents the results from an SQL statementRES
that contains the entire contents of resultSet
(or as much of the contents of resultSet
as is wanted)
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |