Spring Data Jpa之实体属性类型转换器AttributeConverter

目录

是什么?

AttributeConverter<x, y>是一个接口,此接口用于转化实体属性的,但id属性和关系属性不可用。它有两个方法:

  1. y convertToDatabaseColumn(x)

作用:将实体属性x转化为y存储到数据库中,即插入和更新操作时执行;

  1. x convertToEntityAttribute(y)

作用:将数据库中的字段y转化为实体属性x,即查询操作时执行。

在JPA注解中,有个@Convert注解,其中需要传入一个Class作为convert参数,该class需要实现AttributeConverter<X,Y>接口。下面来看看AttributeConverter接口的作用。

场景:接收对象存为字符串,将字符串转为对象显示。

实体

package cn.notemi.model.entity;
@Entity
@Data
public class Project {
    @NotNull
    private String name;
    private String shortName;
    private String description;
    @Convert(converter = ProjectPathConverter.class)
    private List<ProjectPathPoint> path;
}

数组属性封装对象

@Data
public class ProjectPathPoint {
    private Double longitude;
    private Double latitude;
}

创建Convert

package cn.notemi.model.converter;
@Converter(autoApply = true)
public class ProjectPathConverter implements AttributeConverter<List<ProjectPathPoint>, String> {

    @Override
    public String convertToDatabaseColumn(List<ProjectPathPoint> projectPathPoints) {
        String s = JacksonUtil.toJSon(projectPathPoints);
        return s;
    }

    @Override
    public List<ProjectPathPoint> convertToEntityAttribute(String s) {
        List<ProjectPathPoint> projectPathPointList = JacksonUtil.readValue(s, new TypeReference<List<ProjectPathPoint>>() {
        });
        return projectPathPointList;
    }
}

Jackson工具类

package cn.notemi.util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Title:JacksonUtil
 * Description:
 *
 * @author Flicker
 * @create 2017/8/17 0017 18:48
 **/
public class JacksonUtil {
  public static ObjectMapper objectMapper;

  /**
   * 使用泛型方法,把json字符串转换为相应的JavaBean对象。
   * (1)转换为普通JavaBean:readValue(json,Student.class)
   * (2)转换为List,如List<Student>,将第二个参数传递为Student
   * [].class.然后使用Arrays.asList();方法把得到的数组转换为特定类型的List
   *
   * @param jsonStr
   * @param valueType
   * @return
   */
  public static <T> T readValue(String jsonStr, Class<T> valueType) {
    if (objectMapper == null) {
      objectMapper = new ObjectMapper();
    }
    try {
      return objectMapper.readValue(jsonStr, valueType);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * json数组转List
   * @param jsonStr
   * @param valueTypeRef
   * @return
   */
  public static <T> T readValue(String jsonStr, TypeReference<T> valueTypeRef){
    if (objectMapper == null) {
      objectMapper = new ObjectMapper();
    }
    try {
      return objectMapper.readValue(jsonStr, valueTypeRef);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 把JavaBean转换为json字符串
   *
   * @param object
   * @return
   */
  public static String toJSon(Object object) {
    if (objectMapper == null) {
      objectMapper = new ObjectMapper();
    }
    try {
      return objectMapper.writeValueAsString(object);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}

Service查询方法

public List<ProjectPathPoint> findPathById(Long id){
        Project project = projectRepository.findOne(id);
        List<ProjectPathPoint> projectPathPointList = project.getPath();
        return projectPathPointList;
    }

文章最后编辑时间为:2017-09-16 17:01:53

标签: spring data spring data jpa

声明:本博客如无特殊说明皆为原创,转载请注明来源:Spring Data Jpa之实体属性类型转换器AttributeConverter谢谢!

相关文章

发表评论:

icon_question.pngicon_razz.pngicon_sad.pngicon_evil.pngicon_exclaim.pngicon_smile.pngicon_redface.pngicon_biggrin.pngicon_surprised.pngicon_eek.pngicon_confused.pngicon_cool.pngicon_lol.pngicon_mad.pngicon_twisted.pngicon_rolleyes.pngicon_wink.pngicon_idea.pngicon_arrow.pngicon_neutral.pngicon_cry.pngicon_mrgreen.png