Cause: java.sql.SQLFeatureNotSupportedException: getObject with type

记录一次datetime转LocalDateTime报错。

org.springframework.dao.InvalidDataAccessApiUsageException: Error attempting to get column 'birthday' from result set.  Cause: java.sql.SQLFeatureNotSupportedException: getObject with type
; getObject with type; nested exception is java.sql.SQLFeatureNotSupportedException: getObject with type

	at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:96)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:88)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
	at com.sun.proxy.$Proxy88.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMany(MybatisMapperMethod.java:177)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:78)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
	at com.sun.proxy.$Proxy92.selectList(Unknown Source)
	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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
	at com.sun.proxy.$Proxy93.selectList(Unknown Source)
	at com.hello.ShardingsphereTest.testQuery(ShardingsphereTest.java:55)
	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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.sql.SQLFeatureNotSupportedException: getObject with type
	at org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationResultSet.getObject(AbstractUnsupportedOperationResultSet.java:223)
	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)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyAutomaticMappings(DefaultResultSetHandler.java:521)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:402)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:354)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:328)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:301)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:194)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
	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.plugin.Plugin.invoke(Plugin.java:63)
	at com.sun.proxy.$Proxy132.query(Unknown Source)
	at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doQuery(MybatisSimpleExecutor.java:67)
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
	at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.query(MybatisCachingExecutor.java:163)
	at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.query(MybatisCachingExecutor.java:90)
	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.plugin.Plugin.invoke(Plugin.java:63)
	at com.sun.proxy.$Proxy131.query(Unknown Source)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
	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.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
	... 48 more

 环境

SpringBoot 2.1.1.RELEASE

ShardingJDBC 4.0.0-RC1

Mybatis Plus 3.3.1.tmp

解决方法

感谢这位大佬 !

使Jackson和Mybatis支持JSR310标准 - Deolin - 博客园

引入下面这个依赖就可以了!

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-typehandlers-jsr310</artifactId>
   <version>1.0.1</version>
</dependency>

这里并没有完全解决,评论区两位兄弟做了回答,这里贴一下。

 

为什么

1、看异常日志,从上往下找到具体报错信息

 2、T getObject(final String columnLabel, final Class<T> type) 调用直接抛出异常,字面意思是说它不支持这个类型吧。这个类是ShardingJdbc的,再根据异常往下看,是在哪里调用的ShardingJdbc的这个方法。

 3、定位到applyAutomaticMappings方法,通过

createAutomaticMappings(rsw, resultMap, metaObject, columnPrefix);

会方法,为每个字段匹配到对应的类型转换器TypeHandler。

4、字段birthday的转换器是mybatis的LocalDateTimeHandler,然后是调用handler的getResult方法

rs.getObject就是第2步里的方法了,这就找到报错的原因了。

 5、那么添加了mybatis-typehandlers-jsr310依赖就能解决问题呢?

因为它重写并覆盖了LocalDateTimeTypeHandler.java,这样它就不会去调用ShardingJdbc的rs.getObject的方法了,直接就转换并返回结果了。

总结

总而言之就是自定义了类型转换器.....

<think>好的,我现在需要帮助用户解决Java中出现的SQLFeatureNotSupportedException异常问题。首先,我要理解这个异常的原因。根据用户提供的引用[1],当操作时出现这个异常,引用[4]提到可能是数据库驱动不支持某个方法。因此,我需要总结可能的原因和解决方案。 首先,可能的原因之一是JDBC驱动版本过旧,不支持某些新方法。比如,用户可能在代码中调用了ResultSet的getObject()方法并尝试转换为LocalDateTime,而旧版本的驱动可能不支持这种类型转换,导致异常。这时候需要检查驱动版本,并升级到与数据库兼容的版本。 另一个可能的原因是使用了不支持特定方法的JDBC方法。例如,某些高级功能如保存点、特定游标类型可能在当前驱动中不可用。用户需要查阅驱动文档,确认是否支持这些方法,或者寻找替代方法。 此外,框架配置问题也可能导致异常。比如在MyBatis中处理datetime类型时,如果类型处理器配置不当,可能引发问题。引用[3]提到LocalDateTimeTypeHandler的相关日志,说明可能需要检查MyBatis的配置或类型处理是否正确。 接下来,我需要整理解决方案的步骤。首先建议用户检查驱动版本,并升级。然后,查看代码中是否使用了驱动不支持的方法,替换为兼容的写法。同时,检查框架配置,确保类型处理正确。最后,查阅官方文档和社区资源,确认兼容性和最佳实践。 用户的问题可能涉及到具体代码,比如在Spring和Hibernate中的配置,如引用[2]提到的构造函数方式处理查询结果,可能需要调整查询方式以避免使用不支持的方法。此外,引用[4]建议更换驱动或避免使用特定方法,这也是一个解决方案。 最后,生成相关问题时要覆盖异常原因、驱动升级、替代方法、框架配置和常见场景,帮助用户全面理解问题。</think>### Java SQLFeatureNotSupportedException 异常原因及解决方案 #### 异常原因 1. **JDBC驱动版本不兼容** 当使用旧版本的数据库驱动(如MySQL Connector/J)调用新版本JDBC API时,可能因驱动未实现某些接口方法而抛出此异常。例如,尝试使用`ResultSet.getObject(column, LocalDateTime.class)`时,旧驱动可能不支持该类型映射[^3][^4]。 2. **调用了驱动不支持的方法** 部分高级JDBC操作(如特定游标类型、保存点操作或特定数据类型转换)可能未被当前驱动实现。例如: ```java // 旧驱动可能不支持此方法 Statement.setLargeMaxRows(long max); ``` 3. **框架配置问题** 在MyBatis、Hibernate等框架中,若类型处理器(如`LocalDateTimeTypeHandler`)与数据库字段类型不匹配,或驱动不支持该类型映射,可能触发异常。 --- #### 解决方案步骤 1. **升级JDBC驱动** - 检查当前驱动版本(如MySQL Connector/J),升级至与数据库版本匹配的最新驱动。 - 示例:MySQL 8.x推荐使用`mysql-connector-java:8.0.x`,替换旧版5.x驱动。 2. **检查并替换不支持的JDBC方法** - 若使用`getObject(column, Class)`方法,可改用传统类型转换: ```java // 替换前(可能抛出异常) LocalDateTime date = resultSet.getObject("create_time", LocalDateTime.class); // 替换后(兼容旧驱动) Timestamp timestamp = resultSet.getTimestamp("create_time"); LocalDateTime date = timestamp.toLocalDateTime(); ``` 3. **验证框架类型处理器配置** - 在MyBatis中,确认`datetime`字段映射到正确的类型处理器: ```xml <result column="create_time" property="createTime" jdbcType="TIMESTAMP" typeHandler="org.apache.ibatis.type.LocalDateTimeTypeHandler"/> ``` - 若仍报错,回退到`Date`类型或检查驱动兼容性。 4. **查阅驱动文档确认支持的方法** - 参考驱动官方文档(如[MySQL Connector/J API](https://dev.mysql.com/doc/connector-j/en/)),确认方法是否被实现。 --- #### 代码示例(兼容性写法) ```java try (Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM orders"); ResultSet rs = stmt.executeQuery()) { while (rs.next()) { // 避免直接使用LocalDateTime.class Timestamp ts = rs.getTimestamp("order_date"); LocalDateTime orderDate = ts != null ? ts.toLocalDateTime() : null; } } catch (SQLException e) { e.printStackTrace(); } ``` ---
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值