Why MyBatis‑Plus Fails with LocalDateTime and How to Fix It

An old Java project using MySQL, MyBatis, and an outdated mysql‑connector‑java driver encounters a “Conversion not supported for type java.time.LocalDateTime” error after swapping MyBatis for MyBatis‑Plus; the article walks through reproducing the issue, analyzing MyBatis version changes, upgrading the JDBC driver, and fixing subsequent bugs.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Why MyBatis‑Plus Fails with LocalDateTime and How to Fix It

Background

In an existing project the database was MySQL 5.7.36, the ORM framework MyBatis 3.5.0, and the JDBC driver mysql-connector-java version 5.1.26.

A new developer joined the team and felt that MyBatis required too much boilerplate code, so he decided to replace it with MyBatis‑Plus.

Replacing MyBatis with MyBatis‑Plus

A table tbl_order was created and populated with two rows (SQL shown below).

DROP TABLE IF EXISTS `tbl_order`;
CREATE TABLE `tbl_order` (
  `id` bigint(0) 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');

A simple demo was built with MyBatis‑Plus 3.1.1 while keeping the JDBC driver at 5.1.26.

Running com.qsl.OrderTest#orderListAllTest produced the exception:

Conversion not supported for type java.time.LocalDateTime
Answer: mysql‑connector‑java 5.1.37 supports this type.

Upgrading mysql‑connector‑java

The driver was upgraded to 5.1.37 and the test was rerun. The exception disappeared and the query returned the expected results.

Root‑cause analysis

MyBatis 3.5.0 handled LocalDateTime conversion internally (converting java.sql.Timestamp to java.time.LocalDateTime). Starting with MyBatis 3.5.1 this handling was removed; conversion is now delegated to the JDBC driver.

The original driver version 5.1.26 does **not** support LocalDateTime, LocalDate, or LocalTime, which caused the failure.

Inspecting the driver’s supported types confirms the absence of these three Java 8 date‑time classes.

From version 5.1.37 onward, mysql‑connector‑java adds support for LocalDateTime , LocalDate and LocalTime .

Subsequent issue after upgrade

Two days after the upgrade, inserting a new record into tbl_order triggered another exception. The stack trace showed a NullPointerException caused by getTimestamp(columnIndex) returning null.

Upgrading the driver further to 5.1.42 resolved this problem.

MyBatis‑Plus specific discussion

An open issue (issue‑1114) reports a similar “Conversion not supported for type java.time.LocalDateTime” problem. The author notes that the issue may differ when using Druid as the connection pool instead of HikariCP.

The key takeaway is that the same analysis method—examining the exception stack and driver capabilities—applies regardless of the pool.

Conclusion

Component upgrades or legacy code adjustments can cause cascading failures. Avoid unnecessary changes; when changes are required, perform thorough testing to catch compatibility issues early.

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.

DebuggingJavaMyBatismybatis-plusLocalDateTimeVersion Compatibilitymysql-connector-java
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.