Midnight Crash: The ‘Impossible’ MyBatis‑Plus Pitfalls That Can Break Production
The article catalogs six production‑ready pitfalls of MyBatis‑Plus—duplicate Snowflake IDs, batch‑insert disorder, enum storage errors, camel‑case mismatches, auto‑fill failures, and JSON field loss—explains their root causes with concrete examples and code, and provides practical solutions to keep the framework reliable in high‑concurrency environments.
Background and ORM Choice
Rapid data growth and micro‑service fragmentation have pushed Java teams to seek an ORM that balances speed and stability. MyBatis‑Plus emerged as a production‑oriented enhancement over raw MyBatis, offering CRUD shortcuts, logical deletion, auto‑fill, optimistic locking, multi‑tenant support, and code generation.
Production Challenges
In development environments the framework appears flawless, but once deployed the hidden defaults surface, leading to critical failures such as primary‑key conflicts and data inconsistency.
1. Snowflake ID Duplication
Problem: Two container instances generated identical Snowflake IDs, causing primary‑key collisions at 3 AM.
Root Causes:
Machine ID not configured, all instances use the default.
Docker containers clone MAC addresses; the framework extracts the last two bytes and mods by 32, causing collisions when instances exceed 32.
Kubernetes pods receive virtual MACs from CNI plugins, often identical or unavailable, leading to the same workerId.
Clock rollback duplicates timestamps, dataCenterId, workerId, and sequence.
Solution: Provide a unique distributed ID service or switch to database‑generated IDs.
@Slf4j
@Component
public class IdHelper {
public static long getIdc() {
try {
return ZZIdcClient.getInstance().get();
} catch (Throwable e) {
log.error("act=getIdc error={}", e.getMessage());
log.info("k=s act=getIdcByIDHelper");
return IDHelper.generator(0);
}
}
}Alternative enum for IdType shows built‑in options:
public enum IdType {
AUTO(0), NONE(1), INPUT(2), ASSIGN_ID(3), ASSIGN_UUID(4),
@Deprecated ID_WORKER(3), @Deprecated ID_WORKER_STR(3), UUID(4);
private final int key;
}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.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
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.
