Saving an object

In this recipe, we will create a DAO method to save an object in the database; a row will be added to the corresponding database table, for example:

Saving an object

Getting ready

You need to have a model class, for example:

public class User {
  private Long id;
  private String firstName;
  private Integer age;

You need to have a matching database table, for example:

CREATE TABLE `user` (
  `id` int(11) AUTO_INCREMENT,
  `first_name` text,
  `age` int(11),
  PRIMARY KEY (`id`)
)

You need to have a DAO class with a JdbcTemplate attribute (Refer to the Creating a DAO class recipe)

How to do it…

Define an SQL insert query with question marks as placeholders for the actual ...

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.