Mybatis-plus 中 Map 类型对应 Mysql 中的 varchar

比如我现在有如下实体类,那么数据肯定是不能直接插入 数据库 的,因为 mysql 中 varchar 对应的 java 类型应该是 String,而不是 Map。

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class File {
    private Integer id;
 
    private Map<String, String> fileUrl;
}

那么这时候 Mybatis-plus 的 typeHandler 就派上用场了,新建一个 MapTypeHandler 类。

import org.springframework.util.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.ibatis.type.*;
 
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
 
@MappedTypes(Map.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MapTypeHandler extends BaseTypeHandler<Map<String, String>> {
    private ObjectMapper objectMapper = new ObjectMapper();
 
    // 该方法用于将 Java 对象转换为数据库中的类型。
    // 在本例中,将一个 Map<String, String> 对象转换为一个 JSON 字符串,并将其设置为 PreparedStatement 中的参数。
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Map<String, String> parameter, JdbcType jdbcType) throws SQLException {
        try {
            String json = objectMapper.writeValueAsString(parameter);
            ps.setString(i, json);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
 
    // 用于从结果集中获取一个指定列名的值,并将其转换为 Java 对象。
    // 在本例中,获取一个 JSON 字符串,并将其解析为一个 Map<String, String> 对象。如果 JSON 字符串为空,将返回 null 值。
    @Override
    public Map<String, String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String json = rs.getString(columnName);
        return parseJson(json);
    }
 
    // 该方法与上面的方法类似,只不过是根据列索引获取结果集中的值。
    @Override
    public Map<String, String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String json = rs.getString(columnIndex);
        return parseJson(json);
    }
 
    // 该方法用于从存储过程中获取一个指定列索引的值,并将其转换为 Java 对象。
    // 在本例中,获取一个 JSON 字符串,并将其解析为一个 Map<String, String> 对象。如果 JSON 字符串为空,将返回 null 值。
    @Override
    public Map<String, String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String json = cs.getString(columnIndex);
        return parseJson(json);
    }
 
    // 该方法用于将一个 JSON 字符串解析为一个 Map<String, String> 对象。
    private Map<String, String> parseJson(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            return objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

然后在实体类上加上注解即可。

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class File {
    private Integer id;
 
    @TableField(typeHandler = MapTypeHandler.class)
    private Map<String, String> fileUrl;
}
正文完
 0
admin
版权声明:本站原创文章,由 admin 2016-02-11发表,共计2510字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处:https://www.mlzj.net。