|
@@ -0,0 +1,62 @@
|
|
|
+package com.onemap.apply.config;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.apache.ibatis.type.BaseTypeHandler;
|
|
|
+import org.apache.ibatis.type.JdbcType;
|
|
|
+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;
|
|
|
+
|
|
|
+@MappedTypes({Object.class})
|
|
|
+public class FileObjTypeHandler extends BaseTypeHandler<Object> {
|
|
|
+ private static final PGobject jsonObject = new PGobject();
|
|
|
+ private static final ObjectMapper objectMapper = new ObjectMapper(); // 使用 Jackson 的 ObjectMapper
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
|
|
|
+ if (preparedStatement != null) {
|
|
|
+ jsonObject.setType("jsonb");
|
|
|
+ try {
|
|
|
+ jsonObject.setValue(objectMapper.writeValueAsString(o)); // 使用 Jackson 进行序列化
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ throw new SQLException("Error converting Object to JSON", e);
|
|
|
+ }
|
|
|
+ preparedStatement.setObject(i, jsonObject);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
|
|
|
+ String json = resultSet.getString(s);
|
|
|
+ try {
|
|
|
+ return objectMapper.readValue(json, Object.class); // 使用 Jackson 进行反序列化
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ throw new SQLException("Error converting JSON to Object", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
|
|
|
+ String json = resultSet.getString(i);
|
|
|
+ try {
|
|
|
+ return objectMapper.readValue(json, Object.class); // 使用 Jackson 进行反序列化
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ throw new SQLException("Error converting JSON to Object", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
|
|
|
+ String json = callableStatement.getString(i);
|
|
|
+ try {
|
|
|
+ return objectMapper.readValue(json, Object.class); // 使用 Jackson 进行反序列化
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ throw new SQLException("Error converting JSON to Object", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|