Here are minimal examples of different Database Integrations in Spring Boot that you could choose from in your applications.
1. JdbcTemplate (Plain JDBC)
JdbcTemplate is a lower-level approach that directly executes SQL queries.
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class UserJdbcRepository {
private final JdbcTemplate jdbcTemplate;
public UserJdbcRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<User> findAll() {
return jdbcTemplate.query("SELECT id, name FROM users", new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
return new User(rs.getLong("id"), rs.getString("name"));
}
});
}
}
2. JPA with EntityManager
JPA with EntityManager
is a standard way to work with entities in a more flexible manner.
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserJpaRepository {
@PersistenceContext
private EntityManager entityManager;
public List<User> findAll() {
return entityManager.createQuery("SELECT u FROM User u", User.class).getResultList();
}
}
3. Hibernate with SessionFactory
Hibernate SessionFactory
provides more control over transactions and caching.
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserHibernateRepository {
@PersistenceContext
private EntityManager entityManager;
private Session getSession() {
Session session = null;
if (entityManager == null
|| (session = entityManager.unwrap(Session.class)) == null) {
throw new NullPointerException();
}
return session;
}
public List<User> findAll() {
return getSession().createQuery("from Platform", Platform.class).list();
}
}
4. Spring Data JPA Repository (Recommended)
This is the easiest and most common approach, using Spring’s CrudRepository
or JpaRepository
.
Implementation:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
Usage:
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
private final UserRepository userRepository;
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping
public List<User> getUsers() {
return userRepository.findAll();
}
}
Summary:
Approach | Pros | Cons |
JdbcTemplate | Fine-grained control, better performance | More boilerplate code |
JPA (EntityManager) | Standard API, more flexibility | Requires configuration |
Hibernate Sessions | Powerful ORM features, caching | Requires managing sessions manually |
Spring Data JPA | Easiest to use, built-in methods | Less fine-grained control |
For most applications, Spring Data JPA (Repositories) is the best choice due to its simplicity. I use it primarily in my Spring Boot Apps