Sharding jdbc分库分表与mybatis-plus一起使用与LocalDateTime冲突问题解决方案
背景
问题报错:Error attempting to get column ‘create_time’ from result set. Cause: java.sql.SQLFeatureNotSupportedException: getObject with type
java版本:java11
sharding jdbc版本:4.1.1
mybatis-plus版本:3.4.0
原因
Mybatis与Sharding版本某一个不支持LocalDateTime
解决
重写解析器
package com.ypsx.cart.server.infrastructure.handler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
/**
* 描述:重写LocalDateTimeTypeHandler ,解决sharding_jdbc + ibatis LocalDateTime转换问题
* Created by zjw on 2022/2/17 11:13
*/
@Component
public class LocalDateTimeTypeHandler extends org.apache.ibatis.type.LocalDateTimeTypeHandler {
public LocalDateTimeTypeHandler() {
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException {
ps.setTimestamp(i, new Timestamp(this.toTimeMillis(parameter)));
}
@Override
public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Timestamp sqlTimestamp = rs.getTimestamp(columnName);
return sqlTimestamp != null ? this.toLocalDateTime(sqlTimestamp.getTime()) : null;
}
@Override
public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp sqlTimestamp = rs.getTimestamp(columnIndex);
return sqlTimestamp != null ? this.toLocalDateTime(sqlTimestamp.getTime()) : null;
}
@Override
public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp sqlTimestamp = cs.getTimestamp(columnIndex);
return sqlTimestamp != null ? this.toLocalDateTime(sqlTimestamp.getTime()) : null;
}
private long toTimeMillis(LocalDateTime dateTime) {
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
private LocalDateTime toLocalDateTime(long timeMillis) {
return new Date(timeMillis).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
}