暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Mybatis-Plus之使用LocalDateTime等java8新日期时间类型报错

原创 GBASE数据库 2024-03-22
1797

数据库JDBC版本:GBase 8s JDBC V8.8 3.3.0_3
Mybatis-Plus版本: 3.4.3.2
JDK版本: jdk1.8.0_201

对象类型使用LocalDataTime时,报错误:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='et.updatetime', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: System or internal error java.lang.NoSuchMethodException: com.gbasedbt.jdbc.IfxDateTime.fromnull(java.time.LocalDateTime)

 

    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)

    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)

    at com.sun.proxy.$Proxy56.update(Unknown Source)

    at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:288)

    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:64)

    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)

    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)

    at com.sun.proxy.$Proxy62.updateById(Unknown Source)

    at com.gbasedbt.mpdemo.SampleTest.testUpdate(SampleTest.java:35)

    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.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)

    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)

    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)

    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)

    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)

    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)

    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)

    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)

    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)

    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)

    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)

    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)

    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)

    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)

    at java.util.ArrayList.forEach(ArrayList.java:1257)

    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)

    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)

    at java.util.ArrayList.forEach(ArrayList.java:1257)

    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)

    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)

    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)

    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)

    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)

    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)

    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)

    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)

    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)

    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)

    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)

    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)

    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)

    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='et.updatetime', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: System or internal error java.lang.NoSuchMethodException: com.gbasedbt.jdbc.IfxDateTime.fromnull(java.time.LocalDateTime)

    at com.baomidou.mybatisplus.core.MybatisParameterHandler.setParameters(MybatisParameterHandler.java:215)

    at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:94)

    at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:64)

    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:64)

    at com.sun.proxy.$Proxy86.parameterize(Unknown Source)

    at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:88)

    at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:49)

    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)

    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)

    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.Invocation.proceed(Invocation.java:49)

    at com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor.intercept(MybatisPlusInterceptor.java:106)

    at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:62)

    at com.sun.proxy.$Proxy85.update(Unknown Source)

    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:194)

    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:427)

    ... 74 more

Caused by: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: System or internal error java.lang.NoSuchMethodException: com.gbasedbt.jdbc.IfxDateTime.fromnull(java.time.LocalDateTime)

    at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:75)

    at com.baomidou.mybatisplus.core.MybatisParameterHandler.setParameters(MybatisParameterHandler.java:213)

    ... 100 more

Caused by: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: System or internal error java.lang.NoSuchMethodException: com.gbasedbt.jdbc.IfxDateTime.fromnull(java.time.LocalDateTime)

    at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:75)

    at org.apache.ibatis.type.UnknownTypeHandler.setNonNullParameter(UnknownTypeHandler.java:67)

    at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:73)

    ... 101 more

Caused by: java.sql.SQLException: System or internal error java.lang.NoSuchMethodException: com.gbasedbt.jdbc.IfxDateTime.fromnull(java.time.LocalDateTime)

    at com.gbasedbt.util.IfxErrMsg.getSQLException(IfxErrMsg.java:472)

    at com.gbasedbt.jdbc.IfxObject.findMethod(IfxObject.java:163)

    at com.gbasedbt.jdbc.IfxObject.fromObject(IfxObject.java:186)

    at com.gbasedbt.jdbc.IfxValue.makeInstance(IfxValue.java:504)

    at com.gbasedbt.jdbc.IfxValue.makeInstance(IfxValue.java:411)

    at com.gbasedbt.jdbc.IfxPreparedStatement.setObject(IfxPreparedStatement.java:2374)

    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setObject(HikariProxyPreparedStatement.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.PreparedStatementLogger.invoke(PreparedStatementLogger.java:67)

    at com.sun.proxy.$Proxy88.setObject(Unknown Source)

    at org.apache.ibatis.type.LocalDateTimeTypeHandler.setNonNullParameter(LocalDateTimeTypeHandler.java:33)

    at org.apache.ibatis.type.LocalDateTimeTypeHandler.setNonNullParameter(LocalDateTimeTypeHandler.java:28)

    at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:73)

    ... 103 more

原因:
LocalDateTime是java8新增日期时间类型,在mybatis 3.4版本之后有对应的转换类org.apache.ibatis.type.LocalDateTimeTypeHandler,但数据库的jdbc驱动(jdk6)里IfxPreparedStatement.setObject(java.lang.String, java.lang.Class<T>)中未实现该方法。
解决方法:
由于驱动是jdk6的,暂时还没有实现LocalDateTime,故需要在mybatis中对LocalDateTime进行转换。已知mybatis-typehandlers-jsr310专门提供了针对java8的新日期时间类型的转换类,所以将这个依赖加进来就好了,但是里面的转换类的包名和类名都和新版本的mybatis包的一致,所以在pom文件中其依赖必须添加在mybatis相关依赖之前,保证优先加载使用。

1

2

3

4

5

6

<!-- 必须在mybatis相关依赖之前 -->

<dependency>

    <groupId>org.mybatis</groupId>

    <artifactId>mybatis-typehandlers-jsr310</artifactId>

    <version>1.0.2</version>

</dependency>

相关文档:
DEMO文件:
mybatis-plus-demo
在mybatis-plus中增加对GBase 8s的支持

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论