|
@@ -0,0 +1,45 @@
|
|
|
+package com.siwei.apply.handler;
|
|
|
+
|
|
|
+import com.siwei.apply.utils.JsonUtil;
|
|
|
+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 org.postgresql.util.PGobject;
|
|
|
+
|
|
|
+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.OTHER)
|
|
|
+public class JsonbTypeHandler extends BaseTypeHandler<Map<String, Object>> {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType) throws SQLException {
|
|
|
+ PGobject pgObject = new PGobject();
|
|
|
+ pgObject.setType("jsonb");
|
|
|
+ pgObject.setValue(JsonUtil.toJson(parameter)); // 使用 JsonUtil 转 JSON 字符串
|
|
|
+ ps.setObject(i, pgObject);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ String json = rs.getString(columnName);
|
|
|
+ return json == null ? null : JsonUtil.fromJson(json); // 使用 JsonUtil 解析 JSON
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ String json = rs.getString(columnIndex);
|
|
|
+ return json == null ? null : JsonUtil.fromJson(json);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ String json = cs.getString(columnIndex);
|
|
|
+ return json == null ? null : JsonUtil.fromJson(json);
|
|
|
+ }
|
|
|
+}
|