These are some of the advantages of using annotated methods:
You can create a custom interface that follows the Data Access Object (DAO) pattern by writing an interface that creates data access objects. When you finish writing the interface, you run the pureQuery Generator utility to generate the definition of a class that implements the interface. When you write the code for your application, you can create instances of this implementation class and then invoke the methods that are implemented in that instance. You can use Java objects, beans, and collections for providing parameters to your methods and for receiving the results of queries.
For example, the following method uses an @Select annotation and could appear in an interface named CustomerQuery, in which you define methods for querying or updating records in a table named CUSTOMER.
@Select(sql="SELECT CUSTID, NAME FROM CUSTOMER WHERE REGION = ?1")
List<Customer> getCustomersInRegion(int r);
After you compile the interface and use the pureQuery Generator utility to create a class named CustomerQueryImpl that implements the interface, an application can invoke the method and retrieve the query results from the returned List.
Connection con = DriverManager.getConnection(...);
CustomerQuery cQuery = DataFactory.getData(CustomerQuery.class, con);
int region = 123;
List<Customer> customersInRegion = cQuery.getCustomersInRegion(region);
The code performs these steps:
Note that the code directly refers to the CustomerQuery interface, not the implementation class. You can compile your applications without a dependency on the implementation class having been generated.
You can use an XML file to override the SQL statements that are in the annotations on methods that are defined in an interface. You provide the SQL statements in the XML file and provide that file to the pureQuery Generator utility when you generate the implementation class for the interface. In this way, you can reuse interfaces in more than one application when those applications are associated with different databases.
For example, you might have one application that works with a DB2 database and a similar application that works with an Oracle database. An interface named Customer might contain SQL statements that are compatible with the DB2 database. To reuse the interface for the second application, when you generate the implementation class for the Customer interface, you would provide the pureQuery Generator utility with an XML file with SQL statements that are compatible with the Oracle database.