Mybatis-Plus自定义TypeHandler映射JSON类型为List

发布时间 2023-03-29 10:10:14作者: chandol

1.实体类

注意点:别忘了autoResultMap = true

@Data
@TableName(value = "report", autoResultMap = true)
public class Report implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
 
    @TableField(typeHandler = ReportUserListTypeHandler.class)
    private List<ReportUser> reportInfo; 
}

2.公共的ListTypeHandler

提供一个 JSONArray 转换为 Java List集合的处理器
@MappedJdbcTypes指定jdbc的类型
@MappedTypes指定Java的类型

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
 
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; 

@MappedJdbcTypes(JdbcType.ARRAY)
@MappedTypes({List.class})
public abstract class ListTypeHandler<T> extends BaseTypeHandler<List<T>>
{
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<T> parameter, JdbcType jdbcType) throws SQLException {
        String content = CollUtil.isEmpty(parameter) ? null : JSON.toJSONString(parameter);
        ps.setString(i, content);
    }

    @Override
    public List<T> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return this.getListByJsonArrayString(rs.getString(columnName));
    }

    @Override
    public List<T> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return this.getListByJsonArrayString(rs.getString(columnIndex));
    }

    @Override
    public List<T> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return this.getListByJsonArrayString(cs.getString(columnIndex));
    }

    private List<T> getListByJsonArrayString(String content) {
        return StrUtil.isBlank(content) ? new ArrayList<>() : JSON.parseObject(content, this.specificType());
    }

    /**
     * 具体类型,由子类提供
     *
     * @return 具体类型
     */
    protected abstract TypeReference<List<T>> specificType();
}

3.具体的ListTypeHandler

由具体的子类提供List集合泛型类型

import com.alibaba.fastjson.TypeReference;
import com.chandol.entity.po.ReportUser;
import java.util.List;  

public class ReportUserListTypeHandler extends ListTypeHandler<ReportUser> { 
    @Override
    protected TypeReference<List<ReportUser>> specificType() {
        return new TypeReference<List<ReportUser>>() {
        };
    }    
}

4.为什么不在公共类直接提供TypeReference<List>

如果在 ListTypeHandler 类中直接提供 TypeReference<List> 这种类型,那就等效于TypeReference<List> 这种类型,后续 fastjson 在转换时无法确定具体的 Java 类型,转换后的类型最终就会是 List ;同理,如果使用 Jackson 作为 JSON 转换工具,不确定具体类型时,最总会被转换为LinkedHashMap 类型,都需要再使用 TypeReference 来转换一次。




参考:

https://www.yisu.com/zixun/675336.html