SpringBoot+MyBatis Plus对Map中Date格式转换的处理( 四 )

  • public class JacksonUtil {

  •    private static final Logger log = LoggerFactory.getLogger(JacksonUtil.class);

  •    private static final ObjectMapper MAPPER = createObjectMapper();

  •    private JacksonUtil() {}

  •    private static ObjectMapper createObjectMapper() {

  •        ObjectMapper objectMapper = new ObjectMapper();

  •        objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);

  •        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

  •        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  •        objectMapper.setDateFormat(df);

  •        return objectMapper;

  •    }

  •    public static ObjectMapper getObjectMapper() {

  •        return MAPPER;

  •    }

  • }

  • 第三是启动后修改 MyBatisPlus 的设置, 即下面的 changeObjectMapper() 这个方法, 直接换成了上面工具类中定义的 ObjectMapper, 这样在 MyBatis 读写数据库时的格式也统一了.
    1. @Configuration

    2. @MapperScan(basePackages = {"com.somewhere.commons.impl.mapper"})

    3. public class MybatisPlusConfig {

    4.    @Bean

    5.    public MybatisPlusInterceptor mybatisPlusInterceptor() {

    6.        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

    7.        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));

    8.        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

    9.        return interceptor;

    10.    }

    11.    @PostConstruct

    12.    public void changeObjectMapper() {

    13.        // This will unify the date format with util methods

    14.        JacksonTypeHandler.setObjectMapper(JacksonUtil.getObjectMapper());

    15.    }

    16. }



      【SpringBoot+MyBatis Plus对Map中Date格式转换的处理】

      经验总结扩展阅读