Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
Database Integrations in Spring Boot

Database Integrations in Spring Boot

Posted on March 12, 2025 by Toma Velev

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

  • Skipping AI? You’re a Relic – Time to Evolve or Perish!
  • 2026 Flutter Launch Blueprint: Your 10-Step Checklist to App Store Domination
  • Product Requirements Document – for different software development levels
  • The Bottleneck Factory: AI Production Line vs. Human Quality Gate
  • Most Important Things You Could Code in 2026

Categories

  • Apps (25)
  • ChatGPT (26)
  • Choosing a Framework (38)
  • Flutter (280)
  • Graphical User Interface (14)
  • Marketing (119)
  • Software Development (292)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (16)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (87)
    • Flutter Apps (26)
    • GPT (4)
    • Java (39)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (36)
    • Utils (15)
    • Vaadin 24+ (28)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Skipping AI? You’re a Relic – Time to Evolve or Perish!
  • 2026 Flutter Launch Blueprint: Your 10-Step Checklist to App Store Domination
  • Product Requirements Document – for different software development levels
  • The Bottleneck Factory: AI Production Line vs. Human Quality Gate
  • Most Important Things You Could Code in 2026

Post Categories

  • Apps (25)
  • ChatGPT (26)
  • Choosing a Framework (38)
  • Flutter (280)
  • Graphical User Interface (14)
  • Marketing (119)
  • Software Development (292)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (16)