This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
package com.imyeyu.spring.handler;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* MySQL JSON 数据类型处理器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2021-07-04 09:36
|
||||
*/
|
||||
public class GsonHandler extends BaseTypeHandler<JsonElement> {
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, JsonElement parameter, JdbcType jdbcType) throws SQLException {
|
||||
ps.setString(i, parameter.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
return toElement(rs.getString(columnName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
return toElement(rs.getString(columnIndex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
return toElement(cs.getNString(columnIndex));
|
||||
}
|
||||
|
||||
private JsonElement toElement(String json) {
|
||||
if (TimiJava.isNotEmpty(json)) {
|
||||
JsonElement el = JsonParser.parseString(json);
|
||||
if (el.isJsonObject()) {
|
||||
return el.getAsJsonObject();
|
||||
}
|
||||
if (el.isJsonArray()) {
|
||||
return el.getAsJsonArray();
|
||||
}
|
||||
if (el.isJsonPrimitive()) {
|
||||
return el.getAsJsonPrimitive();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.imyeyu.spring.handler;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-03-16 15:42
|
||||
*/
|
||||
@MappedTypes(JsonNode.class)
|
||||
@MappedJdbcTypes({JdbcType.VARCHAR, JdbcType.CLOB, JdbcType.LONGVARCHAR})
|
||||
public class JsonNodeTypeHandler extends BaseTypeHandler<JsonNode> {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, JsonNode parameter, JdbcType jdbcType) throws SQLException {
|
||||
try {
|
||||
ps.setString(i, MAPPER.writeValueAsString(parameter));
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SQLException("Failed to serialize JsonNode to JSON", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
return parseJson(rs.getString(columnName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
return parseJson(rs.getString(columnIndex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
return parseJson(cs.getString(columnIndex));
|
||||
}
|
||||
|
||||
private JsonNode parseJson(String json) throws SQLException {
|
||||
if (json == null || json.isEmpty()) {
|
||||
return MAPPER.createObjectNode();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readTree(json);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SQLException("Failed to parse JSON to JsonNode", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user