先说下解决方法: 1. 降级Mybatis至3.5.0及以下; 2. 或升级mssql-jdbc驱动至7.1.0及以上
原文地址:https://blog.csdn.net/qq_24505485/article/details/103540439
解决过程
之前实体类使用的是Timestamp 来存储时间,没有错,后来使用了LocalDatetime来存储也没错,再之后我把Mybatis从3.5.0升级到了3.5.1之后,(mssql-jdbc的版本是com.microsoft.sqlserver:mssql-jdbc:6.4.0.jre8)LocalDatetime的转换就会抛异常:
com.microsoft.sqlserver.jdbc.SQLServerException: The conversion to class java.time.LocalDateTime is unsupported.at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getObject(SQLServerResultSet.java:2249)at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getObject(SQLServerResultSet.java:2267)at com.zaxxer.hikari.pool.HikariProxyResultSet.getObject(HikariProxyResultSet.java)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(ResultSetLogger.java:69)at com.sun.proxy.$Proxy300.getObject(Unknown Source)at org.apache.ibatis.type.LocalDateTimeTypeHandler.getNullableResult(LocalDateTimeTypeHandler.java:38)at org.apache.ibatis.type.LocalDateTimeTypeHandler.getNullableResult(LocalDateTimeTypeHandler.java:28)at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:81)
原因在于Mybatis3.5.1出现了一个不向后兼容的更新mybatis-3.5.1更新日志
There is a backward incompatible change.Because of the fix for #1478 , LocalDateTypeHandler, LocalTimeTypeHandler and LocalDateTimeTypeHandler now require a JDBC driver that supports JDBC 4.2 API.Also, these type handlers no longer work with Druid. See #1516
大体意思就是Local*TypeHandler需要一个支持JDBC 4.2的JDBC驱动
Mybatis3.5.0 LocalDateTimeTypeHandler.class部分源码:
@Overridepublic LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {Timestamp timestamp = rs.getTimestamp(columnIndex);return getLocalDateTime(timestamp);}@Overridepublic LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {Timestamp timestamp = cs.getTimestamp(columnIndex);return getLocalDateTime(timestamp);}private static LocalDateTime getLocalDateTime(Timestamp timestamp) {if (timestamp != null) {return timestamp.toLocalDateTime();}return null;}
通过源码可知,Mybatis3.5.0及以前应该是将LocalDatetime转换为Timestamp然后再调用驱动来获取数据;
Mybatis3.5.1 LocalDateTimeTypeHandler.class部分源码:
@Overridepublic LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {return rs.getObject(columnName, LocalDateTime.class);}@Overridepublic LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {return rs.getObject(columnIndex, LocalDateTime.class);}@Overridepublic LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {return cs.getObject(columnIndex, LocalDateTime.class);}
这里是直接用驱动的getObject方法来获取对应数据;
找到mssql驱动的实现方法,源码如下:
public <T> T getObject(int index,Class<T> type) throws SQLException {loggerExternal.entering(getClassNameLogging(), "getObject", index);checkClosed();Object returnValue;if (type == String.class) {returnValue = getString(index);}else if (type == Byte.class) {byte byteValue = getByte(index);returnValue = wasNull() ? null : byteValue;}else if (type == Short.class) {short shortValue = getShort(index);returnValue = wasNull() ? null : shortValue;}else if (type == Integer.class) {int intValue = getInt(index);returnValue = wasNull() ? null : intValue;}else if (type == Long.class) {long longValue = getLong(index);returnValue = wasNull() ? null : longValue;}else if (type == BigDecimal.class) {returnValue = getBigDecimal(index);}else if (type == Boolean.class) {boolean booleanValue = getBoolean(index);returnValue = wasNull() ? null : booleanValue;}else if (type == java.sql.Date.class) {returnValue = getDate(index);}else if (type == java.sql.Time.class) {returnValue = getTime(index);}else if (type == java.sql.Timestamp.class) {returnValue = getTimestamp(index);}else if (type == microsoft.sql.DateTimeOffset.class) {returnValue = getDateTimeOffset(index);}else if (type == UUID.class) {// read binary, avoid string allocation and parsingbyte[] guid = getBytes(index);returnValue = guid != null ? Util.readGUIDtoUUID(guid) : null;}else if (type == SQLXML.class) {returnValue = getSQLXML(index);}else if (type == Blob.class) {returnValue = getBlob(index);}else if (type == Clob.class) {returnValue = getClob(index);}else if (type == NClob.class) {returnValue = getNClob(index);}else if (type == byte[].class) {returnValue = getBytes(index);}else if (type == Float.class) {float floatValue = getFloat(index);returnValue = wasNull() ? null : floatValue;}else if (type == Double.class) {double doubleValue = getDouble(index);returnValue = wasNull() ? null : doubleValue;}else {// if the type is not supported the specification says the should// a SQLException instead of SQLFeatureNotSupportedExceptionMessageFormat form = new MessageFormat(SQLServerException.getErrString("R_unsupportedConversionTo"));Object[] msgArgs = {type};throw new SQLServerException(form.format(msgArgs), SQLState.DATA_EXCEPTION_NOT_SPECIFIC, DriverError.NOT_SET, null);}loggerExternal.exiting(getClassNameLogging(), "getObject", index);return type.cast(returnValue);}
发现mssql-jdbc-6.4.0.jre8.jar并不支持LocalDatetime的转换,所以会抛出The conversion to class java.time.LocalDateTime is unsupported.
但是我去mssql-jdbc官方网站看到的是mssql-jdbc:6.4.0.jre8是全面支持JDBC 4.2的
Microsoft JDBC Driver 6.4 for SQL Server 完全符合 JDBC 规范 4.1 和 4.2。 根据 Java 版本兼容性命名 6.4 包中的 jar。 例如,6.4 包中的 mssql-jdbc-6.4.0.jre8.jar 文件必须与 Java 8 配合使用。
JDBC 4.2 新增了对 JSR 310(Date and Time API,JSR 310为Java提供了一个新的、改进的日期和时间API)的支持;也就是说JDBC 4.2 可以直接将Java8的LocalDatetime直接和数据库数据对应上;
所以说应该是他们的bug了。
由于mssql-jdbc是在开源在github上的,所以可以到相关repo找找有没有人提过该bug,以及在哪个版本被修复了。
直接检索jdbc 4.2 发现第二条issue就是和LocalDatetime相关的#749;
修复后发布的版本号:[7.1.0] Preview Release
解决方法
该bug在7.1.0版本中被修复,所以至此有2种方式能解决问题
1.降级Mybatis至3.5.0及以下
2.或升级mssql-jdbc驱动至7.1.0及以上
mssql-jdbc的驱动可以在mvnrepository查找 https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
解决问题的思路
由于最初是因为升级Mybatis版本而引起的,所以在看了异常信息之后就去找Mybatis的更新日志了,通过更新日志才一步步发现这个原来是mssql-jdbc驱动的bug;
还有本来第一反应是百度搜索相关报错,结果没找到,所以这里详细记录一下这次bug的解决过程;
