Integrating Hibernate and MyBatis in a Spring Boot Project

This tutorial demonstrates how to integrate both Hibernate and MyBatis ORM frameworks within a single Spring Boot application, covering environment setup, project configuration, entity and repository definitions, service and controller implementation, and testing to verify concurrent operation.

Architect
Architect
Architect
Integrating Hibernate and MyBatis in a Spring Boot Project

1. Introduction

The article discusses the long‑standing debate between Hibernate and MyBatis and proposes a pragmatic solution: using both ORM frameworks together in one Spring Boot project to leverage the strengths of each.

2. Preparing the Development Environment

Required tools include JDK 1.8+, Maven 3+, Git, an IDE such as IntelliJ IDEA, and a MySQL 5.1.x database. The source code repository is linked at the end of the article.

3. Building the Project

3.1 Adding Dependencies

The project is created with Spring Initializr and includes spring-boot-starter-data-jpa (which brings in Hibernate) and mybatis-spring-boot-starter. The pom.xml snippet is shown as an image in the original article.

3.2 Defining the Entity – User.java

A simple User entity is created using Lombok annotations ( @Data, @NoArgsConstructor, @AllArgsConstructor) to reduce boilerplate code. The full source code is displayed as an image.

3.3 Defining Persistence Interfaces

For write operations, a Spring Data JPA repository extending JpaRepository is defined. For read operations, a MyBatis mapper interface with @Mapper and @Component annotations is created, along with the corresponding UserMapper.xml file (shown as an image).

3.4 Implementing the Service Layer

The UserService interface declares methods for saving a user, finding by ID, and retrieving all users. Its implementation injects both the JPA repository and the MyBatis mapper via constructor injection and uses @Transactional and @Service annotations.

3.5 Implementing the Controller

A REST controller provides three endpoints: POST /users (create), GET /users/{id} (read by ID), and GET /users (list all). The controller also uses constructor injection for the service.

4. Configuring Hibernate and MyBatis

The integration is achieved with a single data source defined in application.yml. Adding the MyBatis configuration section to the same file enables both frameworks to share the database connection without the complexity of multiple data sources.

5. Testing the Integration

Postman is used to test the three endpoints. A sample JSON payload for creating a user is:

{
    "username":"谭朝红",
    "alias":"ramostear",
    "age":28
}

The responses show that the user is persisted via Hibernate (ID = 3) and subsequently retrieved via MyBatis, confirming that both ORM tools operate correctly side‑by‑side.

6. Conclusion

The tutorial proves that integrating Hibernate and MyBatis in a single Spring Boot project is straightforward and avoids the overhead of multiple data sources. Using Hibernate for write‑heavy operations and MyBatis for flexible read queries combines the convenience of JPA with the fine‑grained control of MyBatis, improving development efficiency and system performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentSpring BootMyBatisORMHibernate
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.