JdbcTemplate.queryForList()查询结果如何对Date等日期类型进行格式化?

1.情景展示

在实际开发中,我们往往会遇到这样的需求:

需要对多个数据库进行操作,这用mybatis等框架来进行操作显然不合理,也无法满足多样化的需求。

通过java来进行JDBC操作无疑是最好的选择。

如下图所示,通过org.springframework.jdbc.core.JdbcTemplate.queryForList()方法查询到的数据,含有日期类型。

最终返回给前端的时候日期类在进行序列化时,被转成了这个样子,如何对日期类型数据进行格式化?

2.具体分析

有两种思路:

一种是不使用queryForList方法,而采用其它方法,在拿到数据时进行转换。

另一种是仍然使用queryForList()获取数据,在返回给前端的时候手动进行序列化操作,而不是依赖spring的自动序列化功能。

3.解决方案

我这里推荐大家使用第二种方式:对queryForList()的返回结果进行二次处理。

其实,日期类型被转换成数组形式,本质上是JSON对象序列化的默认序列策略,我们只需要指定日期类型的序列化格式,这个问题就迎刃而解了。

实际上,是解决JSON序列化过程中对于日期类型的处理模式问题。

原本的操作方式是:

让spring自动序列化返回的数据

由于spring自动序列化的方式并不符合我们的实际需求,所以,序列化这一步骤就不让spring类处理了。

直接返回序列化后的字符串,也就是String类型。

第一步:响应前端的数据直接指定为String类型

第二步:对queryForList()的查询结果进行序列化

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.extern.slf4j.Slf4j;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
 * 将日期类型进行格式化(转成时间字符串)
 * java.time.LocalDateTime-->yyyy-MM-dd HH:mm:ss
 * java.time.LocalDate-->yyyy-MM-dd
 * java.time.LocalTime-->HH:mm:ss
 * java.util.Date-->yyyy-MM-dd HH:mm:ss
 * java.sql.Timestamp-->yyyy-MM-dd HH:mm:ss
 * @description: 通过Jackson实现
 * @attention: 如果自定义key名称的话,通过添加注解@JsonProperty来实现
 * @date: 2024/1/31 10:58
 * @param: bean java类对象
 * @return: java.lang.String json字符串
 */
public static String convertDateToStr(Object bean) {
    if (null == bean) return "";

    String jsonStr;
    try {
        ObjectMapper mapper = new ObjectMapper();
        // 初始化JavaTimeModule
        JavaTimeModule timeModule = new JavaTimeModule();
        // 设置LocalDateTime的序列化格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
        // 设置LocalDate的序列化格式
        dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(dateTimeFormatter));
        // 设置LocalTime的序列化格式
        dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(dateTimeFormatter));
        // 设置Date的序列化格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        timeModule.addSerializer(Date.class, new DateSerializer(false, sdf));
        // 设置Timestamp的序列化格式
        timeModule.addSerializer(Timestamp.class, new DateSerializer(false, sdf));

        mapper.registerModule(timeModule);
        jsonStr = mapper.writeValueAsString(bean);
    } catch (JsonProcessingException e) {
        log.error("JavaBean-->json字符串转换失败:" + e.getMessage());
        return "";
    }

    return jsonStr;
}

将查询结果进行序列化并返回。

说明:

图片展示的orcle分页有误,正确的应是:

int start = (myPageIndex - 1) * myPageSize + 1;

4.效果展示 

写在最后

  哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

 相关推荐:

posted @ 2024-01-31 16:04  Marydon  阅读(272)  评论(0编辑  收藏  举报