Spring Boot CRUD operations

To code simple CRUD operations in Spring Boot, a software developer must choose between one of two approaches:

  1. Heavily control the SQL and use the Spring JDBC Starter's JdbcTemplate.
  2. Let the Spring framework handle the SQL and use Spring Data's CrudRepository.

How to use Spring's JdbcTemplate

Once you configure the JDBC URL and credentials in your application.properties or YAML config file, you can autowire the Spring JdbcTemplate into any class.

The following example uses Spring's JdbcTemplate to create a new record in a database. This demonstrates a create operation, but the rest of the Spring Boot CRUD operations follow the same template. SQL is passed to the update or execute method, and a database transaction occurs.

@Component
public class JdbcTaskDao {
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	public void create(Task task) {
		var sql = "INSERT INTO TASK (name, completed) VALUES (?, ?)";
		jdbcTemplate.update(sql, "Joe", false);
	}
} 

Spring Data vs. Spring JDBC

Spring's JdbcTemplate makes database connectivity much easier than plain JDBC, but it's no match for Spring Data's CrudRepository.

To use the CrudRepository, simply extend the interface and specify the associated data type and primary key type. This example implements a persistence layer for a to-do list application, so the associated data type is a Task, and the associated primary key is of type Integer:

public interface TaskRepository extends CrudRepository<Task, Integer> { }

public class Task {
	
	@Id
	int id;
	String name;
	boolean completed;
}

Additionally, as seen above, the primary key in the associated class must be decorated with an @Id annotation, but when that's done, the developer can perform Spring Boot CRUD operations without writing any SQL.

Spring Data CRUD operations

The following example shows the implementation of every CRUD operation without once having to write any SQL:

@Component
public class SpringCrudExample implements TaskDAO{

	@Autowired
	TaskRepository taskRepository;

	public void create(Task task) {
		taskRepository.save(task);
	}

	public Iterable<Task> retrieveAll() {
		return taskRepository.findAll();
	}

	public void update(Task task) {
		taskRepository.save(task);
	}

	public void delete(Task task) {
		taskRepository.delete(task);
	}
}

Create, Read, Update and Delete in Spring Boot

Which approach you take to coding CRUD operations in Spring Boot depends heavily upon your specific needs and requirements. My preference is to take the simplified, object-relational mapping approach and use Spring Data's CrudRepository to relieve myself of the ongoing responsibility of maintaining SQL. The less work a software developer needs to do, the better.

Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development; DevOps; Spring; and container-based technologies such as Docker, Swarm and Kubernetes.

View All Videos
App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close