当前使用版本(必填,否则不予处理)

3.4.3.1

该问题是如何引起的?(确定最新版也有问题再提!!!)

由原先的mybatis升级为mybatis-plus带时区的日期类型转换异常

重现步骤(如果有就写完整)

数据库版本:PostgreSQL 13.1 建表语句:

create table time_test
(
    id   bigserial not null
        constraint time_test_pk
            primary key,
    time timestamp with time zone
);

model

@Data
@ToString
public class TimeTest {
    private Long id;
    private LocalDateTime time;
}

执行最基础的查询

  List<TimeTest> warehouseReceiptDOS = dao.selectList(null);

报错信息

org.springframework.jdbc.BadSqlGrammarException: Error attempting to get column 'time' from result set. Cause: org.postgresql.util.PSQLException: Cannot convert the column of type TIMESTAMPTZ to requested type timestamp. ; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: Cannot convert the column of type TIMESTAMPTZ to requested type timestamp.

at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy100.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMany(MybatisMapperMethod.java:166)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:77)
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.$Proxy117.selectList(Unknown Source)

Comment From: qmdx

你指定 tableFiled jdbcType 属性 TIME_WITH_TIMEZONE 试试

Comment From: weihubeats

MyBatis-Plus Cannot convert the column of type TIMESTAMPTZ to requested type timestamp

指定了一样报错,其次这种解决方式对原有代码的侵入性太强了,我自定义一个LocalDateTimeTypeHandler好像也没起到什么效果。

Comment From: miemieYaho

https://github.com/mybatis/typehandlers-jsr310

Comment From: weihubeats

我加了相关依赖,还是报相同的错误,目前连接池用的springboot默认的 HikariCP(版本:3.2.0),初步看好像是在使用结果映射的时候是使用的HikariCP的。

Comment From: weihubeats

PostgreSQL 带时区类型为OffsetDateTime,自定义一个OffsetDateTime类型转换即可解决