Class MySQL8DateTimeSqlTypeFilter

java.lang.Object
javax.management.NotificationBroadcasterSupport
com.alibaba.druid.filter.FilterAdapter
com.alibaba.druid.filter.mysql8datetime.MySQL8DateTimeSqlTypeFilter
All Implemented Interfaces:
Filter, Wrapper, NotificationBroadcaster, NotificationEmitter

public class MySQL8DateTimeSqlTypeFilter extends FilterAdapter
针对mysql jdbc 8.0.23及以上版本,如果调用方没有使用orm框架,而是直接调用ResultSet的getObject方法,则针对DATETIME类型的字段,得到的对象从TimeStamp类型变成了LocalDateTime类型,导致调用方出现类型转换异常 通过Filter控制将对象类型转换成原来的类型
Author:
lizongbo
See Also:
  • Constructor Details

    • MySQL8DateTimeSqlTypeFilter

      public MySQL8DateTimeSqlTypeFilter()
  • Method Details

    • resultSet_getObject

      public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, int columnIndex) throws SQLException
      针对mysql jdbc 8.0.23及以上版本,通过该方法控制将对象类型转换成原来的类型
      Specified by:
      resultSet_getObject in interface Filter
      Overrides:
      resultSet_getObject in class FilterAdapter
      Parameters:
      chain - chain the FilterChain object that represents the filter chain
      result - the ResultSetProxy object that represents the result set
      columnIndex - the index of the column to retrieve
      Returns:
      an Object holding the column value, or null if the value is SQL NULL
      Throws:
      SQLException - if a database access error occurs or the columnIndex is invalid
      See Also:
    • resultSet_getObject

      public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, String columnLabel) throws SQLException
      针对mysql jdbc 8.0.23及以上版本,通过该方法控制将对象类型转换成原来的类型
      Specified by:
      resultSet_getObject in interface Filter
      Overrides:
      resultSet_getObject in class FilterAdapter
      Parameters:
      chain - chain the FilterChain object that represents the filter chain
      result - the ResultSetProxy object that represents the result set
      columnLabel - the label of the column to retrieve
      Returns:
      an Object holding the column value, or null if the value is SQL NULL
      Throws:
      SQLException - if a database access error occurs or the columnLabel is invalid
      See Also:
    • getObjectReplaceLocalDateTime

      public static Object getObjectReplaceLocalDateTime(Object obj)
      Replaces a LocalDateTime object with its equivalent Timestamp object. If the input object is not an instance of LocalDateTime, it is returned as is. This method is specifically designed to handle cases where upgrading to MySQL JDBC 8.0.23 or above requires converting LocalDateTime objects back to the older compatible type.
      Parameters:
      obj - the object to be checked and possibly replaced
      Returns:
      the replaced object if it is a LocalDateTime, or the original object otherwise
    • resultSet_getMetaData

      public ResultSetMetaData resultSet_getMetaData(FilterChain chain, ResultSetProxy resultSet) throws SQLException
      Retrieves the metadata for the result set, including information about the columns and their properties. This method wraps the original result set metadata with a custom implementation that handles MySQL 8.0.23 or above compatibility for LocalDateTime objects.
      Specified by:
      resultSet_getMetaData in interface Filter
      Overrides:
      resultSet_getMetaData in class FilterAdapter
      Parameters:
      chain - the FilterChain object that represents the filter chain
      resultSet - the ResultSetProxy object that represents the result set
      Returns:
      a ResultSetMetaData object containing the metadata for the result set
      Throws:
      SQLException - if a database access error occurs