Skip to content

The DAO Interfaces

Muhammad Hewedy edited this page Mar 11, 2017 · 4 revisions

The DAO interface is a regular interface you create to contains methods that are mapped to Stored Procedures.

Commonly you will have multiple DAO interfaces for example CustomerDAO, SalesDAO, BillingDAO, etc...

Sample DAO interface:

public interface BillingDAO{
	
	@Scalar(VARCHAR)
	@StoredProc("proc_generate_invoice")
	String generateInvoice(@Param(BIGINT) Long userId);
	
	@StoredProc("proc_deduce_balance")
	void deduceBalance(@Param(BIGINT) Long userId, @Param(DOUBLE) Double amount);
	
	@StoredProc("proc_get_user_invoices")
	List<Invoice> getUserInvoices(@Param(BIGINT) Long userId);

	@AutoMapper
	@StoredProc("proc_get_user_invoice_numbers")
	List<String> getUserInvoiceNumbers(@Param(BIGINT) Long userId);
}

And here's how to create the DAO instance to use:

DataSource dataSource = ....
BillingDAO billingDAO = new DAO.Builder(dataSource).build().create(BillingDAO.class);

NOTE: It is very common to cache the Your DAOs instances and reuse them, see how to accomplish this with spring framework.

NOTE:You can read more about which Mapper to use

DAO methods

The DAO interface methods can take any number of parameters (that equals to Stored Proc input parameters each should be annotated by @Param) and can return one of:

  • void, when the stored proc has no result set or output parameters.
  • List of some type (e.g.: List<Customer>, List<String>, List<Long>, etc...), when the stored proc has result set only.
  • Object of some type (e.g.: Customer, String, Invoice, etc...), when the output parameters only.
  • spwrap.Tuple, when the stored proc has both result set and output parameters.

To obtains the result from the Tuple use myTuple.list() and myTuple.object() to return the result set and the output parameters respectively. (of course you will need to map these result set/output parameters)

NOTE:The DAO interface need each method to be annotated at least with @StoredProc annotation, otherwise it will be ignored and when you call it, you will get null.