MyBatis vs Spring Data JPA: Which Should Power Your Spring Boot Project?
This article compares MyBatis and Spring Data JPA, explaining their core concepts, performance, flexibility, learning curve, community support, and real‑world scenarios, and provides code examples and decision criteria to help developers choose the right data‑access framework for their Spring Boot applications.
MyBatis vs Spring Data JPA
Many developers wonder whether to use MyBatis or Spring Data JPA in a Spring Boot project, and the choice depends on project requirements and team habits.
1. MyBatis and Spring Data JPA
MyBatis is a semi‑automatic ORM that requires manual SQL but offers flexible mapping. Spring Data JPA implements the JPA specification (usually with Hibernate) and provides a fully automatic ORM where most SQL is generated for you.
2. Why the choice?
Project size – Small or prototype projects benefit from JPA’s automation; large or complex projects may need MyBatis’s flexibility.
Team skillset – Teams strong in SQL can adopt MyBatis quickly; teams comfortable with object‑oriented programming may prefer JPA.
Performance requirements – MyBatis allows direct SQL tuning for high‑concurrency scenarios; JPA’s caching can be sufficient for ordinary workloads.
Long‑term maintenance – MyBatis stores SQL in XML, making it easy to trace; JPA reduces boilerplate but can be harder to debug.
3. MyBatis vs Spring Data JPA examples
Assume a simple user table with id, name and email columns.
MyBatis example
Add the starter dependency:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>Define the entity:
public class User {
private Long id;
private String name;
private String email;
// getters and setters omitted
}Create a mapper interface:
@Mapper
public interface UserMapper {
User findById(Long id);
}Write the SQL in src/main/resources/mapper/UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<select id="findById" parameterType="Long" resultType="com.example.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>Use it in a service:
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(Long id) {
return userMapper.findById(id);
}
}Advantage: SQL is visible and easy to optimise. Disadvantage: XML configuration can become bulky in large projects.
Spring Data JPA example
Add the JPA starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>Define the entity with annotations:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// getters and setters omitted
}Create a repository that extends JpaRepository:
public interface UserRepository extends JpaRepository<User, Long> {
// custom query example
@Query("SELECT u FROM User u WHERE u.name = ?1")
List<User> findByName(String name);
}Use it in a service:
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
}Advantage: Less boilerplate and fast development for CRUD. Disadvantage: Generated SQL is hidden and may be inefficient for complex queries.
4. Performance, flexibility and applicable scenarios
1. Performance comparison
MyBatis – Direct SQL lets you add indexes, hints, or use database‑specific functions; ideal for high‑concurrency or complex queries.
Spring Data JPA – Caching (first‑ and second‑level) can reduce DB hits for simple CRUD, but auto‑generated SQL may be sub‑optimal for joins.
2. Flexibility
MyBatis supports dynamic SQL with <if> tags, making it easy to build conditional queries.
<select id="findUsers" parameterType="map" resultType="User">
SELECT * FROM user WHERE 1=1
<if test="name != null"> AND name = #{name}</if>
<if test="email != null"> AND email = #{email}</if>
</select>JPA requires Specification, QueryDSL, or custom @Query annotations, which adds complexity.
3. Learning curve and development efficiency
JPA is beginner‑friendly because Spring Boot auto‑configures most settings; you only need entities and repositories. MyBatis demands learning XML mapping and writing SQL, which takes more time initially.
4. Community and ecosystem
Both projects have strong communities: MyBatis originates from Apache and is widely used in China, while JPA is part of the Java EE standard with a mature Spring Data ecosystem.
5. Real‑world application scenarios
Case 1 – Rapid MVP : A small product launched in two weeks used JPA for speed, adding custom @Query only when needed.
Case 2 – Financial system : Complex reporting and heavy SQL optimisation led to choosing MyBatis, with XML files serving as documentation for DBAs.
Case 3 – Microservice architecture : Some services use JPA for simple CRUD, others use MyBatis for intricate queries; a unified guideline is required to avoid maintenance overhead.
Conclusion
If the project mainly involves simple CRUD, the team is comfortable with object‑oriented programming, and rapid development is a priority, Spring Data JPA is the better choice.
If the project requires complex queries, high performance, or the team has strong SQL expertise, MyBatis offers the necessary flexibility and control.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
