自定转换器:
package com.comon.utils; import java.io.IOException; import java.io.Serializable; import javax.persistence.AttributeConverter; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; @Slf4j public class JpaConverterJsonUtils implements AttributeConverter<Object,String>,Serializable { /** * */ private static final long serialVersionUID = 1L; private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); public String convertToDatabaseColumn(Object attribute) { try { return OBJECT_MAPPER.writeValueAsString(attribute); } catch (Exception e) { log.error("convert data error,the exception is {}",e); return null; } } public Object convertToEntityAttribute(String dbData) { try { if(dbData!=null){ return OBJECT_MAPPER.readValue(dbData, Object.class); } } catch (IOException e) { log.error("convert data error,the exception is {}",e); return null; } return dbData; } }
使用: 在自定名字上添加 eg: XXXX.DO
@Convert(converter=JpaConverterJsonUtils.class) private List<JSONObject> milestones; //在db中为json 类型