快捷搜索: 王者荣耀 脱发

达梦数据text类型字段引发的一些报错

springboot + mybatisplus + 达梦数据库
数据库text字段,查询时使用map接收,jdbc对应的类型为Clob类型,后续会通过默认类型转换器转换为达梦的dm.jdbc.driver.DmdbNClob类型,某些情况下(不知道具体原因)在JSONObject.fromObject()里会导致循环依赖报错。此外在导出Excel中也会显示为dm.jdbc.driver.DmdbNClob@3b82297f。
1、可通过注册自定义类型转换器TypeHandler,统一处理Clob类型。
package common.typeHandler;
import org.apache.ibatis.type.ClobTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.Clob;

@MappedTypes(Clob.class)
@MappedJdbcTypes(JdbcType.CLOB)
public class DmdbNClobTypeHandler extends ClobTypeHandler{

}
application.yml 配置
mybatis-plus:
  type-handlers-package: common.typeHandler

2、如果不使用上面的全局处理,JSONObject.fromObject(object,jsonConfig)的报错还可以单独处理

定义继承Clob的实现类NClobProxyImpl,来重写处理解析获取数据的方法。

JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(NClobProxyImpl.class, new DmdbNClobJsonValueProcessor());
JSONObject jsonStr = JSONObject.fromObject(object,jsonConfig);

public class DmdbNClobJsonValueProcessor implements JsonValueProcessor { @Override public Object processArrayValue(Object o, JsonConfig jsonConfig) { return o == null? null : ((DmdbNClob)((NClobProxyImpl) o).getRawNClob()).data; }

@Override public Object processObjectValue(String s, Object o, JsonConfig jsonConfig) { return o == null? null : ((DmdbNClob)((NClobProxyImpl) o).getRawNClob()).data; } }

经验分享 程序员 微信小程序 职场和发展