Resolving MyBatis‑Plus LocalDateTime Conversion Errors by Upgrading MySQL Connector
This article explains why replacing MyBatis with MyBatis‑Plus caused a LocalDateTime conversion exception, identifies the root cause as an outdated mysql‑connector‑java version, and shows how upgrading the connector (to 5.1.37 or later) resolves the issue while providing code examples and debugging steps.
Background: An old project uses MySQL 5.7.36, MyBatis 3.5.0, and mysql‑connector‑java 5.1.26. A new developer finds MyBatis cumbersome and decides to replace it with MyBatis‑Plus (version 3.1.1) while keeping other component versions unchanged.
MyBatis‑Plus Replaces MyBatis
A table tbl_order is created and initialized with two rows. The following DDL and DML statements are used:
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');A demo project is built with MyBatis‑Plus to simulate the replacement process. When running com.qsl.OrderTest#orderListAllTest , a TransientDataAccessResourceException occurs, indicating that conversion for type java.time.LocalDateTime is not supported.
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 omitted) ...
Caused by: java.sql.SQLException: Conversion not supported for type java.time.LocalDateTime
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
...The stack trace shows that the failure originates from the JDBC driver, not MyBatis‑Plus. The mysql‑connector‑java version 5.1.26 does not support LocalDateTime . Upgrading the driver to 5.1.37 adds support for LocalDateTime , LocalDate , and LocalTime , eliminating the exception.
Upgrading mysql‑connector‑java
After upgrading to 5.1.37 and re‑executing the test, the query succeeds and the results are correct. This confirms that the conversion issue was caused by the outdated driver.
Why the Exception Appears After the Upgrade
MyBatis 3.5.0 handled LocalDateTime conversion internally, but starting with MyBatis 3.5.1 the conversion is delegated to the JDBC driver. Since the older driver lacks support, the exception surfaces only after switching to MyBatis‑Plus (which depends on MyBatis 3.5.1).
Examining the MySQL driver source reveals that ResultSetImpl.getObject throws a Conversion not supported for type … error for unsupported Java 8 time types.
public
T getObject(int columnIndex, Class
type) throws SQLException {
if (type == null) {
throw SQLError.createSQLException("Type parameter can not be null", ...);
}
// ... handling for common types ...
else {
if (this.connection.getAutoDeserialize()) {
try {
return (T) getObject(columnIndex);
} catch (ClassCastException cce) {
SQLException sqlEx = SQLError.createSQLException("Conversion not supported for type " + type.getName(), ...);
sqlEx.initCause(cce);
throw sqlEx;
}
}
throw SQLError.createSQLException("Conversion not supported for type " + type.getName(), ...);
}
}Thus, upgrading the driver resolves the problem.
Additional Observations
The article also mentions a related GitHub issue (mybatis‑plus‑issues‑1114) and notes that different connection pools (e.g., Druid) may exhibit different behavior.
Lesson Learned
Component upgrades or changes to legacy code can have far‑reaching effects. If a change is unavoidable, thorough testing is essential to avoid production incidents.
Overall, the root cause of the Conversion not supported for type java.time.LocalDateTime exception is the combination of MyBatis 3.5.1 delegating time‑type conversion to the JDBC driver and the driver version being too old to support those types.
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.
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.