fix(mybatis): jsonb 类型处理器兼容空字符串, 避免写入报 invalid input syntax for type json

This commit is contained in:
DaxPay Dev
2026-06-26 09:15:57 +08:00
parent c5a876de4a
commit 36f1ec9b09

View File

@@ -10,6 +10,7 @@ import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
/// # PostgreSQL jsonb 字符串类型处理器。
///
@@ -27,6 +28,11 @@ public class JsonbStringTypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
throws SQLException {
// 空字符串不是合法 JSON, jsonb 列应存 NULL
if (parameter == null || parameter.isBlank()) {
ps.setNull(i, Types.OTHER);
return;
}
PGobject jsonObject = new PGobject();
jsonObject.setType("jsonb");
jsonObject.setValue(parameter);