Fixing MyBatis‑Plus Migration Errors: Why LocalDateTime Conversion Fails and How to Resolve It
This article walks through replacing MyBatis with MyBatis‑Plus in a legacy Java project, explains the “Conversion not supported for type java.time.LocalDateTime” error caused by MyBatis 3.5.1 and an outdated mysql‑connector‑java driver, and shows how upgrading the driver resolves the issue while offering debugging tips.
MyBatis 替换成 MyBatis-Plus
背景介绍
一个老项目,数据库用 MySQL 5.7.36,ORM 框架使用 MyBatis 3.5.0,mysql-connector-java 版本为 5.1.26。
新来的同事觉得 MyBatis 使用不够简洁,代码较多,决定替换为 MyBatis-Plus。
Mybatis-Plus 替换 Mybatis
准备一张表 tbl_order 并初始化两条数据。
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');为了简化演示,直接用 MyBatis-Plus 搭建示例 demo,其他组件版本保持不变,MyBatis-Plus 版本使用 3.1.1。
运行 com.qsl.OrderTest#orderListAllTest 时出现异常:
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
at ... (stack trace omitted) ...
Caused by: java.sql.SQLException: Conversion not supported for type java.time.LocalDateTime
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
... (stack trace omitted) ...异常根因是 mysql-connector-java 5.1.26 不支持 java.time.LocalDateTime 类型。
将 mysql-connector-java 升级到 5.1.37 后再次执行测试,异常消失,查询结果正确。
Conversion not supported for type
MyBatis 3.5.0 能处理 LocalDateTime 转换,而 3.5.1 开始不再处理,交由 JDBC 驱动实现。
而 mysql-connector-java 5.1.26 仍不支持 LocalDateTime 、 LocalDate 、 LocalTime ,直到 5.1.37 才加入支持。
因此根因是 MyBatis 3.5.1 放弃对这些类型的内部转换,而使用的 JDBC 驱动版本未提供相应实现。
暴风雨的来临
在表 tbl_order 中插入一条记录:
INSERT INTO tbl_order VALUES (3, 'asdfgh', NULL, '2024-02-21 20:01:31.111', '2024-02-21 20:02:56.764');再次运行测试仍会出现异常,进一步排查发现是 getTimestamp 返回 NULL 导致。
升级 mysql-connector-java 到 5.1.42 后问题得到修复。
mybatis-plus-issues-1114
该 issue 与本文的 Conversion not supported for type java.time.LocalDateTime 问题相同,提示不同的连接池(如 Druid)可能导致不同异常。
修了“不该修的Bug”
在另一个业务场景中,文件生成校验逻辑的 Bug 被修复后,因旧数据兼容性问题再次导致生产异常。
背景介绍
主文件和附属文件的生成依赖关系校验原本只要任意一个主文件生成即通过,实际业务要求全部生成才算通过。
优化前的校验
校验逻辑错误导致不符合业务的记录被错误通过。
优化后的校验
修正校验逻辑,使其只有在所有主文件均生成时才通过。
生产异常
升级后,某附属文件生成失败,异常信息显示依赖资源 abc_{yyyyMMdd}.txt 未生成,实际是数据库中该字段被错误写入文件名而非文件 ID,导致旧 Bug 再次触发。
该案例说明组件升级或旧代码调整可能产生连锁影响,必须进行全面测试。
总结
对组件升级或旧代码改动需谨慎,避免牵一发动全身;若必须改动,务必进行充分的测试。
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.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
