JPA vs MyBatis: Choosing the Right Persistence Strategy for Scalable Backend Design

This article compares JPA and MyBatis, discusses when each framework shines, explores design‑driven development approaches, and highlights common pitfalls such as oversized services, neglected OOP principles, and the importance of proper data modeling for large‑scale Java backend projects.

Java Backend Technology
Java Backend Technology
Java Backend Technology
JPA vs MyBatis: Choosing the Right Persistence Strategy for Scalable Backend Design

The author reflects on the popularity of JPA and MyBatis in China, admitting a personal preference for MyBatis in daily work but acknowledging that both frameworks can be either excellent or disastrous depending on how they are used.

When a project is well‑designed, JPA excels at abstracting transaction scripts and leveraging OOP concepts such as encapsulation, inheritance, and polymorphism. Conversely, MyBatis shines in fast‑moving, data‑driven or front‑end‑driven projects where quick iteration is needed.

Three development styles are described:

Front‑end driven : UI is built first, then the backend follows the UI design. Suitable for small, client‑facing projects; MyBatis fits well.

Back‑end driven : Data modeling and domain design precede implementation. Essential for large B2B systems; JPA is more appropriate.

Data driven : Emphasis on reporting and large‑scale data processing where the data shape dictates the architecture; MyBatis is often a better fit.

Common problems in many codebases include overly heavy Service layers, under‑utilized Controller and Repository layers, and a lack of proper design patterns, leading to what the author calls “pig‑headed development”.

Code examples illustrate typical entity definitions with Lombok annotations and an abstract domain class, emphasizing the use of @Getter, @NoArgsConstructor, and immutable fields.

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "uaa_account")
@Entity
public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;
    // ... other fields and methods ...
}
public abstract class AbstractDomain {
    @Getter
    protected final String attr;
    public AbstractDomain(String attr) {
        this.attr = attr;
    }
}

The author stresses that solid OOP fundamentals (SOLID principles, design patterns) are often forgotten, and that immutable collections like Collections.unmodifiableList() are underused.

Joshua J. Bloch, the author of Effective Java , exemplifies the level of expertise the community should aspire to.

In summary, the choice between JPA and MyBatis depends less on personal bias and more on the overall development methodology, project size, and the rigor of the design phase.

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.

JavaMyBatisOOPDesignjpa
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.