【问题标题】:Has MyBatis ever been able to cast java Calendar?MyBatis 曾经能够投射 java Calendar 吗?
【发布时间】:2022-01-01 16:51:44
【问题描述】:

前两天写了一段访问数据库的程序,运行良好。它包含一些关于以 TIME 作为参数搜索数据的操作。在 db 中,在我的例子中是 pgsql,它是时间戳。当我使用 MyBatis 时,我查看了 MyBatis 网站,我特别记得它说 MyBatis 会将 java.util.Date AND Calendar 转换为时间戳,并且由于 Calendar 很容易计算,那就是我选择的那个。 然后大约 3 小时前,我的程序在已经将 20k 行数据写入我的数据库后崩溃了。错误信息是这样的:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='calendar', 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: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.GregorianCalendar. Use setObject() with an explicit Types value to specify the type to use.

一开始我还以为是空指针的东西,所以很自然的从云服务器拉出来,在本地运行调试,错误依旧。

Can't infer the SQL type to use for an instance of java.util.GregorianCalendar. Use setObject() with an explicit Types value to specify the type to use.

最后我意识到这是一个转换问题,MyBatis 拒绝使用 Calendar 作为时间戳的参数,至少在没有进一步配置的情况下是这样。 然后我检查了MyBatis website,但没有提到任何日历。

问题本身就很简单解决了,调用 getTime() 来获取替换所有日历的日期。 但整件事仍然让我失望。 有人记得 MyBatis 是否曾经能够投射日历吗?或者我是否需要就我的记忆障碍进行医疗护理?

【问题讨论】:

  • Calendar 理论上可以使用,但现在它大多已经过时了。使用 LocalDate、LocalTime 和所有 java.time 类。现在所有引擎都支持它(可能从 2018 年开始)。

标签: java mybatis spring-mybatis mybatis-mapper


【解决方案1】:

我放弃了这个问题。但是,如果您出于同样的原因来到这里,请不要查看它,将日历更改为日期并继续前进。不值得考虑。

【讨论】:

  • java.time 包中的东西替换 Calendar / Date 可能会更好。
  • 是的,我应该这样做。我可能是唯一在 java 17 上使用日历的人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 1970-01-01
  • 2021-03-03
相关资源
最近更新 更多