16.10.2015 Views

Getting Startedwith pureQuery

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13 – Handlers 267<br />

constructor, and uses the same instance every time your application calls the annotated<br />

methods. <strong>pureQuery</strong> recommends that you use this approach as much as you can<br />

because your application will not have to manage instances of the handler, and only one<br />

instance of the handler will be instantiated, no matter how many times your application<br />

invokes the annotated method. However, you might have handlers that do not have public<br />

no-argument constructors. You also might have handlers that store information about the<br />

query result of a query, preventing you from using a single handler for multiple invocations<br />

of an annotated method. For these handlers, you will need to specify the handlers as<br />

parameters to the annotated methods.<br />

13.3.2 Specifying handlers as method parameters<br />

When you want to specify a handler for an annotated method, if you do not want to (or<br />

cannot) specify it by using the @Handler annotation, specify it as a parameter to the<br />

annotated method. When you specify handlers as parameters for an annotated method,<br />

they must be the last parameters to the method. That is, you must first provide any<br />

parameters that provide the information for setting the parameters in the SQL statement,<br />

and then you can provide one or two handlers. Listing 13.6 shows an example of<br />

specifying the IteratorPagingResultHandler for an annotated method.<br />

@Select(sql = "SELECT * FROM EMPLOYEE WHERE WORKDEPT = ?)<br />

Iterator selectEmployeesInDepartment (String workDept,<br />

IteratorPagingResultHandler resultHandler);<br />

Listing 13.6 - An example of specifying a handler as a parameter to an annotated<br />

method<br />

13.3.2.1 In practice: Add an annotated method to HumanResourcesData that<br />

executes the SQL and uses a handler for the query result<br />

The “Company Employees” screen displays all of the employees in the company in pages<br />

of 10 employees each. Let’s use the IteratorPagingResultHandler to retrieve<br />

the employees, one page at a time. Modify the displayAllEmployees method in<br />

hrm.inProgress.pdqMethodStyleWithHandlers.HumanResourcesManager,<br />

along with the utility method selectPageOfEmployees. You will also need to modify the<br />

interface<br />

hrm.inProgress.pdqMethodStyleWithHandlers.HumanResourcesData. Refer<br />

to Section 8.2.1 for the SQL statement that we need to execute.<br />

13.3.2.1.3 Specify annotated method<br />

Declare an annotated method that executes the SQL statement and uses the<br />

IteratorPagingResultHandler to return the results. The<br />

IteratorPagingResultHandler does not have a no-argument constructor, so we<br />

will specify it as a parameter to the annotated method. The return type of the handle<br />

method is ResultIterator. We can use that as the return type of our method, or<br />

we can use Iterator, which is a superinterface of ResultIterator. We

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!