|
|
@@ -0,0 +1,45 @@
|
|
|
+package com.payment.platform.common.handler;
|
|
|
+
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * String ↔ PostgreSQL jsonb 类型转换器
|
|
|
+ * MyBatis-Plus 默认将 String 映射为 VARCHAR,与 jsonb 列类型冲突。
|
|
|
+ * 此 handler 将 String 值包装为 PGobject("jsonb"),解决 INSERT/UPDATE 时的类型转换问题。
|
|
|
+ */
|
|
|
+@MappedTypes(String.class)
|
|
|
+public class JsonbTypeHandler extends BaseTypeHandler<String> {
|
|
|
+
|
|
|
+ private static final String JSONB_TYPE = "jsonb";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
|
|
|
+ PGobject pgObject = new PGobject();
|
|
|
+ pgObject.setType(JSONB_TYPE);
|
|
|
+ pgObject.setValue(parameter);
|
|
|
+ ps.setObject(i, pgObject);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ return rs.getString(columnName);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ return rs.getString(columnIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ return cs.getString(columnIndex);
|
|
|
+ }
|
|
|
+}
|