Spring Boot
Hibernate
MySQL
MVC
Web Development

Running a MVC app using Spring Boot Hibernate MySql

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Running a Spring MVC application with Spring Boot, Hibernate, and MySQL is mostly about getting three layers aligned: web, persistence, and database configuration. When the stack fails to start, the root cause is usually not MVC itself but a mismatch in dependencies, datasource settings, entity mapping, or view resolution.

Start With the Right Dependencies

For a typical server-rendered MVC app, you usually need:

  • Spring Web for controllers and routing
  • Spring Data JPA for persistence
  • a template engine such as Thymeleaf
  • the MySQL JDBC driver

In Maven, that often looks like this:

xml
1<dependencies>
2  <dependency>
3    <groupId>org.springframework.boot</groupId>
4    <artifactId>spring-boot-starter-web</artifactId>
5  </dependency>
6  <dependency>
7    <groupId>org.springframework.boot</groupId>
8    <artifactId>spring-boot-starter-thymeleaf</artifactId>
9  </dependency>
10  <dependency>
11    <groupId>org.springframework.boot</groupId>
12    <artifactId>spring-boot-starter-data-jpa</artifactId>
13  </dependency>
14  <dependency>
15    <groupId>com.mysql</groupId>
16    <artifactId>mysql-connector-j</artifactId>
17    <scope>runtime</scope>
18  </dependency>
19</dependencies>

Keep the driver aligned with the Spring Boot version you are using. Most modern Boot projects should avoid manually mixing old JDBC driver coordinates with newer Boot versions.

Configure MySQL and JPA

A minimal application.properties file looks like this:

properties
1spring.datasource.url=jdbc:mysql://localhost:3306/appdb?useSSL=false&serverTimezone=UTC
2spring.datasource.username=appuser
3spring.datasource.password=apppass
4
5spring.jpa.hibernate.ddl-auto=update
6spring.jpa.show-sql=true
7spring.jpa.properties.hibernate.format_sql=true

This is enough for local development, but it is not the final production shape. For production, schema migration tools such as Flyway or Liquibase are safer than relying on ddl-auto=update.

Still, for getting a local MVC app running, the above configuration is often a practical starting point.

Build a Minimal MVC and Persistence Flow

A clean first version should include:

  • an entity
  • a repository
  • a service
  • a controller
  • a view template

Entity:

java
1import jakarta.persistence.Entity;
2import jakarta.persistence.GeneratedValue;
3import jakarta.persistence.GenerationType;
4import jakarta.persistence.Id;
5
6@Entity
7public class Book {
8
9    @Id
10    @GeneratedValue(strategy = GenerationType.IDENTITY)
11    private Long id;
12
13    private String title;
14    private String author;
15
16    public Long getId() {
17        return id;
18    }
19
20    public String getTitle() {
21        return title;
22    }
23
24    public void setTitle(String title) {
25        this.title = title;
26    }
27
28    public String getAuthor() {
29        return author;
30    }
31
32    public void setAuthor(String author) {
33        this.author = author;
34    }
35}

Repository:

java
1import org.springframework.data.jpa.repository.JpaRepository;
2
3public interface BookRepository extends JpaRepository<Book, Long> {
4}

Controller:

java
1import org.springframework.stereotype.Controller;
2import org.springframework.ui.Model;
3import org.springframework.web.bind.annotation.GetMapping;
4
5@Controller
6public class BookController {
7
8    private final BookRepository repository;
9
10    public BookController(BookRepository repository) {
11        this.repository = repository;
12    }
13
14    @GetMapping("/books")
15    public String books(Model model) {
16        model.addAttribute("books", repository.findAll());
17        return "books";
18    }
19}

Template:

html
1<!DOCTYPE html>
2<html xmlns:th="http://www.thymeleaf.org">
3<body>
4  <h1>Books</h1>
5  <ul>
6    <li th:each="book : ${books}" th:text="${book.title}"></li>
7  </ul>
8</body>
9</html>

With this structure, visiting /books should render the view and load data through Hibernate from MySQL.

How to Run It Locally

The usual local workflow is:

  1. start MySQL
  2. create the database
  3. confirm the configured username and password work
  4. run the Spring Boot app

For example:

bash
./mvnw spring-boot:run

Or after packaging:

bash
java -jar target/app.jar

Once the app is running, open http://localhost:8080/books.

If the page loads but no data appears, your web stack may be fine and the issue may be in the database or repository layer. If the app does not start at all, look first at datasource and entity scanning errors in the logs.

Typical Startup Failures

Most first-run issues fall into a small set of categories:

  • wrong JDBC URL or credentials
  • MySQL server not running
  • entity package not scanned
  • template path does not match the controller return name
  • SQL dialect or timezone-related connection issues

This is why a minimal app is so valuable. It narrows the debugging surface quickly.

A good sanity check is to confirm that:

  • the app starts
  • Hibernate creates or validates the table
  • '/books resolves to a template'
  • 'repository.findAll() executes successfully'

Once that works, you can safely add validation, service logic, and forms.

Common Pitfalls

The most common pitfall is mixing too many frameworks and features before the first successful local run. Start with one page, one entity, and one repository path.

Another mistake is relying on ddl-auto=update in environments where controlled schema migration is required. It is fine for simple development, not for disciplined production schema management.

A third issue is incorrect package structure. Spring Boot component scanning depends on where the application class sits relative to controllers, entities, and repositories.

Finally, teams often debug Thymeleaf when the real problem is database connectivity, or debug Hibernate when the real problem is view resolution. Keep the layers separate in your diagnosis.

Summary

  • A Spring MVC app with Spring Boot, Hibernate, and MySQL needs aligned web, persistence, and datasource configuration.
  • Start with minimal dependencies and a small entity-repository-controller-template flow.
  • Use simple local application.properties settings first, then harden them for production later.
  • Most startup failures come from JDBC, scanning, or template-path mismatches rather than from MVC itself.
  • Get one end-to-end page running locally before expanding the application architecture.

Course illustration
Course illustration

All Rights Reserved.