1.处理过程
1.1 实体类设为autoResultMap = true
@TableName ( value = "table1" , autoResultMap = true )
1.2 字段 typeHandler = JacksonTypeHandler.class
@ApiModelProperty ( value = "明细" ) @TableField ( value = "detail" , typeHandler = JacksonTypeHandler . class ) private Object detail;
2.Json字段类型处理器
@Slf4j
@MappedTypes ( { Object . class } )
@MappedJdbcTypes ( JdbcType . VARCHAR )
public class JacksonTypeHandler extends AbstractJsonTypeHandler < Object > { private static ObjectMapper OBJECT_MAPPER ; private final Class < ? > type; private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter . ofPattern ( "yyyy-MM-dd HH:mm:ss" ) ; private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter . ofPattern ( "yyyy-MM-dd" ) ; private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter . ofPattern ( "HH:mm:ss" ) ; public JacksonTypeHandler ( Class < ? > type) { if ( log. isTraceEnabled ( ) ) { log. trace ( "JacksonTypeHandler(" + type + ")" ) ; } Assert . notNull ( type, "Type argument cannot be null" ) ; this . type = type; } @Override protected Object parse ( String json) { try { return getObjectMapper ( ) . readValue ( json, type) ; } catch ( IOException e) { throw new RuntimeException ( e) ; } } @Override protected String toJson ( Object obj) { try { return getObjectMapper ( ) . writeValueAsString ( obj) ; } catch ( JsonProcessingException e) { throw new RuntimeException ( e) ; } } public static ObjectMapper getObjectMapper ( ) { if ( null == OBJECT_MAPPER ) { OBJECT_MAPPER = new ObjectMapper ( ) ; OBJECT_MAPPER . configure ( SerializationFeature . WRITE_DATES_AS_TIMESTAMPS , false ) ; OBJECT_MAPPER . configure ( SerializationFeature . WRITE_DURATIONS_AS_TIMESTAMPS , false ) ; OBJECT_MAPPER . setDateFormat ( new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ) ; JavaTimeModule javaTimeModule = new JavaTimeModule ( ) ; javaTimeModule. addSerializer ( LocalDateTime . class , new LocalDateTimeSerializer ( DATE_TIME_FORMATTER ) ) ; javaTimeModule. addSerializer ( LocalDate . class , new LocalDateSerializer ( DATE_FORMATTER ) ) ; javaTimeModule. addSerializer ( LocalTime . class , new LocalTimeSerializer ( TIME_FORMATTER ) ) ; javaTimeModule. addSerializer ( Date . class , new DateSerializer ( false , new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ) ) ; javaTimeModule. addDeserializer ( LocalDateTime . class , new LocalDateTimeDeserializer ( DATE_TIME_FORMATTER ) ) ; javaTimeModule. addDeserializer ( LocalDate . class , new LocalDateDeserializer ( DATE_TIME_FORMATTER ) ) ; javaTimeModule. addDeserializer ( LocalTime . class , new LocalTimeDeserializer ( DATE_TIME_FORMATTER ) ) ; javaTimeModule. addDeserializer ( Date . class , new DateDeserializers. DateDeserializer ( new DateDeserializers. DateDeserializer ( ) , new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) , "yyyy-MM-dd HH:mm:ss" ) ) ; OBJECT_MAPPER . registerModule ( javaTimeModule) ; OBJECT_MAPPER . setTimeZone ( TimeZone . getDefault ( ) ) ; OBJECT_MAPPER . setSerializationInclusion ( JsonInclude. Include . NON_EMPTY ) ; } return OBJECT_MAPPER ; } public static void setObjectMapper ( ObjectMapper objectMapper) { Assert . notNull ( objectMapper, "ObjectMapper should not be null" ) ; JacksonTypeHandler . OBJECT_MAPPER = objectMapper; }
}