Why Upgrading to MyBatis‑Plus Triggers LocalDateTime Errors and How to Resolve Them

This article walks through a real‑world scenario where replacing MyBatis with MyBatis‑Plus caused a LocalDateTime conversion exception due to changes in MyBatis 3.5.1 and an outdated mysql‑connector‑java driver, demonstrates how to pinpoint the root cause via stack traces, and provides step‑by‑step fixes including driver upgrades and version considerations, while also sharing lessons learned from related validation bugs.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Why Upgrading to MyBatis‑Plus Triggers LocalDateTime Errors and How to Resolve Them

MyBatis‑Plus Replaces MyBatis

In an existing project using MySQL 5.7.36, MyBatis 3.5.0, and mysql‑connector‑java 5.1.26, a new developer found MyBatis cumbersome and decided to switch to MyBatis‑Plus 3.1.1, keeping other component versions unchanged.

Demo Setup

A simple table tbl_order is created and populated with two rows:

DROP TABLE IF EXISTS tbl_order;
CREATE TABLE tbl_order (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  order_no VARCHAR(50) NOT NULL COMMENT '订单号',
  pay_time DATETIME(3) DEFAULT NULL COMMENT '付款时间',
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '最终修改时间',
  PRIMARY KEY (id) USING BTREE
) ENGINE=InnoDB COMMENT='订单';
INSERT INTO tbl_order VALUES (1,'123456','2024-02-21 18:38:32.000','2024-02-21 18:37:34.000','2024-02-21 18:40:01.720');
INSERT INTO tbl_order VALUES (2,'654321','2024-02-21 19:33:32.000','2024-02-21 19:32:12.020','2024-02-21 19:34:03.727');

The demo runs com.qsl.OrderTest#orderListAllTest, which initially throws an exception.

Exception Details

org.springframework.dao.TransientDataAccessResourceException: Error attempting to get column 'pay_time' from result set.  Cause: java.sql.SQLException: Conversion not supported for type java.time.LocalDateTime
... (stack trace truncated) ...
java.sql.SQLException: Conversion not supported for type java.time.LocalDateTime
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    ...

The error message points to an unsupported conversion for java.time.LocalDateTime.

Root‑Cause Analysis

From MyBatis 3.5.1 onward, the framework no longer handles conversion of LocalDateTime, LocalDate, and LocalTime; it delegates the conversion to the JDBC driver. The previously used mysql‑connector‑java 5.1.26 does not support these Java 8 time types, leading to the exception.

MySQL connector version 5.1.37 introduced support for LocalDateTime, LocalDate, and LocalTime. Upgrading the driver resolves the conversion issue.

Connector version support
Connector version support

Fix Implementation

Upgrade mysql‑connector‑java to 5.1.37 (or newer). After the upgrade, re‑run com.qsl.OrderTest#orderListAllTest; the exception disappears and query results are correct.

Supported types in connector
Supported types in connector

Further Investigation

Additional screenshots show the MyBatis version change from 3.5.0 (which handled LocalDateTime) to 3.5.1 (which does not). The article also references MyBatis‑Plus issue #1114, noting that different connection pools (HikariCP vs. Druid) may surface related but distinct problems.

Validation Logic Bug Story

The author shares a separate incident where a file‑generation validation routine was incorrectly optimized, turning a “all‑files‑must‑exist” rule into an “any‑file‑exists” rule, which silently allowed corrupted data. After fixing the validation, a production failure surfaced because the underlying data still contained an invalid filename placeholder (e.g., abc_{yyyyMMdd}.txt) that the buggy logic had previously tolerated.

The lesson emphasized is that seemingly harmless refactors can have far‑reaching side effects, especially when component versions or data schemas change.

Conclusion

Component upgrades—whether a new ORM library or a JDBC driver—can introduce subtle incompatibilities. Understanding version‑specific behavior (such as MyBatis 3.5.1’s removal of time‑type handling) and ensuring the supporting libraries (like mysql‑connector‑java) are compatible is essential. Comprehensive testing before production deployment helps avoid “fixing the wrong bug” scenarios.

JavaORMMyBatis-PlusLocalDateTimeVersion Compatibility
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.