Error attempting to get column time from result set. Cause: java.sql.SQLFe Error date LocalDateTime serialization

Remarks: When using sharding-JDBC, this solution uses Timestamp (java.sql.Timestamp) to receive in useless entities

wrong reason:

Error message: Error attempting to get column 'time' from result set. Cause: java.sql.SQLFe
Reason for error: Date cannot be serialized and deserialized normally

The current technology is
1. The database field is datetime type
2. The mybatis-plus framework is used, currently 3.3.0
3. The entity class uses LocalDateTime,
Insert picture description here
Insert picture description here

Solution 1: Add the serialized jar package

     <!-- Po类中的域,可以用LocalDate来映射数据库中的date类型字段了,可以用LocalTime来映射数据库中的time类型字段了,可以用LocalDateTime字段来映射数据库中的datetime类型字段了 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-typehandlers-jsr310</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.9.2</version>
        </dependency>

Solution 2: Configure date serializer and deserializer

The format of the returned data can be: yyyy-MM-dd HH:mm:ss

LocalDateTimeConvertConfig

package com.ws.ldy.config;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * LocalDateTime 日期适配器
 *
 * @author MoCha
 * @date 2019/11/30
 */
@Configuration
public class LocalDateTimeConvertConfig {
    
    
    @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
    private String pattern;

    /**
     * 配置LocalDateTime 序列化器与反序列化器
     * <p>
     * 说明:
     * 1. 下面配置的new LocalDateTimeDeserializer()为我们定制的LocalDateTime反序列化器
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    
    
        return builder -> {
    
    
            builder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer());
        };
    }

    /**
     * LocalDateTime 序列化器
     */
    @Bean
    public LocalDateTimeSerializer localDateTimeSerializer() {
    
    
        return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
    }
}

2 、 LocalDateTimeDeserializer

package com.ws.ldy.config;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * LocalDateTime 反序列化器
 * <p>
 * 说明:
 * 1. 借用hutool相关工具类
 *
 * @author MoCha
 * @date 2019/11/30
 */
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
    
    
    @Override
    public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    
    
        String dateStr = parser.getText();
        DateTime dateTime;
        try {
    
    
            dateTime = DateUtil.parse(dateStr);
        } catch (Exception e) {
    
    
            dateTime = DateUtil.parseDateTime(dateStr.replaceAll("T", " "));
        }
        Date date = dateTime.toJdkDate();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    @Override
    public Class<?> handledType() {
    
    
        // 关键
        return LocalDateTime.class;
    }
}

wrong reason:

Error message: Error attempting to get column 'time' from result set. Cause: java.sql.SQLFe
Reason for error: Date cannot be serialized and deserialized normally

The current technology is
1. The database field is datetime type
2. The mybatis-plus framework is used, currently 3.3.0
3. The entity class uses LocalDateTime,
Insert picture description here
Insert picture description here

Solution 1: Add the serialized jar package

Guess you like

Origin blog.csdn.net/My_Way666/article/details/111591188