Spring Boot, the popular Java-based framework, simplifies building production-grade microservices and reusable domain-specific components. When it comes to database integration, Spring Boot provides an efficient and seamless experience. This tutorial will guide you through connecting and working with databases in Spring Boot.

Before we dive in, ensure you have a Java Development Kit (JDK) installed, along with an Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse. You'll also need a database to connect to, such as MySQL, PostgreSQL, or SQLite.
![Spring Boot + MySQL Database example (CRUD) in Java [Tutorial]](https://i.pinimg.com/originals/f6/7a/7a/f67a7aba5610d72b1ab2a15ac066ad86.png)
Setting Up Your Project and Dependencies
Start by creating a new Spring Boot project with Spring Initializr (start.spring.io). Select 'Web' and your preferred database driver (e.g., MySQL Driver, PostgreSQL Driver) to add them as dependencies. For this tutorial, we'll use MySQL.

Once you have your project set up, add the configuration details for your database in the application.properties file:
```bash spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myuser spring.datasource.password=mypassword spring.jpa.hibernate.ddl-auto=update ```
Configuring the DataSource
![Top 7 Spring Boot Online Courses for Beginners in 2025 - Best of Lot [UPDATED]](https://i.pinimg.com/originals/de/ba/04/deba0477e758c632e421324262cf4f0a.png)
Spring Boot automatically configures a HikariCP datasource based on the provided credentials in application.properties. You can further customize the datasource using spring.datasource.hikari.* properties. For instance, to set the maximum number of connections:
```bash spring.datasource.hikari.maximum-pool-size=10 ```
Creating and Accessing Entities
Create a Java class, for example User.java, to map your database table:

```java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // Getters and setters } ```
Spring Boot's EntityManager will automatically handle database operations like saving, finding, and deleting entities based on JPA annotations.
Using Spring Data JPA to Perform Database Operations
Spring Data JPA simplifies working with databases by providing interfaces with predefined methods for common database operations. Create a repository interface for your entity:

```java
public interface UserRepository extends JpaRepository In this example, Spring Data JPA generates implementations for methods like findAll(), save(), and deleteAll(). It also creates a custom query method findByNameContaining().
Injecting and Using the Repository









Inject the repository in your service or controller class using constructor-based injection:
```java
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public List Now, you can use the injected repository to perform database operations as needed.
Working with Transactions
By default, Spring Data JPA manages transactions based on the notification of inserted, updated, or deleted entity instances. You can also explicitly manage transactions in your service layer:
```java
@Service
public class UserService {
// ...
@Transactional
public void saveUsers(List Using the @Transactional annotation ensures that changes are either committed or rolled back as a single unit of work.
Embrace the power of Spring Boot and explore its database features further. Whether you're working with JPA, MongoDB, or another supported database, Spring Boot provides a consistent and productive development experience.