Creating a DAO class

In this recipe, we will create a DAO (data access object) class. A DAO class provides methods to save and retrieve objects from the database. It can be used from a controller, for example:

Creating a DAO class

The controller calls the findUsers() method from UserDAO, which takes care of getting the results from the database (using the JdbcTemplate bean defined in the previous recipe).

How to do it…

Here are the steps to create a DAO class:

  1. Create a class annotated with @Repository:
    @Repository
    public class UserDAO {
  2. Add an autowired JdbcTemplate field to it:
    @Autowired
    private JdbcTemplate jdbcTemplate;

How it works…

@Repository allows the UserDAO class ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.