1.前言
MySQL从5.7版本之后开始支持JSON数据类型,相比于JSON格式的字符串类型有如下优势:存储在JSON列中的JSON文档的会被自动验证。无效的文档会产生错误;最佳存储格式。存储在JSON列中的JSON文档会被转换为允许快速读取文档元素的内部格式。
2.背景
mybatis目前还不支持映射json的数据格式,需要我们自己进行增强
3.实现
对mybatis TypeHandler进行增强
3.1ListStringTypeHandler 核心代码
代码语言:java复制package com.xxx.xxx.pipeline.repository.typehandle;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.SneakyThrows;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListStringTypeHandler implements TypeHandler<List<String>> {
@Override
public void setParameter(PreparedStatement ps, int i, List parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, JsonUtils.objectToJson(parameter));
}
@SneakyThrows
@Override
public List getResult(ResultSet rs, String columnName) throws SQLException {
return JsonUtils.jsonToObject(rs.getString(columnName), new TypeReference<List>() {
});
}
@SneakyThrows
@Override
public List getResult(ResultSet rs, int columnIndex) throws SQLException {
return JsonUtils.jsonToObject(rs.getString(columnIndex), new TypeReference<List>() {
});
}
@SneakyThrows
@Override
public List getResult(CallableStatement cs, int columnIndex) throws SQLException {
return JsonUtils.jsonToObject(cs.getString(columnIndex), new TypeReference<List>() {
});
}
}
3.2 PO代码
代码语言:java复制package com.xxx.xx.pipeline.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import lombok.experimental.SuperBuilder;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class PluginConfigPO extends BasePO {
private static final String DEFAULT_JSON_LIST = "[]";
private String gitUrl;
private String name;
private String title;
private String version;
private String description;
private String commitId;
private List<String> osTypes;
private List<String> categories;
private List<String> contributors;
private String releaseNote;
private LocalDateTime publishedAt;
3.3 mapper代码
代码语言:java复制package com.xxx.xx.pipeline.repository;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface PluginConfigDAO {
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@Insert("insert into plugin_config(git_url,name,commit_id,"
"os_types,categories,"
"published_at,contributors,title,"
"version,release_note,description,operator,status,"
"create_time,update_time) "
"value (#{gitUrl},#{name},#{commitId},"
"#{osTypes,jdbcType=OTHER ,typeHandler=com.xxx.xx.pipeline.repository.typehandle.ListStringTypeHandler},"
"#{categories,jdbcType=OTHER ,typeHandler=com.xxx.xx.pipeline.repository.typehandle.ListStringTypeHandler},"
"#{publishedAt},#{contributors,jdbcType=OTHER ,typeHandler=com.xxx.xx.pipeline.repository.typehandle.ListStringTypeHandler},#{title},"
"#{version},#{releaseNote},#{description},#{operator},#{status},"
"now(),now())")
int create(PluginConfigPO pluginConfig);
3.4 配置文件代码
代码语言:java复制mybatis:
configuration:
map-underscore-to-camel-case: true
type-handlers-package: com.xxx.xx.pipeline.repository.typehandle