Skip to content
Model centric tools for Spring development Community portal
Skyway PerspectivesModel driven delivery

MyEclipse for Spring

Code Reference: CustomerDaoImpl.java

The data access implementation behind the customer service, showing how a persistence template is used for reads and writes.

Four stacked application layers for Spring MVC and Spring Web Flow
Figure 23. The data access implementation is the only class that talks to the template.

CustomerDaoImpl implements the customer data access interface. It is the only class in the sample that talks to the persistence template. Everything above it works with plain Java types, which keeps the service layer free of persistence details.

Methods

  • findById. Reads one record by key and returns null when no row matches.
  • findAll. Reads every record ordered by name.
  • findByEmail. Reads one record by the unique email value.
  • save. Creates or updates a record and returns the managed instance.
  • delete. Removes a record by key and returns the number of affected rows.

Template usage

Each method is a short call to the template. Reads pass a query and a row mapper. Writes pass the entity. No method contains business logic, and none opens a transaction. The transaction boundary belongs to the service layer, which is one level up.

Row mapping. Keep row mapping in one place per entity. A second mapper for the same table drifts over time and produces fields that disagree between screens.

The class is generated. If the project needs a hand written query, add it to the model as a named query so that it is regenerated with the rest of the layer.