ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## BeanProcessor BeanProcessor是非常底层一个类,紧密跟JDBC 规范打交道,因此许多个性化扩展都可以通过实现BeanProcessor的某些方法来完成,比如,在前面例子中展示的让Clickhouse的结果集能映射java.util.Date上,这是最常用的情况,BeanProcessor已经内置如下类型转化,你的数据库可以重新实现或者新增类型转化 ```java static BigDecimalTypeHandler bigDecimalHandler = new BigDecimalTypeHandler(); static BooleanTypeHandler booleanDecimalHandler = new BooleanTypeHandler(); static ByteArrayTypeHandler byteArrayTypeHandler = new ByteArrayTypeHandler(); static ByteTypeHandler byteTypeHandler = new ByteTypeHandler(); static CharArrayTypeHandler charArrayTypeHandler = new CharArrayTypeHandler(); static DateTypeHandler dateTypeHandler = new DateTypeHandler(); static DoubleTypeHandler doubleTypeHandler = new DoubleTypeHandler(); static FloatTypeHandler floatTypeHandler = new FloatTypeHandler(); static IntegerTypeHandler integerTypeHandler = new IntegerTypeHandler(); static LongTypeHandler longTypeHandler = new LongTypeHandler(); static ShortTypeHandler shortTypeHandler = new ShortTypeHandler(); static SqlDateTypeHandler sqlDateTypeHandler = new SqlDateTypeHandler(); static SqlXMLTypeHandler sqlXMLTypeHandler = new SqlXMLTypeHandler(); static StringTypeHandler stringTypeHandler = new StringTypeHandler(); static TimestampTypeHandler timestampTypeHandler = new TimestampTypeHandler(); static TimeTypeHandler timeTypeHandler = new TimeTypeHandler(); static CLobJavaSqlTypeHandler clobTypeHandler = new CLobJavaSqlTypeHandler(); static BlobJavaSqlTypeHandler blobTypeHandler = new BlobJavaSqlTypeHandler(); static LocalDateTimeTypeHandler localDateTimeHandler = new LocalDateTimeTypeHandler(); static LocalDateTypeHandler localDateHandler = new LocalDateTypeHandler(); ``` 如果考虑到某个类的所有子类都采用指定的Handler,那需要调用addAcceptType方法,指明,比如JsonNode类都使用JsonNodeTypeHandler ```java JsonNodeTypeHandler typeHandler = new JsonNodeTypeHandler(); sqlManager.getDefaultBeanProcessors().addAcceptType( new BeanProcessor.InheritedAcceptType( JsonNode.class,typeHandler)); ``` 另外一个扩展方法可能是setPreparedStatementPara,这是给PreparedStatement赋值,如果有需要特殊处理逻辑,也可以扩展此处。 还有一个很少用的扩展地方是getColName方法,他是根据ResultSet结果集,返回结果集的列名称,在Hive中,就重新实现了此方法,因为Hive会把SQL的子查询的前缀也传递到Java侧,比如 ``` select * from (select id from user) t ``` 在JDBC返回结果中,列名是t.id,而不是id,这样会导致无法映射,因此有些情况,需要排除这个前缀