天天看點

Sharding-JDBC 踩坑 LocalDate

Sharding-JDBC 踩坑

涉及代碼 Don212
  • Spring Boot 2.5.2
  • Druid 1.2.6
  • Mybatis Plus 3.4.3
  • Sharding jdbc 4.1.1

1、屬性使用 LocalDate 的問題

運作時報錯:

org.springframework.dao.InvalidDataAccessApiUsageException: Error attempting to get column 'c_date' from result set.  Cause: java.sql.SQLFeatureNotSupportedException: getObject with type
; getObject with type; nested exception is java.sql.SQLFeatureNotSupportedException: getObject with type

	at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:96)

Caused by: java.sql.SQLFeatureNotSupportedException: getObject with type
	at org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationResultSet.getObject(AbstractUnsupportedOperationResultSet.java:221)
	at org.apache.ibatis.type.LocalDateTypeHandler.getNullableResult(LocalDateTypeHandler.java:38)
	at org.apache.ibatis.type.LocalDateTypeHandler.getNullableResult(LocalDateTypeHandler.java:28)
           

翻閱代碼,可以看到

AbstractUnsupportedOperationResultSet.class

@Override
    public final <T> T getObject(final String columnLabel, final Class<T> type) throws SQLException {
        throw new SQLFeatureNotSupportedException("getObject with type");
    }
           

LocalDateTypeHandler.class

@Override
  public LocalDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
    return rs.getObject(columnName, LocalDate.class);
  }
           

直接抛出了異常!!!

試試寫一個

LocalDateTypeHandler

package com.learn.springboot.type;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Component
@MappedTypes(LocalDate.class)
@MappedJdbcTypes(value = JdbcType.DATE, includeNullJdbcType = true)
public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType) throws SQLException {
        ps.setObject(i, parameter);
    }

    @Override
    public LocalDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
        if (null == rs.getObject(columnName)) {
            return null;
        }
        return LocalDate.parse(rs.getObject(columnName).toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    @Override
    public LocalDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        if (null == rs.getObject(columnIndex)) {
            return null;
        }
        return LocalDate.parse(rs.getObject(columnIndex).toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    @Override
    public LocalDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        if (null == cs.getObject(columnIndex)) {
            return null;
        }
        return LocalDate.parse(cs.getObject(columnIndex).toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

}

           

測試一下

2021-07-14 17:27:52.694  INFO 13692 --- [           main] ShardingSphere-SQL                       : Actual SQL: m1 ::: SELECT  c_id,c_name,user_id,c_status,c_date  FROM course_1 
 
 WHERE (user_id = ? AND c_id = ?) ::: [110, 1411618397971636226]
Course(cId=1411618397971636226, cName=sharding10, userId=110, cStatus=Normal, cDate=2021-07-14)
           

正常啟動,目前沒什麼問題 -_-

繼續閱讀