shardingsphere.core.exception.ShardingException: Unsupported Date type:class java.lang.String

最近使用sharding做分表, 其中做一个简单的查询时出现以下报错.

Cause: org.apache.shardingsphere.core.exception.ShardingException: Unsupported Date type:class java.lang.String
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
	... 59 common frames omitted

经跟踪源码发现在执行到org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(Object proxy, Method method, Object[] params)时

@Override
  public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, params);
      }    
      Object o = method.invoke(rs, params);
      if ("next".equals(method.getName())) {
        if (((Boolean) o)) {
          rows++;
          if (isTraceEnabled()) {
            ResultSetMetaData rsmd = rs.getMetaData();
            final int columnCount = rsmd.getColumnCount();
            if (first) {
              first = false;
              printColumnHeaders(rsmd, columnCount);
            }
            //注意此处
            printColumnValues(columnCount);
          }
        } else {
          debug("     Total: " + rows, false);
        }
      }
      clearColumnInfo();
      return o;
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
  }

printColumnValues(columnCount);处报错.继续查看该方法发现其中colname = rs.getString(i);此处值类型为其实是Date类型.

继续跟踪此处源码到

org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset.ResultSetUtil中执行convertDateValue时报错.
private static Object convertDateValue(final Object value, final Class convertType) {
        Date date = (Date) value;
        switch (convertType.getName()) {
            case "java.sql.Date":
                return new java.sql.Date(date.getTime());
            case "java.sql.Time":
                return new Time(date.getTime());
            case "java.sql.Timestamp":
                return new Timestamp(date.getTime());
            default:
                throw new ShardingException("Unsupported Date type:%s", convertType);
        }
    }

解决方法:

此问题应该是一个打印sql查询结果的bug,只需要设置不打印sql结果即可.

其中isTraceEnabled()具体子类判断是否打印sql查询结果日志.

我的日志设置的是

mybatis.configuration.log-impl= org.apache.ibatis.logging.stdout.StdOutImpl

该类默认isTraceEnabled()返回true.所以将该配置去掉使用默认的配置即可.

你可能感兴趣的:(Mybatis)