JPA vs MyBatis: Choosing the Right Persistence Framework and Design Practices

The article compares JPA and MyBatis in Chinese software projects, argues that JPA excels when solid OOP design and domain modeling are prioritized while MyBatis shines in data‑driven or fast‑delivery scenarios, and illustrates the discussion with Java code examples and design‑pattern reminders.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
JPA vs MyBatis: Choosing the Right Persistence Framework and Design Practices

I cannot definitively say whether JPA or MyBatis is more popular in China, but I personally prefer JPA even though I use MyBatis daily.

MyBatis works like a god when used well; otherwise it becomes a mess. JPA, when used well, transcends ordinary code because it frees you from transaction scripts, but a poorly used JPA is even worse than MyBatis.

Most domestic projects lack proper design; they are written ad‑hoc without even basic OOP, driven by market pressure rather than thoughtful architecture.

When the focus is on design, JPA’s OOP support (encapsulation, inheritance, polymorphism, abstraction, interfaces) is far superior to MyBatis.

Below is a typical Java entity using Lombok, JPA annotations, and event‑driven methods:

@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;

    private AccountRepository accountRepository;

    public Account(AccountRepository accountRepository) {
        this.accountRepository = accountRepository;
    }

    public void login(LoginCommand command) {}
    public void register(RegisterCommand command) {}

    @PostPersist
    public void emmitEvent() {}
}

Another example shows an abstract domain class with a protected final attribute:

public abstract class AbstractDomain {
    @Getter
    protected final String attr;

    public AbstractDomain(String attr) {
        this.attr = attr;
    }
}

The article warns that many developers have forgotten OOP fundamentals such as abstract classes, protected state, and the use of final. It also reminds readers of the importance of immutable collections like Collections.unmodifiableList().

Three development approaches are described: Frontend‑driven: UI is built first, then backend follows the UI design. Fast for small, UI‑centric projects but leads to fragmented architecture. Backend‑driven: Data modeling and domain design precede implementation. Suitable for large B2B systems where a solid domain model prevents code bloat. Data‑driven: Emphasizes data processing (e.g., reporting). MyBatis fits well here, while JPA may be less appropriate.

The author criticizes the current “pig‑headed” development style where services become massive, controllers and repositories contain little logic, and design is ignored, resulting in high maintenance costs.

In conclusion, the choice between JPA and MyBatis depends on the project’s size, data‑driven nature, and the team’s willingness to invest in proper domain modeling and OOP design.

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.

BackendDesign PatternsJavaPersistenceMyBatisOOPjpa
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.