SoFunction
Updated on 2025-05-23

Comparison of advantages and disadvantages of MyBatis and Spring Data JPA in Spring Boot

1. Core concept

MyBatis

definition: SQL-based persistence layer framework that provides flexible SQL mapping and custom query capabilities.

Features

  • Developers write SQL (XML or annotations) manually.
  • Supports dynamic SQL and complex query optimization.
  • Lightweight, strong control over databases.

Spring Data JPA

definition: ORM framework based on JPA (Java Persistence API) specification, providing Repository abstraction.

Features

  • Automatically generate CRUD methods through the interface (such assave()findAll())。
  • Support method name derived query (such asfindByUsername(String name))。
  • Rely on Hibernate implementation, suitable for rapid development.

2. Comparison of advantages and disadvantages

characteristic MyBatis Spring Data JPA
SQL Control Completely written manually, with high flexibility Automatic generation, low flexibility
Development efficiency Need to write SQL and mapping files, which are less efficient Automatically generate code, high development efficiency
Learning Cost Need to be familiar with SQL and XML configurations Understand JPA specifications and derivative query syntax
Complex query support Strong (support dynamic SQL) Weak (need to combine@Queryor QueryDSL extension)
Database compatibility Relying on SQL dialect Adapt to multiple databases via Hibernate

3. Comparison of framework structure

1. Typical hierarchical structure of MyBatis

src/
├── main/
│   ├── java/
│   │   ├── entity/           # Entity class (map with database table)│   │   ├── mapper/           # Mapper interface (defines SQL operations)│   │   ├── service/          # Business logic layer│   │   └── controller/       # Control layer (processing HTTP requests)│   └── resources/
│       └── mapper/           # SQL Mapping files(XML)

2. Typical hierarchical structure of Spring Data JPA

src/
├── main/
│   ├── java/
│   │   ├── entity/           # Entity class (with JPA annotation)│   │   ├── repository/       # Repository interface (inherited by JpaRepository)│   │   ├── service/          # Business logic layer│   │   └── controller/       # Control layer│   └── resources/
│       └──    # Configuration JPA Connect with the database

4. Comparison of code examples

1. MyBatis implements query

// Mapper interfacepublic interface UserMapper {
    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Long id);
}
// Service layer@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public User getUser(Long id) {
        return (id);
    }
}

2. Spring Data JPA implements query

// Repository interfacepublic interface UserRepository extends JpaRepository<User, Long> {
    User findByUsername(String username); // Automatically generate query}
// Service layer@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    public User getUser(String username) {
        return (username);
    }
}

5. Choose suggestions

Choose MyBatis

  • Complex SQL or deep optimization query performance is required.
  • Legacy database table structure is complex and difficult to map through ORM.

Choose Spring Data JPA

  • Rapidly develop CRUD functions and reduce boilerplate code.
  • The project adopts domain-driven design (DDD).

6. Comparison of architecture diagrams (text description)

MyBatis Architecture:
[Controller] → [Service] → [Mapper Interface] ↔ [XML SQL] → [Database]
Spring Data JPA Architecture:
[Controller] → [Service] → [Repository Interface] → [JPA/Hibernate] → [Database]

Through comparison, it can be seen that MyBatis' SQL control is more underlying, while JPA hides SQL details through abstraction layers.

This is the article about the comparison and introduction of MyBatis and Spring Data JPA in Spring Boot. For more information about Spring Boot MyBatis and Spring Data JPA, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!